不管 Web 前端,仍是 APP 開發,都避免不了和圖像處理打交道,對於前端來講,圖像處理都還好說,也比較簡單。php
但對於應用後臺,或者接口而言,畢竟主要工做是處理數據的,圖像處理方面比較少,可是如今後臺處理圖片功能,也會愈來愈多,如在公衆號,要實現特定海報生成功能,這時候就須要將粉絲用戶的頭像和暱稱內嵌到固定的圖片上,製做成海報,分享朋友圈,起到宣傳做用。前端
因此今天特向 PHP 工程師們推薦一個 Intervention Image 圖片處理插件。laravel
Intervention/image 是爲 Laravel 定製的圖片處理工具, 它提供了一套易於表達的方式來建立、編輯圖片。express
Intervention Image is an open source PHP image handling and manipulation library. It provides an easier and expressive way to create, edit, and compose images and supports currently the two most common image processing libraries GD Library and Imagick.canvas
The class is written to make PHP image manipulating easier and more expressive. No matter if you want to create image thumbnails, watermarks or format large image files Intervention Image helps you to manage every task in an easy way with as little lines of code as possible.小程序
The library follows the FIG standard PSR-2 to ensure a high level of interoperability between shared PHP code and is fully unit-tested.api
本文結合 Laravel 項目介紹 Intervention Image 基本使用,因此使用 composer 來安裝 Intervention Image 再適合不過了,並且 Intervention Image 官網也推薦使用 composer 來安裝。bash
composer require intervention/image // 或者 php composer.phar require intervention/image
如何安裝 Composer,能夠看看我以前的文章網絡
在 config/app.php 配置文件的$providers
數組中加入 provider:
Intervention\Image\ImageServiceProvider::class
在 $aliases
數組中加入對應的 aliase:
'Image' => Intervention\Image\Facades\Image::class
若是須要配置 Image Driver,只須要在配置文件config/image.php
中修改,配置文件只須要運行對應的命令生成便可:
php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravel5"
配置文件以下:
<?php return array( /* |-------------------------------------------------------------------------- | 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' => 'gd' );
若是須要改爲 Imagick 驅動,只須要把 driver
值改了就好。
Route::get('/yemeishu/{value}', function ($value) { $img = Image::make(base_path().'/***/background0.jpeg'); return $img->response('jpeg'); });
這樣,就能夠直接輸出這張圖片,固然咱們能夠在這張圖片上加上一段話:「I like Laravel」,再輸出:600*800大小。
Route::get('/yemeishu/{value}', function ($value) { $img = Image::make(base_path().'/***/background0.jpeg')->resize(600, 800); $img->text('「我喜歡 Laravel」', 120, 220, function ($font) { $font->file(base_path().'/***/font1.ttf'); $font->size(32); $font->valign('bottom'); $font->color('#333333'); }); return $img->response('jpeg'); });
接着咱們再往上面放個「二維碼圖像」,這個二維碼是個 url 連接:
Route::get('/yemeishu/{value}', function ($value) { $img = Image::make(base_path().'/public/***/background0.jpeg')->resize(600, 800); $img->text('「我喜歡 Laravel」', 120, 220, function ($font) { $font->file(base_path().'/***/font1.ttf'); $font->size(32); $font->valign('bottom'); $font->color('#333333'); }); // 獲取遠程圖片 $erweimaimage = Image::make('http://ow20g4tgj.bkt.clouddn.com/2017-11-11-15103969491817.jpg')->resize(200, 200); // 插入到底部,下邊距 50 處 $img->insert($erweimaimage, 'bottom', 0, 50); return $img->response('jpeg'); });
這樣就有種「海報」的感受了吧。
接下來讓咱們進入有點「料」的。在實際生成中,咱們的海報主要由設計師設計出來後,開發人員切圖,而後落實成爲具體的功能實現出來。
直接上代碼:
public function getBookImageMaker($book, $share, $xcxurl) { $background = [ base_path().'/public/***/background0.jpeg', base_path().'/public/***/background1.jpeg', base_path().'/public/***/background2.jpeg' ]; $font_paths = [base_path().'/***/font0.ttf', base_path().'/***/font1.ttf']; $font_path = $font_paths[rand(0, 1)]; $img = Image::make($background[rand(0,2)])->resize(640, 1000); $face_img = Image::make($share['face_img']) ->resize(60, 60); // 頭部加頭像 $img->insert( $face_img, 'top-left', 55, 76 ); // 頭部加暱稱 $img->text($share['nickname'].'爲你推薦', 131, 120, function ($font) use ($font_path) { $font->file($font_path); $font->size(32); $font->valign('bottom'); $font->color('#333333'); }); // 圖書圖片區域 $bodyimage = Image::canvas(533, 475, '#fe7e86'); $goodsimage = Image::make($book['goods_img']) ->resize(531, 309); $bodyimage->insert($goodsimage, 'top-left', 1, 1); $bodybuttomimage = Image::canvas(531, 164, '#fff'); $strings = $this->mbStrSplit($book['name'], 18); $i = 0; //top position of string if (count($strings) == 1) { $bodybuttomimage->text($strings[0], 17, 44, function ($font) use ($font_path) { $font->file($font_path); $font->size(30); $font->valign('top'); $font->color('#333333'); }); } else { foreach($strings as $key => $string) { if ($key == 2) { break; } // 標題部分 $bodybuttomimage->text($string, 17, 16 + $i, function ($font) use ($font_path) { $font->file($font_path); $font->size(27); $font->valign('top'); $font->color('#333333'); }); $i = $i + 43; //shift top postition down 42 } } // 價格 if ($book['orig_price']) { $price = $book['orig_price']; } else { $price = $book['price']; } $bodybuttomimage->text('原價:'.$price, 17, 118, function ($font) use ($font_path) { $font->file($font_path); $font->size(24); $font->valign('top'); $font->color('#a3a3a3'); }); if ($book['group'] && $book['group']['endtime'] > date("Y-m-d H:i:s")) { $xianjiaString = '團購價:'; $xianPrice = $book['group']['price']; $tuanButton = Image::canvas(107, 33, '#ff0000'); $tuanButton->text($book['group']['min_quantity'].'人團', 22, 6, function ($font) use ($font_path) { $font->file($font_path); $font->size(25); $font->align('left'); $font->valign('top'); $font->color('#fff'); }); $bodybuttomimage->insert($tuanButton, 'top-right', 30, 110); } else { $xianjiaString = '現價:'; $xianPrice = $book['price']; } $bodybuttomimage->text($xianjiaString, 180, 118, function ($font) use ($font_path) { $font->file($font_path); $font->size(24); $font->valign('top'); $font->color('#333333'); }); $bodybuttomimage->text('¥'.$xianPrice, 270, 118, function ($font) use ($font_path) { $font->file($font_path); $font->size(27); $font->valign('top'); $font->color('#fe0000'); }); $bodyimage->insert($bodybuttomimage, 'top-left', 1, 310); $img->insert($bodyimage, 'top-left', 55, 154); // 底部二維碼部分 $dibuimage = Image::canvas(596,308); $codeimage = Image::make(base_path().'/public/img/maker/1/codeborder.jpeg')->resize(255, 255); $codesourceimage = Image::make($xcxurl) ->resize(249, 249); $codeimage->insert($codesourceimage, 'top-left', 3, 3); $dibuimage->insert($codeimage, 'top-left', 33, 23); $dibuimage->text('長按識別小程序碼', 300, 110, function ($font) use ($font_path) { $font->file($font_path); $font->size(27); $font->valign('top'); $font->color('#333333'); }); $dibuimage->text('當即搶購!', 370, 150, function ($font) use ($font_path) { $font->file($font_path); $font->size(27); $font->valign('top'); $font->color('#333333'); }); $img->insert($dibuimage, 'top-left', 22, 650); return $img; }
最終的成品以下:
至於上述代碼的函數做用能夠參考官網 api 說明:
如今各類電商、內容應用平臺,各類公衆號宣傳等都會用到「海報」,如何在後臺接口製做各類個性化標籤的嵌入式海報,應該是個剛需。因此本文推薦使用 Intervention Image 插件。
推薦一個 PHP 網絡請求插件 Guzzle https://mp.weixin.qq.com/s/w2I8hUmHu0UgjgbSMPEKpg
推薦一個 Laravel admin 後臺管理插件 https://mp.weixin.qq.com/s/PnAj0j2X3-lq3Mn06qjIdQ
「完」
coding01 期待您繼續關注
也很感謝您能看到這了