Intervention/image 是為 Laravel 定制的圖片處理工具, 它提供了一套易於表達的方式來創建、編輯圖片。
一、環境要求
二、安裝及配置
下載地址:https://packagist.org/packages/intervention/image
使用composer進行安裝:
composer require intervention/image
修改 app/config/app.php
添加 ServiceProvider:
// 將下面代碼添加到 providers 數組中 'providers' => [ // ... Intervention\Image\ImageServiceProvider::class, // ... ], // 將下面代碼添加到 aliases 數組中 'aliases' => [ // ... 'Image' => Intervention\Image\Facades\Image::class, // ... ],
生成 config/image.php
配置文件:
php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravel5"
此擴展包默認使用 PHP 的 GD 庫來進行圖像處理, 但由於 GD 庫對圖像的處理效率要稍遜色於 imagemagick 庫, 因此這里推薦替換為 imagemagick 庫來進行圖像處理.
開始之前, 你得先確定本地已經安裝好 GD 或 Imagick。可以修改配置文件來完成 GD 和 Imagick 庫之間的互相切換.
return [ /* |-------------------------------------------------------------------------- | Image Driver |-------------------------------------------------------------------------- | | Intervention Image supports "GD Library" and "Imagick" to process images | internally. You may choose one of them according to your PHP | configuration. By default PHP's "GD Library" implementation is used. | | Supported: "gd", "imagick" | */ 'driver' => 'imagick' ];
三、基礎用法
//-----------------方式一-------------------- // 指定圖片的大小 $img = Image::make('./image/abc.jpg')->resize(300, 300); // 插入水印:將def.jpg作為水印,水印位置在原圖片的右下角, 距離下邊距 10 像素, 距離右邊距 15 像素 $img->insert('./image/def.jpg', 'bottom-right', 15, 10); // 將處理后的圖片重新保存到其他路徑 $img->save('./image/new_abc.jpg'); //-----------------方式二-------------------- /* 上面的邏輯可以通過鏈式表達式搞定 */ $img = Image::make('./image/abc.jpg') ->resize(300, 300) ->insert('./image/def.jpg', 'bottom-right', 15, 10) ->save('./image/new_abc.jpg');
四、特色功能
除上文介紹的基本用法之外, 此擴展包還支持:
- 圖片上傳功能;
- 圖片緩存功能;
- 圖片過濾功能: 將圖片按照統一規則進行轉換;
- 圖片動態處理: 根據訪問圖片的 URL 參數自動調整圖片大小
官方文檔:http://image.intervention.io/getting_started/introduction
參考:https://laravel-china.org/topics/1903/extension-recommended-interventionimage-image-processing
https://packagist.org/packages/intervention/image