PHP極其強大的圖片處理庫Grafika詳細教程(1):圖像基本處理

Grafika是一個PHP圖像處理庫,是基於Imagick和GD,能夠用於改變圖片大小,剪裁,比較,添加水印等等功能。還有感知哈希,高級圖像過濾,繪製貝塞爾曲線等功能,可謂很是強大。php

因爲功能太多,因此分紅幾篇文章寫。git

《一、圖像基本處理》
《二、圖像特效處理模塊》
《三、圖像屬性處理》
《四、圖形繪製》github

優勢:

  • 縮略圖的速度很是快,質量很是高
  • 支持智能剪裁
  • 很好的支持GIF圖片
  • 5種縮略圖模式
  • 圖像對比功能
  • 圖像高級過濾功能
  • 圖像混合
  • 其餘圖像處理庫支持的API基本都支持

安裝

下載

一、直接下載:segmentfault

Grafika的官網Github地址php7

二、composercomposer

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

環境需求

  • PHP >= 5.3,固然官方推薦php7
  • GD庫 >= 2.0版本
  • Imagick最好(不強求)>=3.3.0 , ImageMagick >= 6.5.3

部署

下載下來的Grafika目錄基本結構像下面這樣:測試

Grafika目錄基本結構

不過composer下載下來的多一點兒,你只須要使用kosinix/grafika目錄下的東西就行了。字體

咱們在grafika目錄下創建一個index.php,以後的操做都在這裏。動畫

grafika給咱們提供了一個很是好用的autoloader.php位於src目錄下。ui

index.php中引入它,(說明下,如下示例都須要引入這個autoloader.php文件,咱們默認省略),下面就能夠直接開發了。

require_once 'src/autoloader.php';

建立Editors

一、createEditor

grafika經過靜態方法createEditor來建立一個editor。它包含全部的圖片處理方法。

因爲,grafika是基於ImagickGD庫,因此使用createEditor方法會根據當前狀況,自動選擇所須要的圖片處理庫。(推薦使用)

use Grafika\Grafika; // Import package
$editor = Grafika::createEditor(); // Create the best available editor

二、Imagick 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 Editor

你也能夠直接使用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

一、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');

固然不要忘了第一行的require

圖片描述圖片描述

二、Resize Exact

固定尺寸縮放類型。就是無論圖片長寬比,所有縮小到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');

圖片描述圖片描述

三、Resize Fill

居中剪裁。就是把較短的變縮放到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');

圖片描述圖片描述

四、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');

圖片描述

圖片描述

五、Resize Exact Height

等高縮放。最終高爲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個位置

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

看下結果

圖片描述

智能剪裁

原圖

圖片描述

咱們使用智能剪裁將圖片剪裁至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' );

發現仍是能夠突出重點的

圖片描述

GIF縮略圖

壓縮GIF,不丟失動畫

grafika能夠直接壓縮GIF圖片,而且不丟失動畫功能。

圖片描述

use Grafika\Grafika;
$editor = Grafika::createEditor();
$editor->open( $image, 'sample.gif' );
$editor->resizeFit( $image, 250, 128 );
$editor->save( $image, 'output.gif' );

咱們這裏將原圖壓縮到原來的一半,發現動畫並無丟失

圖片描述

移除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.,這裏的類型意思就是圖片疊加的模式,下面會給出實例看每種的不一樣。
  • 第三個參數爲透明度,這個不說太多,容易想到。
  • 第四個爲位置,有10個選擇,其中,前面9種爲用戶自定義拜訪位置,而最後一個是智能拜訪,由grafika來判斷擺放在哪裏好。 top-left, top-center, top-right, center-left, center, center-right, bottom-left, bottom-center, bottom-right and smart
  • 第五個參數爲可選參數,表示圖片2距離圖片1左邊的距離
  • 第六個參數也爲可選參數,表示圖片2距離圖片1上邊的距離

咱們試着擺幾種狀況。

一、normal

其中位置信息:center,透明度爲0.9,也就是上面代碼的那種

圖片描述

二、multiply

位置信息:,top-left,其餘不變

圖片描述

三、overlay

位置信息:bottom-right,其餘不變

圖片描述

四、screen

位置信息:,最後一個位置參數不給,也就是默認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日晚

博客:segmentfault主頁

推薦一個咱們團隊本身開發的針對開發者的網址導航:筆點導航 - 用心作最簡潔的網址導航

能夠自定義網址
能夠自定義分類
分類能夠標記顏色
自定義皮膚
自定義搜索
網址拖拽排序
自定義插件小模塊

圖片描述

相關文章
相關標籤/搜索