PHP使用intervention-image實現對圖片的加入文字,圖片等添加編輯功能.

前言:作爲一名全棧開發,和圖像處理打交道是習以爲常,主要是以前端爲主.對於前端來說,圖像處理都還好說,也比較簡單。但對於應用後臺,或者接口而言,畢竟主要工作是處理數據的,圖像處理方面比較少,但是現在後臺處理圖片功能,也會越來越多,以下舉幾個例子:

(1)特定活動海報,(電商app常見)

(2)個人名片(筆者基於此開發功能)

(3)各種各樣的分享

所以今天特向 PHP 工程師們推薦一個 Intervention Image 圖片處理插件,它提供了一套易於表達的方式來創建、編輯圖片。

文檔:

注意點:

由於Phalcon的資料較少,筆者自己提供如何在Phalcon中引用intervention-image

(1)引入包:
   composer require intervention/image
 (2)在app/config/service.php 文件里加入:
   $di->setShared('image-manager', function () { 
        return new \Intervention\Image\ImageManager(['driver' => 'gd']); 
   });

最後,貼上實例代碼,希望對大家有所作用

 Laravel
    Use Image;
    /**
     * 生成保存集團個性名片-後臺例子
     * Intervention/image : http://image.intervention.io/api/text
     * @param int $imageId 圖片ID
     * @param string $path 保存路徑
     */
    protected function saveBusinessCardTemplateExample($imageId, $path)
    {
        $imageManager = new Image();
        $fontPath = public_path('font/HYXIAOBOMEIYANTIW.TTF');

        $image = $imageManager::make(背景url)->resize(330, 190);
        $face_img = $imageManager::make(頭像url)->resize(65, 86);
        $qrcode_img = $imageManager::make(二維碼url)->resize(50, 50);
        $phone_img = $imageManager::make(手機符號url)->resize(10, 10);
        $address_img = $imageManager::make(地址符號url)->resize(10, 10);

        // 插入水印(當前管理頭像), 水印位置在原圖片的右下角, 距離左邊距 18 像素, 距離上邊距 30 像素
        $image->insert($face_img, 'left-top', 18, 30);
        // 插入水印(微信二維碼), 水印位置在原圖片的右下角, 距離右邊距 21 像素, 距離上邊距 120 像素
        $image->insert($qrcode_img, 'right-top', 21,120);
        // 插入水印(手機符號)
        $image->insert($phone_img, 'left-bottom', 100,75);
        // 插入水印(地址符號)
        $image->insert($address_img, 'left-bottom', 100,92);

        // 插入客戶經理名稱
        $image->text('李白', 100,41, function ($font) use ($fontPath) {
            $font->file($fontPath);
            $font->size(13);
            $font->valign('left');
        });
        // 插入客戶經理 職稱
        $image->text('集團專屬客戶經理', 100, 59, function ($font) use ($fontPath) {
            $font->file($fontPath);
            $font->size(12);
            $font->valign('left');
        });
        // 插入客戶經理 職稱
        $image->text('中國移動通信集團附件有限公司廈門分公司', 100, 77, function ($font) use ($fontPath) {
            $font->file($fontPath);
            $font->size(12);
            $font->valign('left');
        });
        // 插入客戶經理 職稱
        $image->text('廈門市蓮前西路233號移動通信大樓', 110, 96, function ($font) use ($fontPath) {
            $font->file($fontPath);
            $font->size(12);
            $font->valign('left');
        });
        // 插入客戶經理手機號
        $image->text('18159801628', 108, 114, function ($font) use ($fontPath)  {
            $font->file($fontPath);
            $font->size(12);
            $font->valign('left');
        });
        // 其他文字
        $image->text('▲掃碼瞭解', 254, 188, function ($font) use ($fontPath)  {
            $font->file($fontPath);
            $font->size(12);
            $font->color('#ffffff');
            $font->align('left');
        });
        // 其他文字
        $image->text('爲企業提供基礎通信、營銷管理等', 18, 170, function ($font) use ($fontPath)  {
            $font->file($fontPath);
            $font->size(12);
            $font->color('#ffffff');
            $font->align('left');
        });
        // 其他文字
        $image->text('一站式解決方案', 55, 185, function ($font) use ($fontPath)  {
            $font->file($fontPath);
            $font->size(12);
            $font->color('#ffffff');
            $font->align('left');
        });
        $image->save($path);
    }
phalcon
    /**
     * 保存個性名片
     * Intervention/image使用api文檔http://image.intervention.io/
     * @param string $path
     */
    protected function saveBusinessCardTemplate2($imageId, $path)
    {
        // 實例化
        $imageManager = $this->getDI()->get('image-manager');
        // 字體文件
        $fontPath = BASE_PATH . '/public/font/HYXIAOBOMEIYANTIW.TTF';

        $image = $imageManager->make(背景圖片url)->resize(330,190);
        $face_img = $imageManager->make(頭像圖片url)->resize(65, 86);
        $qrcode_img = $imageManager->make(二維碼圖片url)->resize(50, 50);
        $phone_img = $imageManager->make(手機自豪圖url)->resize(10, 10);
        $address_img = $imageManager->make(地址符號圖url)->resize(10, 10);

        // 插入水印(當前管理頭像), 水印位置在原圖片的左上角, 距離左邊距18像素, 距離上邊距 30 像素
        $image->insert($face_img, 'left-top', 18, 30);
        // 插入水印(微信二維碼), 水印位置在原圖片的右上角, 距離右邊距 21 像素, 距離上邊距 120 像素

        $image->insert($qrcode_img, 'right-top', 21,120);
        // 插入水印(手機符號)
        $image->insert($phone_img, 'left-bottom', 100,75);
        // 插入水印(地址符號)
        $image->insert($address_img, 'left-bottom', 100,92);

        // 插入客戶經理名稱
        $image->text('李白', 100,41, function ($font) use ($fontPath) {
            $font->file($fontPath);
            $font->size(13);
            $font->valign('left');
        });
        // 插入客戶經理 職稱
        $image->text('集團專屬客戶經理', 100, 59, function ($font) use ($fontPath) {
            $font->file($fontPath);
            $font->size(12);
            $font->valign('left');
        });
        // 插入客戶經理 職稱
        $image->text('中國移動通信集團附件有限公司廈門分公司', 100, 77, function ($font) use ($fontPath) {
            $font->file($fontPath);
            $font->size(12);
            $font->valign('left');
        });
        // 插入客戶經理 職稱
        $image->text('廈門市蓮前西路233號移動通信大樓', 110, 96, function ($font) use ($fontPath) {
            $font->file($fontPath);
            $font->size(12);
            $font->valign('left');
        });
        // 插入客戶經理手機號
        $image->text('18159801628', 108, 114, function ($font) use ($fontPath)  {
            $font->file($fontPath);
            $font->size(12);
            $font->valign('left');
        });
        // 其他文字
        $image->text('▲掃碼瞭解', 254, 188, function ($font) use ($fontPath)  {
            $font->file($fontPath);
            $font->size(12);
            $font->color('#ffffff');
            $font->align('left');
        });
        // 其他文字
        $image->text('爲企業提供基礎通信、營銷管理等', 18, 170, function ($font) use ($fontPath)  {
            $font->file($fontPath);
            $font->size(12);
            $font->color('#ffffff');
            $font->align('left');
        });
        // 其他文字
        $image->text('一站式解決方案', 55, 185, function ($font) use ($fontPath)  {
            $font->file($fontPath);
            $font->size(12);
            $font->color('#ffffff');
            $font->align('left');
        });
        $image->save($path);
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章