最近写个yii2编辑器扩展用到了官方图片处理扩展,这里顺便说下。
首先安装:composer require –prefer-dist yiisoft/yii2-imagine
prefer-dist参数本地保存缓存,重新添加缓存读取。
扩展目录添加2个新目录
vendor/imagine
vendor/yiisoft/yii2-imagine
使用先引入:
use yii\imagine\Image;
$image=’@webroot/test-image.jpg’;
$quality=75;
//图片水印
Image::watermark($image, ‘@webroot/watermark.png’, [0, 0])->save($image, [‘quality’ => $quality]);
//文字水印
Image::text($image, ‘netpc.com.cn测试’, ‘@vendor/netpc/yii2-ueditor/assets/fonts/Alibaba-PuHuiTi-Heavy.otf’, [0,0], [‘color’ => ‘#000000’, ‘size’ =>16])->save($image, [‘quality’ => $quality]);
//修改尺寸
$image = Image::resize($image, 600, 0)->save($image, [‘quality’ => $quality]);
//裁剪
Image::crop($image, 120 , 120, [0, 0])->save($image, [‘quality’ => $quality]);
//略图
Image::thumbnail($image, 120, 120 ,\Imagine\Image\ManipulatorInterface::THUMBNAIL_INSET)->save($image, [‘quality’ => $quality]);
//旋转
Image::frame($image, 5, ‘666’, 0)->rotate(-8)->save($image, [‘quality’ => $quality]);
其它可以参考我的写的composer require netpc/yii2-ueditor扩展或查看官方源码