图片处理扩展 Grafika 的简单使用

Grafika是一个PHP图像处理库,是基于Imagick和GD,可以用于改变图片大小,剪裁,比较,添加水印等等功能。还有感知哈希,高级图像过滤,绘制贝塞尔曲线等功能,功能非常强大。优点:缩略图的速度非常快,质量非常高、支持智能剪裁、很好的支持GIF图片、5种缩略图模式、图像对比功能、图像高级过滤功能、图像混合、其他图像处理库支持的API基本都支持

一、 环境需求

  • PHP >= 5.3,当然官方推荐php7
  • GD库 >= 2.0版本
  • Imagick最好(不强求)>=3.3.0 , ImageMagick >= 6.5.3

二、 安装及配置

composer require kosinix/grafika:dev-master --prefer-dist

三、常用 API 及用法

创建编辑器

  • Create Editor 编辑器用于处理图像,官方建议使用 Grafika::createEditor() 创建 ,Grafika 会自动选择可用的最佳编辑器(将检查Imagick是否可用,如果没有就会使用GD)
 // 导入扩展包
use Grafika\Grafika;

// 创建最佳可用编辑器
$editor = Grafika::createEditor(); 
  • Imagick Editor() 当然也可直接使用 Imagick 类库
use Grafika\Imagick\Editor;

// 创建 Imagick 编辑器
$editor = new Editor(); 

注意:在使用 Imagick 编辑器时因为某些PHP安装默认情况下没有Imagick编辑器,需要添加一些安全检查:

use Grafika\Imagick\Editor;

$editor = new Editor(); 

// 安全检查
if( $editor->isAvailable() ) {
     //  ……
}
  • GD Editor 也可直接使用GD库,也有些情况可能不支持,注意检查
use Grafika\Gd\Editor;
//  ……
// 创建 GD 编辑器
$editor = new Editor();

// 安全检查
if( $editor->isAvailable() ) {
   //  ……
}

创建图像,Grafika允许使用4种方式创建一个待处理的图像

  • 编辑器打开
use Grafika\Grafika;
 //  ……
$editor = Grafika::createEditor();
$editor->open( $image, 'images/foo.jpg');
  • 使用静态方法打开图片
use Grafika\Grafika;
//  ……
$editor = Grafika::createEditor();
$image = Grafika::createImage('images/foo.jpg'); 
  • 创建一个空白的画布
use Grafika\Grafika;
 //  ……
$image = Grafika::createBlankImage(100,100);
  • 从已有图片拷贝一个副本
$copy = clone $image;

缩放

  • resizeFit() 等比例缩放,调整图像大小以适应给定的宽度和高度,重新调整大小的图像不会超过给定的尺寸,如果要保留宽高比,则此选项非常有用
resizeFit ( ImageInterface &$image, int $newWidth, int $newHeight ): EditorInterface 
参数 描述
image 基础图片
newWidth 新的宽度
newWidth 新的高度
use Grafika\Grafika;
//  ……
// 打开图像
$editor->open( $image, "images/foo.jpg" );
// 将图像调整大小
$editor->resizeFit( $image, 200, 200 ); 
// 保存
$editor->save( $image, "images/testResizeFit.jpg"); 
  • resizeExact() 固定尺寸缩放,调整图像大小以精确尺寸,会忽略宽高比。
resizeExact ( ImageInterface &$image, int $newWidth, int $newHeight ): EditorInterface 
参数 描述
image 基础图片
newWidth 新的宽度
newWidth 新的高度
use Grafika\Grafika;
//  ……
$editor->open( $image, "images/foo.jpg" );
$editor->resizeExact( $image, 200, 200 );
$editor->save( $image, "images/testResizeExact.jpg");

resizeExactWidth() 等宽缩放,高度自动计算
resizeExactHeight() 等高缩放,宽度自动计算

  • resizeFill() 缩放裁剪,调整图像大小以填充给定维度中的所有空间,多余的部分被裁切。
resizeFill ( ImageInterface &$image, int $newWidth, int $newHeight ): EditorInterface
参数 描述
image 基础图片
newWidth 新的宽度
newWidth 新的高度
use Grafika\Grafika;
//  ……
$editor->open( $image, "images/foo.jpg" );
$editor->resizeFill( $image, 200, 200 );
$editor->save( $image, "images/testResizeFill.jpg");
$editor = Grafika::createEditor();
$editor->open( $image, "images/foo.png" );
$editor->resizeFit($image, 200, 200);  // 等比缩放
//$editor->resizeExact($image, 200, 200);  // 固定尺寸缩放
//$editor->resizeFill($image, 200, 200);  // 缩放裁剪
header('Content-type: image/png');
$image->blob('PNG');
  • compare() 图片相似度对比,返回值越接近于0表示图片越相似,如果数字在0-10范围内那么图片都可能相似,果数字大于10那么可能就完全不同。
compare(string|ImageInterface $image1, string|ImageInterface $image2): int 
参数 描述
image1 图片1
image2 图片2
use Grafika\Grafika;
//  ……
$editor = Grafika::createEditor();
$result = $editor->compare('images/foo-gray.png' , 'images/foo.png');
  • blend()图片合并,将两张图像合并,第一张图像作为基础,第二张图像位于基础之上,支持多种混合模式。
blend(ImageInterface &$image1, ImageInterface $image2, string $type = 'normal', float $opacity = 1, string $position = 'top-left', int $offsetX = 0, int $offsetY = 0): EditorInterface 
参数 描述
image1 基础图片
image2 放置在基础图片之上的图片
type 图片叠加模式,值为:normal、multiply,、overlay 、 screen
opacity 图片2的不透明度,取值范围为0.0到1,默认为1
position 图片2在图片1上的位置,值为:top-left、op-center、top-right、center-left、 center、 center-right、 bottom-left、bottom-center、bottom-right 、 smart,默认为 top-left,smart 表示 grafika 来判断摆放在哪里好
offsetX 图片2距离图片1左边的距离
offsetY 图片2距离图片1上边的距离
use Grafika\Grafika;
//  ……
$editor = Grafika::createEditor();
$editor->open($image1 , 'images/foo-h.jpg');
$editor->open($image2 , 'images/foo.jpg');
$editor->blend ( $image1, $image2 , 'normal', 0.9, 'center');
$editor->save($image1,'images/foo-blend.jpg');
  • rotate()图像旋转
rotate(ImageInterface &$image, int $angle, Color|null $color = null): EditorInterface 
参数 描述
image 基础图片
angle 角度
color 背景色
use Grafika\Grafika;
use Grafika\Color;
 //  ……
$editor = Grafika::createEditor();
$editor->open($image , 'images/foo.png');
$editor->rotate($image ,'45',new Color('#ff0000'));
header('Content-type: image/png');
$image->blob('PNG');
  • text() 图片写文字
text (ImageInterface &$image, string $text, int $size = 12, int $x = 0, int $y = 12, Color $color = null, string $font = '', int $angle = 0): EditorInterface 
参数 描述
image 图片
text 文字
size 字体大小(可选),默认为12px
x 从图像左边缘到文本左边缘的距离(可选),默认为0
y 从图像上边缘到文本基线的距离(可选),默认值为12(等于字体大小),以便将文本放置在图像中
color 字体颜色(可选),默认为黑色
font 字体路径(可选)
angle 文字旋转角度(可选),取值范围为0-359,默认为0
use Grafika\Grafika;
use Grafika\Color;
//  ……
$editor = Grafika::createEditor();
$editor->open($image , 'images/foo.jpg');
$editor->text($image ,'foo',30,200,100,new Color("#000000"),'',45);
$editor->save($image,'images/foo-text.jpg');
$editor = Grafika::createEditor();
$editor->open($image , 'images/foo.png');
$editor->text($image, '测试文字', 24, 50, 50, new Color("#000000"), 'images/Alibaba-PuHuiTi-Light.ttf',90);
header('Content-type: image/png');
$image->blob('PNG');

*getWidth() 获取图片宽度

getWidth (): int 
use Grafika\Grafika;
//  ……
$editor = Grafika::createEditor();
$editor->open( $image, 'images/foo.jpg' );
$image->getWidth();

getHeight() 获取高度 、getImageFile() 获取名称 getType()获取类型isAnimated是否是动图

  • 绘制直线
