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
轉載:https://www.cnblogs.com/jxl1996/p/10255742.html
intervention包文檔 :http://image.intervention.io/
-----------------------------------------------------------thinkphp5.1------------------------------------------------------------------------------
use Intervention\Image\ImageManagerStatic as Image;
//發布的料生成二維碼
public function make_code($openid)
{
$url = "http://xx.xxxi.club/wxapi/index/make_code_src?openid=".$openid; //二維碼內容
$code_img = $this->scerweima1($url);
//dump($code_img);die; //二維碼鏈接地址
// open an image file
$img = Image::make('http://xxx.xxxi.club/upload/admin/background_img/20200706/0f538a7771bx768f6d77dc28177d741f4.jpg');
// resize image instance
$img->resize(800, 800);
// insert a watermark
$img->insert($code_img,'text-align: center;', 300, 300);
// save image in desired format
$headimgurl = "https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTL8rQJOMuXvlqQUp9FIbmjOsdfTicqP2TCPoRpg6Pll5ejgJ0vloIB7DTz2nggBibviauoia1KN8C3byWA/wrf";
$img->insert($headimgurl,'text-align: center;', 50, 600);
$img->text('小伙子', 250, 650, function($font) {
$font->file('../public/upload/Aa.ttf');
$font->size(35);
$font->color("#ffffff");
$font->align('center');
$font->valign('top');
});
$img->text('¥128', 650, 650, function($font) {
$font->file('../public/upload/Aa.ttf');
$font->size(45);
$font->color("#FBDC6C");
$font->align('center');
$font->valign('top');
});
$img->text('7.6最好一場', 400, 180, function($font) {
$font->file('../public/upload/Aa.ttf');
$font->size(45);
$font->color("#ffffff");
$font->align('center');
$font->valign('top');
});
$img->text('截止01:30', 400, 240, function($font) {
$font->file('../public/upload/Aa.ttf');
$font->size(35);
$font->color("#ffffff");
$font->align('center');
$font->valign('top');
});
// use callback to define details
$img->text('不對返還', 700, 30, function($font) {
$font->file('../public/upload/Aa.ttf');
$font->size(30);
$font->color('#91543F');
$font->align('center');
$font->valign('right');
$font->angle(310);
});
// draw transparent text
$img->text('foo', 0, 0, function($font) {
$font->color(array(255, 255, 255, 0.5));
});
$img->save('../public/'.md5(time()).'.jpg');
}