推荐一个 PHP 图像处理操做插件 Intervention Image

不管 Web 前端,仍是 APP 开发,都避免不了和图像处理打交道,对于前端来讲,图像处理都还好说,也比较简单。php

但对于应用后台,或者接口而言,毕竟主要工做是处理数据的,图像处理方面比较少,可是如今后台处理图片功能,也会愈来愈多,如在公众号,要实现特定海报生成功能,这时候就须要将粉丝用户的头像和昵称内嵌到固定的图片上,制做成海报,分享朋友圈,起到宣传做用。前端

因此今天特向 PHP 工程师们推荐一个 Intervention Image 图片处理插件。laravel

Intervention Image

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

摘自官网 http://image.intervention.io/数组

安装 Intervention Image

本文结合 Laravel 项目介绍 Intervention Image 基本使用,因此使用 composer 来安装 Intervention Image 再适合不过了,并且 Intervention Image 官网也推荐使用 composer 来安装。bash

composer require intervention/image

// 或者

php composer.phar require intervention/image

如何安装 Composer,能够看看我以前的文章网络

https://d.laravel-china.org/docs/5.5/installation

Laravel 配置

在 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 说明:

http://image.intervention.io/

总结

如今各类电商、内容应用平台,各类公众号宣传等都会用到「海报」,如何在后台接口制做各类个性化标签的嵌入式海报,应该是个刚需。因此本文推荐使用 Intervention Image 插件。

推荐阅读

  1. 推荐一个 PHP 网络请求插件 Guzzle https://mp.weixin.qq.com/s/w2I8hUmHu0UgjgbSMPEKpg

  2. 推荐一个 Laravel admin 后台管理插件 https://mp.weixin.qq.com/s/PnAj0j2X3-lq3Mn06qjIdQ

「完」


coding01 期待您继续关注

qrcode


也很感谢您能看到这了

qrcode

相关文章
相关标签/搜索