rutkoski / simplify-thumb
缩略图生成和图像处理,可扩展且具有流畅的接口。
v1.0.0
2016-03-31 19:31 UTC
Requires
- php: >=5.3
- rutkoski/simplify-functions: 1.*
This package is auto-updated.
Last update: 2024-09-29 04:08:41 UTC
README
缩略图生成和图像处理,可扩展且具有流畅的接口。
使用方法
基本示例
$thumb = new \Simplify\Thumb(); $filename = $thumb ->setBaseDir(dirname(__file__)) ->setFilesPath('files/') ->setCachePath('files/cache/') ->load('dummy.jpg') ->resize(150, 150) ->cache() ->getCacheFilename();
该库使用方法的参数(如resize/150/150)来检查是否存在缓存文件,并使用它而不是重新创建图像。
输出
获取缓存文件名
$thumb->getCacheFilename();
或保存到新位置
$thumb->save('path/newfile.php');
或覆盖当前图像
$thumb->save();
或将图像发送到浏览器
$thumb->output();
更改格式
只需使用PHP常量(IMAGETYPE_PNG, IMAGETYPE_JPEG, ...)
$thumb->cache(IMAGETYPE_PNG);
或
$thumb->output(IMAGETYPE_PNG);
可用方法
调整大小
$thumb->resize($width, $height, $mode, $far, $r, $g, $b, $a);
调整大小的第三个参数更改图像调整大小的方式。默认为Simplify_Thumb::FIT_INSIDE
。$mode
的选项有
Simplify_Thumb::FIT_INSIDE
- 将图像调整大小以适应由$width
和$height
定义的框内Simplify_Thumb::FIT_OUTSIDE
- 将图像调整大小以适应由$width
和$height
定义的框外Simplify_Thumb::SCALE_TO_FIT
- 将图像调整大小为正好$width
和$height
Simplify_Thumb::NO_SCALE
- 不调整图像大小,这对于放大图像画布很有用
第四个参数$far
(强制宽高比),当为true时,强制最终图像正好为$width
x $height
像素。$r
、$g
、$b
和$a
参数设置背景颜色,因此,例如
$thumb->resize(150, 150, Simplify_Thumb::FIT_INSIDE, true, 0, 0, 0, 0);
在300 x 100px的图像上,将图像适应到150 x 150px的黑色背景框内。
缩放裁剪
裁剪图像,使其填充指定的尺寸。第三个参数指定使用图像的哪个部分。
$thumb->zoomCrop($width, $height, Simplify_Thumb::CENTER);
PHP图像过滤器
$thumb->brightness($level); $thumb->grayscale(); $thumb->negate(); $thumb->contrast($level); $thumb->colorize($red, $green, $blue, $alpha); $thumb->edgedetect(); $thumb->emboss(); $thumb->gaussianBlur(); $thumb->selectiveBlur(); $thumb->meanRemoval(); $thumb->smooth($level); $thumb->pixelate($blockSize, $advanced);
偏移
缩小/放大图像画布并填充背景颜色。
$thumb->offset($top, $right, $bottom, $left, $r = 0, $g = 0, $b = 0, $a = 0);
$top
、$right
、$bottom
和left
是相对的。正数使画布更大,负数使画布更小(裁剪图像)。
自定义插件
通过扩展Simplify_Thumb_Plugin
并调用实现自定义插件
$thumb->plugin($plugin);
其中$plugin
是表示插件类的字符串。
示例
覆盖当前图像上的图像的插件
class \Simplify\Thumb\Plugin\Overlay extends \Simplify\Thumb\Plugin { protected function process(\Simplify\Thumb\Processor $thumb, $overlayImage = null) { $overlay = \Simplify\Thumb\Functions::load($overlayImage); $w = imagesx($overlay); $h = imagesy($overlay); imagecopyresampled($thumb->image, $overlay, 0, 0, 0, 0, $w, $h, $w, $h); } }
然后
$thumb->plugin('\Simplify\Thumb\Plugin\Overlay', 'overlay.png');