PHP 图像处理操作插件 Intervention Image

安装 Intervention Image

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

官方文档
http://image.intervention.io/api/text

composer require intervention/image

// 或者

php composer.phar require intervention/image复制代码

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;
    }复制代码

最终的成品如下:

 

https://blog.csdn.net/beyond__devil/article/details/62230610

https://learnku.com/laravel/t/30978

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值