参数 描述
point1 起始点的坐标(数组)
point2 结束点的坐标(数组)
thickness 宽度,其中GD库会忽略掉,默认为1
color 颜色,默认为黑色
$editor = Grafika::createEditor();
$editor->open($image , 'images/foo.jpg');
// 创建绘制对象,同时检测可用的编辑器
$drawingObject = Grafika::createDrawingObject('Line', [0, 0], [200, 200], 1, new Color('#FF0000'));
// 在图像上绘制上面创建的绘制对象
$editor->draw( $image, $drawingObject ); 
header('Content-type: image/png');
$image->blob('PNG');
  • 绘制椭圆
参数 描述
width 宽度
height 高度
pos 位置[x,y],X 为椭圆最左边距离图像最左边值,Y最上边距离图形最上边值
borderSize 边宽度,0表示无边框,默认为1
borderColor 边颜色,NULL 表示无边框颜色,默认为黑色
fillColor 填充颜色,NULL 表示无填充颜色,默认为白色
// ……
$drawingObject = Grafika::createDrawingObject('Ellipse', 100, 50, [50, 75], 1, new Color('#000000'), new Color('#FF0000'));
$editor->draw( $image, $drawingObject ); 
  • 绘制矩形
参数 描述
width 宽度
height 高度
pos 位置[x,y],X 为矩形最左边距离图像最左边值,Y 为矩形最上边距离图像最上边值,默认为 [0,0]
borderSize 边宽度,0表示无边框,默认为1
borderColor 边颜色,NULL 表示无边框颜色,默认为黑色
fillColor 填充颜色,NULL 表示无填充颜色,默认为白色
// 没有边框的85x50绿色矩形
// ……
$drawingObject = Grafika::createDrawingObject('Rectangle', 85, 50,[105, 70], 0, null, new Color('#00FF00'));
$editor->draw( $image,drawingObject );
$editor = Grafika::createEditor();
$editor->open($image , 'images/foo.png');
// 绘制直线
$drawingObject = Grafika::createDrawingObject('Line', [5, 145], [190, 145], 1, new Color('#FF0000'));
// 绘制椭圆
//$drawingObject = Grafika::createDrawingObject('Ellipse', 180, 70, [10, 10], 1, new Color('#000000'), new Color('#FF0000'));
// 绘制矩形
//$drawingObject = Grafika::createDrawingObject('Rectangle', 180, 70,[10, 10], 0, null, new Color('#FF0000'));
$editor->draw($image, $drawingObject);
header('Content-type: image/png');
$image->blob('PNG');
  • 绘制多边形
参数 描述
points 多边形角的坐标 [[0, 0], [50, 0], [0, 50]],至少有三个
borderSize 边宽度,0表示无边框,默认为1
borderColor 边颜色,NULL 表示无边框颜色,默认为黑色
fillColor 填充颜色,NULL 表示无填充颜色,默认为白色
$drawingObject = Grafika::createDrawingObject('Polygon', [[100, 0], [140, 50], [100, 100], [60, 50]], 1, NULL, new Color('#FF0000'))
$editor->draw( $image,drawingObject );
  • blob() 图像二进制输出
blob(string|ImageType $type): void 
参数 描述
type 图片格式 GIF、JPEG、PNG、WBMP
use Grafika\Grafika;
//  ……
$editor = Grafika::createEditor();
$editor->open( $image, 'images/foo.jpg' ); 
header('Content-type: image/png'); // 设置浏览器输出的类型是图片
$image->blob('PNG'); 
  • save() 保存
save(ImageInterface $image, string $file, null|string $type = null, null|string $quality = null, bool $interlace = false, int $permission = 0755): EditorInterface 
参数 描述
image 图片
file 保存路径
type 图像格式,可以为空,gif、png或jpeg,如果为空,将根据$file中的输出文件名选择适当的格式
quality 图像质量,仅适用于JPEG,范围为数字0-100,其中0是最低的,100是最高的质量,默认为空,如果空值为75,则为默认质量
interlace 设置为渐进式JPEG,仅适用于JPEG,默认为false
permission 目录不存在时创建目录的默认权限,默认值为0755
$editor->open($image, 'images/foo.png');
$editor->save($image, 'images/output.png', null, null, false, 0777);
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 1

文字如何加粗呢

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!