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日晚
推薦一個咱們團隊本身開發的針對開發者的網址導航:筆點導航 - 用心作最簡潔的網址導航
能夠自定義網址
能夠自定義分類
分類能夠標記顏色
自定義皮膚
自定義搜索
網址拖拽排序
自定義插件小模塊