图片处理
namespace ginkgo;
class Image {
// 属性
public $error;
public $rule = 'md5';
public $quality = 90;
public $imageMimes = array();
public $thumbs = array();
public $infoDst = array();
public $infoSrc = array(
'width' => 0,
'height' => 0,
'name' => '',
'ext' => '',
'mime' => '',
'path' => '',
);
protected static $instance;
private $imageMimesThis = array(
'gif' => array(
'image/gif',
),
'jpg' => array(
'image/jpeg',
'image/pjpeg'
),
'jpeg' => array(
'image/jpeg',
'image/pjpeg'
),
'jpe' => array(
'image/jpeg',
'image/pjpeg'
),
'png' => array(
'image/png',
'image/x-png'
),
'bmp' => array(
'image/bmp',
'image/ms-bmp',
'image/x-bmp',
'image/x-bitmap',
'image/x-xbitmap',
'image/x-win-bitmap',
'image/x-windows-bmp',
'image/x-ms-bmp',
'application/bmp',
'application/x-bmp',
'application/x-win-bitmap'
),
);
private $res_imgSrc;
private $res_imgDst;
private $obj_file;
private $imageExts;
// 方法
public static instance( [ array $imageMimes ] ) : object
public config( array $imageMimes )
public open( string $path ) : mixed
public stamp( string $stamp [, array $font [, mixed $size = false [, mixed $posi = false [, int $angle = 0 [, int $pct = 100 ]]]]] ) : object
public crop( int $width, int $height [, int $x_src = 0 [, int $y_src = 0 [, mixed $width_src = false [, mixed $height_src = false ]]]] ) : object
public thumb( [ int $width = 100 [, int $height = 100 [, string $type = 'ratio' ]]] ) : object
public output( [ mixed $path = null [, mixed $mime = false [, mixed $quality = false [, int $interlace = 1 ]]]] ) : bool
public save( [ mixed $dir = false [, mixed $name = false [, mixed $quality = false [, int $interlace = 1 ]]]] ) : bool
public batThumb( array $thumbRows ) : bool
public getThumbs() : array
public getMime( string $path [, bool $strict = false ] ) : string
public getExt( string $path [, mixed $mime = false ] ) : string
public getInfo( [ string $name ] ) : mixed
public getError() : string
public rule( string $rule )
protected __construct( [ array $imageMimes ] ) : object
protected __clone()
private openProcess( string $path ) : mixed
private createImgBg( int $width, int $height [, mixed $mime = false [, bool $transparent = true [, bool $savealpha = true ]]] ) : mixed
private verifyFile( string $ext [, string $mime ] ) : bool
private imgSizeProcess( string $path ) : array
private txtStampInit( array $font, string $string ) : array
private stampSizeProcess( array $size, array $stamp ) : array
private stampPosiProcess( array $posi, resouce $imgDst ) : array
private colorProcess( mixed $color ) : array
private rgbProcess( array $color ) : array
private genFilename( [ bool $name = true ] ) : string
private dstProcess( string $path [, string $width = '' [, string $height = '' [, string $type = '' ]]] ) : array
private thumbSizeProcess( int $width_dst, int $height_dst [, string $type = 'ratio' ] ) : array
private ptToPx( int $pt ) : int
private fontProcess() : string
}
- | 权限 | 类型 | 描述 |
---|---|---|---|
属性 | - | - | - |
$error |
public | string | 错误 |
$rule |
public | string | 生成文件名规则(函数名) |
$imageMimes |
public | array | 图片 MIME |
$thumbs |
public | array | 缩略图 |
$infoSrc |
public | array | 原始图片信息 |
$infoDst |
public | array | 目的图片信息 |
$instance |
protected | object static | 本类实例 |
$imageMimesThis |
private | array | 默认图片 MIME |
$res_imgSrc |
private | resource | 原始图片资源 |
$res_imgDst |
private | resource | 目的图片资源 |
$obj_file |
private | object | 文件对象 |
$imageExts |
private | array | 图片扩展名 |
方法 | - | - | - |
instance() | public | static | 实例化 |
config() | public | 配置 | |
open() | public | 打开一个图片文件 | |
stamp() | public | 水印 | |
crop() | public | 裁切图片 | |
thumb() | public | 生成缩略图 | |
output() | public | 输出图片 | |
save() | public | 保存图片 | |
batThumb() | public | 批量生成缩略图 | |
getThumbs() | public | 获取缩略图 | |
getMime() | public | 获取 MIME 类型 | |
getExt() | public | 获取扩展名 | |
getInfo() | public | 获取文件信息 | |
getError() | public | 获取错误 | |
rule() | public | 设置生成文件名规则 | |
__construct() | protected | 同 instance() | |
__clone() | protected | 克隆,无实际功能,仅供限制为单例模式使用 | |
openProcess() | private | 打开图片处理 | |
createImgBg() | private | 创建图片背景 | |
verifyFile() | private | 验证是否为允许的文件 | |
imgSizeProcess() | private | 图片类型检测处理 | |
txtStampInit() | private | 文字水印初始化 | |
stampSizeProcess() | private | 水印尺寸处理 | |
stampPosiProcess() | private | 水印位置处理 | |
colorProcess() | private | 颜色处理 | |
rgbProcess() | private | RGB 处理 | |
genFilename() | private | 生成文件名 | |
dstProcess() | private | 目的处理 | |
thumbSizeProcess() | private | 缩略图尺寸处理 | |
ptToPx() | private | PT 转 PX | |
fontProcess() | private | 随机获取字体文件 |
$infoSrc
原始图片信息public $infoSrc;
结构
名称 | 类型 | 默认 | 描述 |
---|---|---|---|
name | string | 原始文件名 | |
ext | string | 扩展名 | |
mime | string | MIME | |
width | int | 0 | 宽度 |
height | int | 0 | 高度 |
path | string | 路径 |
instance()
实例化方法public static function instance() : object
参数
返回
config()
配置public function config( array $imageMimes )
参数
imageMimes
图片 MIME返回
open()
打开一个图片文件public function open( string $path ) : mixed
参数
path
路径返回
stamp()
水印public function stamp( string $stamp [, array $font [, mixed $size = false [, mixed $posi = false [, int $angle = 0 [, int $pct = 100 ]]]]] ) : object
参数
stamp
文字或水印图片路径,
当本参数为路径且图片文件不存在,或 font
参数不为空时代表文字水印
font
字体
size
水印尺寸posi
水印位置angle
水印旋转角度,0 - 360 之间pct
水印透明度,0 - 100 之间,0 代表透明,100 代表不透明返回
crop()
裁切图片public function crop( int $width, int $height [, int $x_src = 0 [, int $y_src = 0 [, mixed $width_src = false [, mixed $height_src = false ]]]] ) : object
参数
width
目的宽度height
目的高度x_src
原始图片的 x 点y_src
原始图片的 y 点width_src
源宽度height_src
源高度返回
thumb()
生成缩略图public function thumb( [ int $width = 100 [, int $height = 100 [, string $type = 'ratio' ]]] ) : object
参数
width
目的宽度height
目的高度type
ratio 代表按比例缩小,crop 代表裁切返回
output()
输出图片public function output( [ mixed $path = null [, mixed $mime = false [, mixed $quality = false [, int $interlace = 1 ]]]] ) : bool
参数
path
路径,为 null 是代表直接输出mime
生成图片 MIME 类型,为 false 代表根据源图生成quality
图片质量,0 - 100 之间,仅对 jpg 和 png 有效,数字越大质量越好interlace
jpg 隔行扫描返回
save()
保存图片public function save( [ $path = false [, $name = false [, $type = false [, $quality = 90 [, $interlace = true ]]]]] ) : bool // 0.2.0 前
public function save( [ $path = false [, $name = false [, $quality = 90 [, $interlace = true ]]]] ) : bool // 0.2.0 及以后
参数
path
保存目录
此参数为 false 时,与原图片同目录。
name
文件名
此参数为 false 时,系统会自动生成。
type
保存类型 0.2.0
弃用
如 name
参数指定了扩展名,则系统将按照扩展名类型保存,此参数自动失效。
可能的值
值 | 描述 |
---|---|
false(默认值) | 系原图片相同 |
jpe | JPG 图片 |
jpg | JPG 图片 |
jpeg | JPG 图片 |
pjpeg | JPG 图片 |
gif | GIF 图片 |
png | PNG 图片 |
x-png | PNG 图片 |
bmp | BMP 图片 |
x-ms-bmp | BMP 图片 |
x-windows-bmp | BMP 图片 |
quality
图片质量
仅对 JPG 有效,默认为 90
interlace
是否设置隔行扫描
仅对 JPG 有效,默认为 true
设置隔行扫描的情况下,浏览时是从上到下逐行显示,否则图片是由模糊到清晰整个显示。
返回
batThumb()
批量生成缩略图批量生成缩略图会自动调用
save()
方法。
public function batThumb( array $thumbRows ) : bool
参数
thumbRows
缩略图列表,例如:
$thumbRows = array(
array(
'thumb_width' => 100,
'thumb_height' => 100,
'thumb_type' => 'ratio',
),
array(
'thumb_width' => 150,
'thumb_height' => 200,
'thumb_type' => 'crop',
),
);
返回
getThumbs()
获取缩略图public function getThumbs() : array
参数
返回
getMime()
获取 MIME 类型public function getMime( string $path [, bool $strict = false ] ) : string
参数
path
路径strict
严格严格检测返回
getExt()
获取扩展名public function getExt( string $path [, mixed $mime = false ] ) : string
参数
path
路径mime
MIME 类型返回
getInfo()
获取文件信息public function getInfo( [ string $name ] ) : mixed
参数
name
信息名称返回
getError()
获取错误public function getError() : string
参数
返回
rule()
设置生成文件名规则public function rule( string $rule )
参数
rule
生成文件名规则(函数名)返回
openProcess()
打开图片处理private function openProcess( string $path ) : mixed
参数
path
图片路径返回
createImgBg()
创建图片背景private function createImgBg( int $width, int $height [, mixed $mime = false [, bool $transparent = true [, bool $savealpha = true ]]] ) : mixed
参数
width
目的宽度height
目的高度mime
生成图片 MIME 类型,为 false 代表根据源图生成transparent
是否设为透明背景,仅对 png 与 gif 有效savealpha
是否保留 Alpha 通道,仅对 png 有效返回
verifyFile()
验证是否为允许的文件private function verifyFile( string $ext [, string $mime ] ) : bool
参数
ext
扩展名mime
MIME返回
imgSizeProcess()
图片类型检测处理private function imgSizeProcess( string $path ) : array
参数
path
图片路径返回
txtStampInit()
文字水印初始化private function txtStampInit( array $font, string $string ) : array
参数
font
字体,可以为数字或数组,为数字时代表字号(pt),为数组时结构如下
键名 | 类型 | 描述 |
---|---|---|
size | int | 字号(pt) |
color | mixed | 颜色 |
file | string | 字体文件 |
string
文字内容
返回
stampSizeProcess()
水印尺寸处理private function stampSizeProcess( array $size, array $stamp ) : array
参数
size
水印尺寸,如为空,系统会根据字号或者水印图片自动计算
键名 | 类型 | 描述 |
---|---|---|
width | mixed | 水印宽度 |
height | mixed | 水印高度 |
stamp
水印初始信息,通常此参数由系统根据字号或者水印图片自动计算
键名 | 类型 | 描述 |
---|---|---|
width | mixed | 水印宽度 |
height | mixed | 水印高度 |
返回
stampPosiProcess()
水印位置处理private function stampPosiProcess( array $posi, resouce $imgDst ) : array
参数
posi
水印位置
可以为字符串、数字、或数组
array(10,10)
离上边和左边各 10px、array(-10,-10)
离右边和下边各 10px、array(10,-10)
离上边和右边各 10px、array('x' => 10, 'y' => 10)
离上边和左边各 10pximgDst
目标图片资源
返回
colorProcess()
颜色处理private function colorProcess( mixed $color ) : array
参数
color
颜色值
可以为字符串或数组,以下例子均代表白色
array(255,255,255)
、array(0xFF,0xFF,0xFF)
返回
rgbProcess()
RGB 处理private function rgbProcess( array $color ) : array
参数
color
颜色,必须为数组,以下例子均代表白色
array(255,255,255)
、array(0xFF,0xFF,0xFF)
返回
genFilename()
生成文件名private function genFilename( [ bool $name = true ] ) : string
参数
name
true 时按规则生成文件名,false 时使用原始文件名返回
dstProcess()
目的处理private function dstProcess( string $path [, string $width = '' [, string $height = '' [, string $type = '' ]]] ) : array
参数
path
路径width
宽度height
高度type
ratio 代表按比例缩小,crop 代表裁切返回
thumbSizeProcess()
缩略图尺寸处理private function thumbSizeProcess( int $width_dst, int $height_dst [, string $type = 'ratio' ] ) : array
参数
width_dst
目的宽度height_dst
目的高度type
ratio 代表按比例缩小,crop 代表裁切返回
ptToPx()
PT 转 PXprivate function ptToPx( int $pt ) : int
参数
pt
字号(pt)返回
fontProcess()
随机获取字体文件private function fontProcess() : string
参数
返回