新聞中心
Grafika是一個(gè)PHP圖像處理庫,是基于Imagick和GD,可以用于改變圖片大小,剪裁,比較,添加水印等等功能。還有感知哈希,高級(jí)圖像過濾,繪制貝塞爾曲線等功能,可謂非常強(qiáng)大。

由于功能太多,所以分成幾篇文章寫。
??《1、圖像基本處理》??
??《2、圖像特效處理模塊》??
??《3、圖像屬性處理》??
??《4、圖形繪制》??
優(yōu)點(diǎn):
- 縮略圖的速度非???,質(zhì)量非常高
- 支持智能剪裁
- 很好的支持GIF圖片
- 5種縮略圖模式
- 圖像對比功能
- 圖像高級(jí)過濾功能
- 圖像混合
- 其他圖像處理庫支持的API基本都支持
安裝
下載
1、直接下載:
??Grafika的官網(wǎng)???、??Github地址??
2、composer:
composer require kosinix/grafika:dev-master --prefer-dist
環(huán)境需求
- PHP >= 5.3,當(dāng)然官方推薦php7
- GD庫 >= 2.0版本
- Imagick***(不強(qiáng)求)>=3.3.0 , ImageMagick >= 6.5.3
部署
下載下來的Grafika目錄基本結(jié)構(gòu)像下面這樣:
???
不過composer下載下來的多一點(diǎn)兒,你只需要使用kosinix/grafika目錄下的東西就好了。
我們在grafika目錄下建立一個(gè)index.php,之后的操作都在這里。
grafika給我們提供了一個(gè)非常好用的autoloader.php位于src目錄下。
在index.php中引入它,(說明下,以下示例都需要引入這個(gè)autoloader.php文件,我們默認(rèn)省略),下面就可以直接開發(fā)了。
require_once 'src/autoloader.php';
創(chuàng)建Editors
1、createEditor
grafika通過靜態(tài)方法createEditor來創(chuàng)建一個(gè)editor。它包含所有的圖片處理方法。
由于,grafika是基于Imagick和GD庫,所以使用createEditor方法會(huì)根據(jù)當(dāng)前情況,自動(dòng)選擇所需要的圖片處理庫。(推薦使用)
use Grafika\Grafika; // Import package $editor = Grafika::createEditor(); // Create the best available editor
2、Imagick Editor
當(dāng)然你也可以直接使用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 } 3、GD Editor
你也可以直接使用GD庫,也有些情況可能不支持,記得檢查
use Grafika\Gd\Editor; // Import package $editor = new Editor(); // Gd editor if( $editor->isAvailable() ) { // Safety check // Your code here } 創(chuàng)建圖像
grafika允許你使用4種方式創(chuàng)建一個(gè)待處理的圖像
1、直接打開圖像
創(chuàng)建editor + open方法
use Grafika\Grafika; $editor = Grafika::createEditor(); $editor->open( $image, 'path/to/image.jpg');
2、使用靜態(tài)方法打開圖片
使用直接打開、創(chuàng)建圖片
use Grafika\Grafika; $image = Grafika::createImage('path/to/image.jpg'); // 這里省略了$editor = Grafika::createEditor(); 3、創(chuàng)建一個(gè)空白的畫布
新建一個(gè)畫布作為新圖像
use Grafika\Grafika; $image = Grafika::createBlankImage(100,100);
4、從已有圖片拷貝一個(gè)
拷貝一個(gè)圖像作為圖像處理
$copy = clone $image;
這種方法你要保證之前有一張圖片
這幾種方法之后的操作大同小異,我們只選擇***種常規(guī)方法作為講解示例
圖片縮略圖
我們先準(zhǔn)備一個(gè)原圖 ????
接下來,假設(shè)我們要?jiǎng)?chuàng)建的縮略圖長:200px寬200px
1、Resize Fit
等比例縮放類型。那么就保證圖片較長的一邊不超過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');
當(dāng)然不要忘了***行的require ?????????
2、Resize Exact
固定尺寸縮放類型。就是不管圖片長寬比,全部縮小到200px,可能導(dǎo)致圖片變形。
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');
????????? 3、Resize Fill
居中剪裁。就是把較短的變縮放到200px,然后將長邊的大于200px的部分居中剪裁掉,圖片不會(huì)變形。 ?????????
4、Resize Exact Width
等寬縮放。和***種功能相似,最終寬為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');
?????? ????5、Resize Exact Height
等高縮放。最終高為200px,等比縮放,不考慮圖片寬度。 ?????? ????
圖像對比功能
1、圖片相似度對比
我們首先準(zhǔn)備一張基本圖,用來和其他圖片對比。(segmentfault網(wǎng)頁圖片可能處理過,直接使用本文圖片可能結(jié)果不一致) ????
1)、我們***次使用一張灰度圖片來比較 ????
use Grafika\Grafika; $editor = Grafika::createEditor(); $result = $editor->compare('yanying.jpg' , 'yanying_grey.jpg'); var_dump($result); // int 2 說明: grafika圖片對比方法compare返回一個(gè)數(shù)字,其中如果數(shù)字越接近于0,那么表示圖片越相似。如果數(shù)字在0-10范圍內(nèi),那么圖片都可能相似。但是如果數(shù)字大于10,那么,可能就完全不同。
這里返回2,說明相似度還是非常高的。
2)、我們再用一張縮小的圖片來測試,記住都是和***張基本圖比較。 ????
use Grafika\Grafika; $editor = Grafika::createEditor(); $result = $editor->compare('yanying.jpg' , 'yanying-smaller.jpg'); var_dump($result); // int 0 這里結(jié)果返回0,相似度非常高。
3)、我們再用一張剪裁下來的局部圖片測試 ????
use Grafika\Grafika; $editor = Grafika::createEditor(); $result = $editor->compare('yanying.jpg' , 'yanying-half.jpg'); var_dump($result); // int 20 結(jié)果超過10了,相似度不怎么高
4)、我們再用一張完全不同的圖片測試 ????
use Grafika\Grafika; $editor = Grafika::createEditor(); $result = $editor->compare('yanying.jpg' , 'yanying-h.jpg'); var_dump($result); // int 39 結(jié)果39,越來越大,越來越不像
2、比較圖片是否相同
grafika提供方法equal來檢查兩張圖片是否完全相同。這里的檢查是一個(gè)像素一個(gè)像素的檢測,所以時(shí)間可能會(huì)較長。
當(dāng)然grafika也會(huì)預(yù)檢查,如果兩張圖片大小不相同,則直接返回false。只有其他都相同后才會(huì)進(jìn)行逐像素檢查。
我們這里對比之前創(chuàng)建的一張縮略圖,因?yàn)榇笮〔灰恢?,所以直接返回false ????
use Grafika\Grafika; $editor = Grafika::createEditor(); $result = $editor->equal('yanying.jpg' , 'yanying-smaller.jpg'); var_dump($result); // boolean false 智能剪裁
智能剪裁是自動(dòng)識(shí)別圖像中的重要部分,剪裁時(shí)候偏向于保留重點(diǎn)部分。
不過grafika也提供了人為操控位置剪裁,我們先說這個(gè)。
基本位置剪裁
基本位置剪裁包含9個(gè)位置
- top-left
- top-center
- top-right
- center-left
- center
- center-right
- bottom-left
- bottom-center
- bottom-right
我們這里一起說了,這里我們使用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 );
看下結(jié)果 ????
智能剪裁
原圖 ????
我們使用智能剪裁將圖片剪裁至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' );
發(fā)現(xiàn)還是可以突出重點(diǎn)的 ????
GIF縮略圖
壓縮GIF,不丟失動(dòng)畫
grafika可以直接壓縮GIF圖片,并且不丟失動(dòng)畫功能。
???
use Grafika\Grafika; $editor = Grafika::createEditor(); $editor->open( $image, 'sample.gif' ); $editor->resizeFit( $image, 250, 128 ); $editor->save( $image, 'output.gif' );
我們這里將原圖壓縮到原來的一半,發(fā)現(xiàn)動(dòng)畫并沒有丟失
???
移除GIF動(dòng)畫效果
當(dāng)然,如果有需要,我們也可以直接移除GIF的動(dòng)畫效果
use Grafika\Grafika; $editor = Grafika::createEditor(); $editor->open( $image, 'sample.gif' ); $editor->flatten( $image ); $editor->save( $image, 'output-no-animation.gif' );
????
圖片合并
圖片合并需要2張圖片,將其中一張作為基本圖,準(zhǔn)備的第二章圖片就是放置在基礎(chǔ)圖片之上。
我們首先來看代碼
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為基礎(chǔ)圖片,也就是放在下面的。重點(diǎn)在blend這個(gè)方法。
其中:
- ***個(gè)參數(shù)為基礎(chǔ)圖片
- 第二個(gè)參數(shù)為放置在基礎(chǔ)圖片之上的圖片normal, multiply, overlay or screen.,這里的類型意思就是圖片疊加的模式,下面會(huì)給出實(shí)例看每種的不同。
- 第三個(gè)參數(shù)為透明度,這個(gè)不說太多,容易想到。
- 第四個(gè)為位置,有10個(gè)選擇,其中,前面9種為用戶自定義拜訪位置,而***一個(gè)是智能拜訪,由grafika來判斷擺放在哪里好。top-left, top-center, top-right, center-left, center, center-right, bottom-left, bottom-center, bottom-right and smart
- 第五個(gè)參數(shù)為可選參數(shù),表示圖片2距離圖片1左邊的距離
- 第六個(gè)參數(shù)也為可選參數(shù),表示圖片2距離圖片1上邊的距離
我們試著擺幾種情況。
1、normal
其中位置信息:center,透明度為0.9,也就是上面代碼的那種 ????
2、multiply
位置信息:,top-left,其他不變 ????
3、overlay
位置信息:bottom-right,其他不變 ????
4、screen
位置信息:,***一個(gè)位置參數(shù)不給,也就是默認(rèn)top-left ????
圖像旋轉(zhuǎn)
圖像旋轉(zhuǎn)比較簡單,只需要給一個(gè)旋轉(zhuǎn)角度參數(shù)就可以了,如果想要給背景填充個(gè)顏色,再給一個(gè)顏色參數(shù)即可。(默認(rèn)不給背景色為黑色)
代碼如下
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'); ***一個(gè)背景顏色參數(shù)也是需要Color對象 ????
圖片寫文字
在圖片上面寫文字的參數(shù)比較多,不過如果正常使用,只需要給前兩個(gè)必填的即可,后面的參數(shù)都是可選的。
我們逐一的來看各個(gè)參數(shù)
- image:所需要寫文字的圖片
- text:需要寫的文字
- size:(選填)字體大小,默認(rèn)為12px
- x:(選填)文字的最左邊距離圖片最左邊的距離,默認(rèn)為0
- y:(選填)文字的基線到圖片的最上邊的距離,默認(rèn)是12px,也就是文字的高度。(基線你就當(dāng)做文字最下面好了)
- color:(選填)字體顏色,Color對象,需要new Color一下,默認(rèn)為黑色。
- font:(選填)字體的完整路徑,默認(rèn)Sans font.
- angle:(選填)文字旋轉(zhuǎn)角度,取值范圍為0-359,默認(rèn)為0,也就是不旋轉(zhuǎn)
我們隨便找個(gè)文字試試
use Grafika\Grafika; use Grafika\Color; $editor = Grafika::createEditor(); $editor->open($image , 'yanying-smaller.jpg'); $editor->text($image ,'yanying',30,200,100,new olor("#000000"),'',45); $editor->save($image,'333/yanying-text.jpg'); 看下效果。這里說明下,如果文字為中文,需要找一個(gè)支持中文的字體。默認(rèn)字體不支持中文,所以你寫中文,就是都是小方框。????
標(biāo)題名稱:PHP圖片處理庫Grafika詳細(xì)教程(1):圖像基本處理
網(wǎng)站網(wǎng)址:http://m.fisionsoft.com.cn/article/dpsdgjs.html


咨詢
建站咨詢
