php 圖片處理 grafika 使用


感謝,推薦看他們的,我的只是保存一份,害怕以后找不到

https://learnku.com/articles/39118

https://blog.csdn.net/qq_34657922/article/details/79479892

 

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

三、常用 API 及用法#

 

創建編輯器#

  • Create Editor 編輯器用於處理圖像,官方建議使用 Grafika::createEditor() 創建 ,Grafika 會自動選擇可用的最佳編輯器(將檢查 Imagick 是否可用,如果沒有就會使用 GD)
 // 導入擴展包
use Grafika\Grafika;

// 創建最佳可用編輯器
$editor = Grafika::createEditor(); 
  • Imagick Editor() 當然也可直接使用 Imagick 類庫
use Grafika\Imagick\Editor;

// 創建 Imagick 編輯器
$editor = new Editor(); 

注意:在使用 Imagick 編輯器時因為某些 PHP 安裝默認情況下沒有 Imagick 編輯器,需要添加一些安全檢查:

use Grafika\Imagick\Editor;

$editor = new Editor(); 

// 安全檢查
if( $editor->isAvailable() ) {
     // ……
}
  • GD Editor 也可直接使用 GD 庫,也有些情況可能不支持,注意檢查
use Grafika\Gd\Editor;
// ……
// 創建 GD 編輯器
$editor = new Editor();

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

創建圖像,Grafika 允許使用 4 種方式創建一個待處理的圖像#

  • 編輯器打開
use Grafika\Grafika;
 // ……
$editor = Grafika::createEditor();
$editor->open( $image, 'images/foo.jpg');
  • 使用靜態方法打開圖片
use Grafika\Grafika;
// ……
$editor = Grafika::createEditor();
$image = Grafika::createImage('images/foo.jpg'); 
  • 創建一個空白的畫布
use Grafika\Grafika;
 // ……
$image = Grafika::createBlankImage(100,100);
  • 從已有圖片拷貝一個副本
$copy = clone $image;
 

縮放#

  • resizeFit() 等比例縮放,調整圖像大小以適應給定的寬度和高度,重新調整大小的圖像不會超過給定的尺寸,如果要保留寬高比,則此選項非常有用
resizeFit ( ImageInterface &$image, int $newWidth, int $newHeight ): EditorInterface 
參數 描述
image 基礎圖片
newWidth 新的寬度
newWidth 新的高度
use Grafika\Grafika;
// ……
// 打開圖像
$editor->open( $image, "images/foo.jpg" );
// 將圖像調整大小
$editor->resizeFit( $image, 200, 200 ); 
// 保存
$editor->save( $image, "images/testResizeFit.jpg"); 
  • resizeExact() 固定尺寸縮放,調整圖像大小以精確尺寸,會忽略寬高比。
resizeExact ( ImageInterface &$image, int $newWidth, int $newHeight ): EditorInterface 
參數 描述
image 基礎圖片
newWidth 新的寬度
newWidth 新的高度
use Grafika\Grafika;
// ……
$editor->open( $image, "images/foo.jpg" );
$editor->resizeExact( $image, 200, 200 );
$editor->save( $image, "images/testResizeExact.jpg");

resizeExactWidth() 等寬縮放,高度自動計算
resizeExactHeight() 等高縮放,寬度自動計算

  • resizeFill() 縮放裁剪,調整圖像大小以填充給定維度中的所有空間,多余的部分被裁切。
resizeFill ( ImageInterface &$image, int $newWidth, int $newHeight ): EditorInterface
參數 描述
image 基礎圖片
newWidth 新的寬度
newWidth 新的高度
use Grafika\Grafika;
// ……
$editor->open( $image, "images/foo.jpg" );
$editor->resizeFill( $image, 200, 200 );
$editor->save( $image, "images/testResizeFill.jpg");
$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');
  • compare() 圖片相似度對比,返回值越接近於 0 表示圖片越相似,如果數字在 0-10 范圍內那么圖片都可能相似,果數字大於 10 那么可能就完全不同。
compare(string|ImageInterface $image1, string|ImageInterface $image2): int 
參數 描述
image1 圖片 1
image2 圖片 2
use Grafika\Grafika;
// ……
$editor = Grafika::createEditor();
$result = $editor->compare('images/foo-gray.png' , 'images/foo.png');
  • blend() 圖片合並,將兩張圖像合並,第一張圖像作為基礎,第二張圖像位於基礎之上,支持多種混合模式。
blend(ImageInterface &$image1, ImageInterface $image2, string $type = 'normal', float $opacity = 1, string $position = 'top-left', int $offsetX = 0, int $offsetY = 0): EditorInterface 
參數 描述
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');
  • rotate() 圖像旋轉
rotate(ImageInterface &$image, int $angle, Color|null $color = null): EditorInterface 
參數 描述
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');
  • text() 圖片寫文字
text (ImageInterface &$image, string $text, int $size = 12, int $x = 0, int $y = 12, Color $color = null, string $font = '', int $angle = 0): EditorInterface 
參數 描述
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');
$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');

*getWidth() 獲取圖片寬度

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

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');
  • 繪制橢圓
參數 描述
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 ); 
  • 繪制矩形
參數 描述
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 );
$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');
  • 繪制多邊形
參數 描述
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 );
  • blob() 圖像二進制輸出
blob(string|ImageType $type): void 
參數 描述
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'); 
  • save() 保存
save(ImageInterface $image, string $file, null|string $type = null, null|string $quality = null, bool $interlace = false, int $permission = 0755): EditorInterface 
參數 描述
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);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM