0
点赞
收藏
分享

微信扫一扫

php 图片处理 grafika 使用

感谢,推荐看他们的,我的只是保存一份,害怕以后找不到

​​https://learnku.com/articles/39118​​

 

  • 一、 环境需求
  • 二、 安装及配置
  • 三、常用 API 及用法
  • 创建编辑器
  • 创建图像,Grafika 允许使用 4 种方式创建一个待处理的图像
  • 缩放


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-distCopy

 

三、常用 API 及用法​​#​​

 

创建编辑器​​#​​

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

// 导入扩展包
use Grafika\Grafika;

// 创建最佳可用编辑器
$editor = Grafika::createEditor(); Copy

  • ​Imagick Editor()​​​ 当然也可直接使用​​Imagick​​ 类库

use Grafika\Imagick\Editor;

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

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

use Grafika\Imagick\Editor;

$editor = new Editor(); 

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

  • GD Editor 也可直接使用 GD 库,也有些情况可能不支持,注意检查

use Grafika\Gd\Editor;
//  ……
// 创建 GD 编辑器
$editor = new Editor();

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

 

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

  • 编辑器打开

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

  • 使用静态方法打开图片

use Grafika\Grafika;
//  ……
$editor = Grafika::createEditor();
$image = Grafika::createImage('images/foo.jpg'); Copy

  • 创建一个空白的画布

use Grafika\Grafika;
 //  ……
$image = Grafika::createBlankImage(100,100);Copy

  • 从已有图片拷贝一个副本

$copy = clone $image;Copy

 

缩放​​#​​

  • ​resizeFit()​​ 等比例缩放,调整图像大小以适应给定的宽度和高度,重新调整大小的图像不会超过给定的尺寸,如果要保留宽高比,则此选项非常有用

resizeFit ( ImageInterface &$image, int $newWidth, int $newHeight ): EditorInterface Copy

参数

描述

image

基础图片

newWidth

新的宽度

newWidth

新的高度

use Grafika\Grafika;
//  ……
// 打开图像
$editor->open( $image, "images/foo.jpg" );
// 将图像调整大小
$editor->resizeFit( $image, 200, 200 ); 
// 保存
$editor->save( $image, "images/testResizeFit.jpg"); Copy

  • ​resizeExact()​​ 固定尺寸缩放,调整图像大小以精确尺寸,会忽略宽高比。

resizeExact ( ImageInterface &$image, int $newWidth, int $newHeight ): EditorInterface Copy

参数

描述

image

基础图片

newWidth

新的宽度

newWidth

新的高度

use Grafika\Grafika;
//  ……
$editor->open( $image, "images/foo.jpg" );
$editor->resizeExact( $image, 200, 200 );
$editor->save( $image, "images/testResizeExact.jpg");Copy


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


  • ​resizeFill()​​ 缩放裁剪,调整图像大小以填充给定维度中的所有空间,多余的部分被裁切。

resizeFill ( ImageInterface &$image, int $newWidth, int $newHeight ): EditorInterfaceCopy

参数

描述

image

基础图片

newWidth

新的宽度

newWidth

新的高度

use Grafika\Grafika;
//  ……
$editor->open( $image, "images/foo.jpg" );
$editor->resizeFill( $image, 200, 200 );
$editor->save( $image, "images/testResizeFill.jpg");Copy

$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');Copy

  • ​compare()​​ 图片相似度对比,返回值越接近于 0 表示图片越相似,如果数字在 0-10 范围内那么图片都可能相似,果数字大于 10 那么可能就完全不同。

compare(string|ImageInterface $image1, string|ImageInterface $image2): int Copy

参数

描述

image1

图片 1

image2

图片 2

use Grafika\Grafika;
//  ……
$editor = Grafika::createEditor();
$result = $editor->compare('images/foo-gray.png' , 'images/foo.png');Copy

  • ​blend()​​ 图片合并,将两张图像合并,第一张图像作为基础,第二张图像位于基础之上,支持多种混合模式。

blend(ImageInterface &$image1, ImageInterface $image2, string $type = 'normal', float $opacity = 1, string $position = 'top-left', int $offsetX = 0, int $offsetY = 0): EditorInterface Copy

参数

描述

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');Copy

  • ​rotate()​​ 图像旋转

rotate(ImageInterface &$image, int $angle, Color|null $color = null): EditorInterface Copy

参数

描述

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');Copy

  • ​text()​​ 图片写文字

text (ImageInterface &$image, string $text, int $size = 12, int $x = 0, int $y = 12, Color $color = null, string $font = '', int $angle = 0): EditorInterface Copy

参数

描述

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');Copy

$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');Copy

*​​getWidth()​​ 获取图片宽度

getWidth (): int Copy

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


​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');Copy

  • 绘制椭圆

参数

描述

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 ); Copy

  • 绘制矩形

参数

描述

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 );Copy

$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');Copy

  • 绘制多边形

参数

描述

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 );Copy

  • ​blob()​​ 图像二进制输出

blob(string|ImageType $type): void Copy

参数

描述

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'); Copy

  • ​save()​​ 保存

save(ImageInterface $image, string $file, null|string $type = null, null|string $quality = null, bool $interlace = false, int $permission = 0755): EditorInterface Copy

参数

描述

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);Copy

破罐子互摔

举报

相关推荐

0 条评论