Grafika是一个PHP图像处理库,是基于Imagick和GD,能够用于改变图片大小,剪裁,比较,添加水印等等功能。还有感知哈希,高级图像过滤,绘制贝塞尔曲线等功能,可谓很是强大。php
因为功能太多,因此分红几篇文章写。git
《一、图像基本处理》
《二、图像特效处理模块》
《三、图像属性处理》
《四、图形绘制》github
一、直接下载:segmentfault
Grafika的官网、Github地址php7
二、composer
:composer
composer require kosinix/grafika:dev-master --prefer-dist
Imagick
最好(不强求)>=3.3.0 , ImageMagick
>= 6.5.3下载下来的Grafika
目录基本结构像下面这样:测试
不过composer
下载下来的多一点儿,你只须要使用kosinix/grafika
目录下的东西就行了。字体
咱们在grafika
目录下创建一个index.php
,以后的操做都在这里。动画
grafika
给咱们提供了一个很是好用的autoloader.php
位于src
目录下。ui
在index.php
中引入它,(说明下,如下示例都须要引入这个autoloader.php
文件,咱们默认省略),下面就能够直接开发了。
require_once 'src/autoloader.php';
grafika
经过静态方法createEditor
来建立一个editor
。它包含全部的图片处理方法。
因为,grafika
是基于Imagick
和GD
库,因此使用createEditor
方法会根据当前状况,自动选择所须要的图片处理库。(推荐使用)
use Grafika\Grafika; // Import package $editor = Grafika::createEditor(); // Create the best available editor
固然你也能够直接使用Imagick
类库。
use Grafika\Imagick\Editor; // Import package $editor = new Editor(); // Imagick editor
注意:有些状况可能不支持该类库,你须要使用下面语句检查后使用,(不过你最好直接使用方法1,就没这些事)
use Grafika\Imagick\Editor; // Import package $editor = new Editor(); // Imagick editor if( $editor->isAvailable() ) { // Safety check // Your code here }
你也能够直接使用GD
库,也有些状况可能不支持,记得检查
use Grafika\Gd\Editor; // Import package $editor = new Editor(); // Gd editor if( $editor->isAvailable() ) { // Safety check // Your code here }
grafika
容许你使用4种方式建立一个待处理的图像
建立editor
+ open
方法
use Grafika\Grafika; $editor = Grafika::createEditor(); $editor->open( $image, 'path/to/image.jpg');
使用直接打开、建立图片
use Grafika\Grafika; $image = Grafika::createImage('path/to/image.jpg'); // 这里省略了$editor = Grafika::createEditor();
新建一个画布做为新图像
use Grafika\Grafika; $image = Grafika::createBlankImage(100,100);
拷贝一个图像做为图像处理
$copy = clone $image;
这种方法你要保证以前有一张图片
这几种方法以后的操做大同小异,咱们只选择第一种常规方法做为讲解示例
咱们先准备一个原图
接下来,假设咱们要建立的缩略图长:200px宽200px
等比例缩放类型。那么就保证图片较长的一边不超过200px,等比缩放,缩放后不填充背景。
use Grafika\Grafika; $editor = Grafika::createEditor(); $editor->open($image1 , 'yanying.jpg'); // 打开yanying.jpg而且存放到$image1 $editor->resizeFit($image1 , 200 , 200); $editor->save($image1 , 'yanying1.jpg'); $editor->open($image2 , 'yanying-h.jpg'); // 打开yanying.jpg而且存放到$image2 $editor->resizeFit($image2 , 200 , 200); $editor->save($image2 , 'yanying2.jpg');
固然不要忘了第一行的require
固定尺寸缩放类型。就是无论图片长宽比,所有缩小到200px,可能致使图片变形。
use Grafika\Grafika; $editor = Grafika::createEditor(); $editor->open($image1 , 'yanying.jpg'); // 打开yanying.jpg而且存放到$image1 $editor->resizeExact($image1 , 200 , 200); $editor->save($image1 , 'yanying1.jpg'); $editor->open($image2 , 'yanying-h.jpg'); // 打开yanying.jpg而且存放到$image2 $editor->resizeExact($image2 , 200 , 200); $editor->save($image2 , 'yanying2.jpg');
居中剪裁。就是把较短的变缩放到200px,而后将长边的大于200px的部分居中剪裁掉,图片不会变形。
use Grafika\Grafika; $editor = Grafika::createEditor(); $editor->open($image1 , 'yanying.jpg'); // 打开yanying.jpg而且存放到$image1 $editor->resizeFill($image1 , 200,200); $editor->save($image1 , 'yanying1.jpg'); $editor->open($image2 , 'yanying-h.jpg'); // 打开yanying.jpg而且存放到$image2 $editor->resizeFill($image2 , 200,200); $editor->save($image2 , 'yanying2.jpg');
等宽缩放。和第一种功能类似,最终宽为200px,等比缩放,高度无论。
use Grafika\Grafika; $editor = Grafika::createEditor(); $editor->open($image1 , 'yanying.jpg'); // 打开yanying.jpg而且存放到$image1 $editor->resizeExactWidth($image1 , 200); $editor->save($image1 , 'yanying1.jpg'); $editor->open($image2 , 'yanying-h.jpg'); // 打开yanying.jpg而且存放到$image2 $editor->resizeExactWidth($image2 , 200); $editor->save($image2 , 'yanying2.jpg');
等高缩放。最终高为200px,等比缩放,不考虑图片宽度。
咱们首先准备一张基本图,用来和其余图片对比。(segmentfault网页图片可能处理过,直接使用本文图片可能结果不一致)
一、咱们第一次使用一张灰度图片来比较
use Grafika\Grafika; $editor = Grafika::createEditor(); $result = $editor->compare('yanying.jpg' , 'yanying_grey.jpg'); var_dump($result); // int 2
说明: grafika图片对比方法compare
返回一个数字,其中若是数字越接近于0,那么表示图片越类似。若是数字在0-10范围内,那么图片均可能类似。可是若是数字大于10,那么,可能就彻底不一样。
这里返回2,说明类似度仍是很是高的。
二、咱们再用一张缩小的图片来测试,记住都是和第一张基本图比较。
use Grafika\Grafika; $editor = Grafika::createEditor(); $result = $editor->compare('yanying.jpg' , 'yanying-smaller.jpg'); var_dump($result); // int 0
这里结果返回0,类似度很是高。
三、咱们再用一张剪裁下来的局部图片测试
use Grafika\Grafika; $editor = Grafika::createEditor(); $result = $editor->compare('yanying.jpg' , 'yanying-half.jpg'); var_dump($result); // int 20
结果超过10了,类似度不怎么高
四、咱们再用一张彻底不一样的图片测试
use Grafika\Grafika; $editor = Grafika::createEditor(); $result = $editor->compare('yanying.jpg' , 'yanying-h.jpg'); var_dump($result); // int 39
结果39,愈来愈大,愈来愈不像
grafika提供方法equal
来检查两张图片是否彻底相同。这里的检查是一个像素一个像素的检测,因此时间可能会较长。
固然grafika也会预检查,若是两张图片大小不相同,则直接返回false
。只有其余都相同后才会进行逐像素检查。
咱们这里对比以前建立的一张缩略图,由于大小不一致,因此直接返回false
use Grafika\Grafika; $editor = Grafika::createEditor(); $result = $editor->equal('yanying.jpg' , 'yanying-smaller.jpg'); var_dump($result); // boolean false
智能剪裁是自动识别图像中的重要部分,剪裁时候偏向于保留重点部分。
不过grafika也提供了人为操控位置剪裁,咱们先说这个。
基本位置剪裁包含9个位置
咱们这里一块儿说了,这里咱们使用900*600的图片,分红9块
use Grafika\Grafika; $editor = Grafika::createEditor(); $src = 'yanying.jpg'; $editor->open( $image, $src ); $editor->crop( $image, 300, 200, 'top-left' ); $editor->save( $image, 'result1.jpg' ); $editor->free( $image ); $editor->open( $image, $src ); $editor->crop( $image, 300, 200, 'top-center' ); $editor->save( $image, 'result2.jpg' ); $editor->free( $image ); $editor->open( $image, $src ); $editor->crop( $image, 300, 200, 'top-right' ); $editor->save( $image, 'result3.jpg' ); $editor->free( $image ); $editor->open( $image, $src ); $editor->crop( $image, 300, 200, 'center-left' ); $editor->save( $image, 'result4.jpg' ); $editor->free( $image ); $editor->open( $image, $src ); $editor->crop( $image, 300, 200, 'center' ); $editor->save( $image, 'result5.jpg' ); $editor->free( $image ); $editor->open( $image, $src ); $editor->crop( $image, 300, 200, 'center-right' ); $editor->save( $image, 'result6.jpg' ); $editor->free( $image ); $editor->open( $image, $src ); $editor->crop( $image, 300, 200, 'bottom-left' ); $editor->save( $image, 'result7.jpg' ); $editor->free( $image ); $editor->open( $image, $src ); $editor->crop( $image, 300, 200, 'bottom-center' ); $editor->save( $image, 'result8.jpg' ); $editor->free( $image ); $editor->open( $image, $src ); $editor->crop( $image, 300, 200, 'bottom-right' ); $editor->save( $image, 'result9.jpg' ); $editor->free( $image );
看下结果
原图
咱们使用智能剪裁将图片剪裁至200*200px
use Grafika\Grafika; $editor = Grafika::createEditor(); $editor->open( $image, 'yanying-smaller.jpg' ); $editor->crop( $image, 200, 200, 'smart' ); $editor->save( $image, 'yanying-smart.jpg' );
发现仍是能够突出重点的
grafika能够直接压缩GIF图片,而且不丢失动画功能。
use Grafika\Grafika; $editor = Grafika::createEditor(); $editor->open( $image, 'sample.gif' ); $editor->resizeFit( $image, 250, 128 ); $editor->save( $image, 'output.gif' );
咱们这里将原图压缩到原来的一半,发现动画并无丢失
固然,若是有须要,咱们也能够直接移除GIF的动画效果
use Grafika\Grafika; $editor = Grafika::createEditor(); $editor->open( $image, 'sample.gif' ); $editor->flatten( $image ); $editor->save( $image, 'output-no-animation.gif' );
图片合并须要2张图片,将其中一张做为基本图,准备的第二章图片就是放置在基础图片之上。
咱们首先来看代码
use Grafika\Grafika; $editor = Grafika::createEditor(); $editor->open($image1 , 'yanying-h.jpg'); $editor->open($image2 , 'yanying-smaller.jpg'); $editor->blend ( $image1, $image2 , 'normal', 0.9, 'center'); $editor->save($image1,'333/yanying-blend.jpg');
解释一下
首先打开两张图片,其中$image1
为基础图片,也就是放在下面的。重点在blend
这个方法。
其中
normal, multiply, overlay or screen.
,这里的类型意思就是图片叠加的模式,下面会给出实例看每种的不一样。 top-left, top-center, top-right, center-left, center, center-right, bottom-left, bottom-center, bottom-right and smart
咱们试着摆几种状况。
其中位置信息:center,透明度为0.9,也就是上面代码的那种
位置信息:,top-left,其余不变
位置信息:bottom-right,其余不变
位置信息:,最后一个位置参数不给,也就是默认top-left
图像旋转比较简单,只须要给一个旋转角度参数就能够了,若是想要给背景填充个颜色,再给一个颜色参数便可。(默认不给背景色为黑色)
代码以下
use Grafika\Grafika; use Grafika\Color; $editor = Grafika::createEditor(); $editor->open($image , 'yanying-smaller.jpg'); $editor->rotate($image ,'45',new Color('#ff0000')); $editor->save($image,'333/yanying-rotate.jpg');
最后一个背景颜色参数也是须要Color对象
在图片上面写文字的参数比较多,不过若是正常使用,只须要给前两个必填的便可,后面的参数都是可选的。
咱们逐一的来看各个参数
image
:所须要写文字的图片text
:须要写的文字size
:(选填)字体大小,默认为12px
x
:(选填)文字的最左边距离图片最左边的距离,默认为0
y
:(选填)文字的基线到图片的最上边的距离,默认是12px
,也就是文字的高度。(基线你就当作文字最下面好了)color
:(选填)字体颜色,Color
对象,须要new Color
一下,默认为黑色。font
:(选填)字体的完整路径,默认Sans font
.angle
:(选填)文字旋转角度,取值范围为0-359
,默认为0
,也就是不旋转咱们随便找个文字试试
use Grafika\Grafika; use Grafika\Color; $editor = Grafika::createEditor(); $editor->open($image , 'yanying-smaller.jpg'); $editor->text($image ,'yanying',30,200,100,new Color("#000000"),'',45); $editor->save($image,'333/yanying-text.jpg');
看下效果。这里说明下,若是文字为中文,须要找一个支持中文的字体。默认字体不支持中文,因此你写中文,就是都是小方框。
严颖,PHP研发工程师
2016-11-07日晚
推荐一个咱们团队本身开发的针对开发者的网址导航:笔点导航 - 用心作最简洁的网址导航
能够自定义网址
能够自定义分类
分类能够标记颜色
自定义皮肤
自定义搜索
网址拖拽排序
自定义插件小模块