PHP使用Laravel生成荣誉证书和往图片上写字

先来看看效果图:

背景图(生成前):

PHP使用Laravel生成荣誉证书和往图片上写字_第1张图片

 

生成后:

PHP使用Laravel生成荣誉证书和往图片上写字_第2张图片

软件使用插件Intervention/image,这个是官网

http://image.intervention.io

1.首先,给LARAVEL中安装该插件

composer require intervention/image

2.修改config/app.php文件,就是给文件里面的providers那里和aliases那里分别加入这两句

在$providers 中加入这句

Intervention\Image\ImageServiceProvider::class

在 $aliases 中加入这句.

'Image' => Intervention\Image\Facades\Image::class

 

3.在要使用的控制器顶部,加入

use Image;

4.以下为控制器中的方法,给图片插入文字并显示

public function createCertification(){

        // create Image from file
        $img = Image::make(public_path().'\img\hjzs.png'); //背景图的地址

        // use callback to define details
        $img->text('大美女', 150, 260, function($font) {
            $font->file(public_path().'\font\hwzs.ttf'); //字体的地址,地址错误会报GD库的错
            $font->size(35);
            // $font->color('#fdf6e3');
            // $font->align('center');
            // $font->valign('top');
            // $font->angle(45);
        });

        $img->text('二十二', 290, 320, function($font) {
            $font->file(public_path().'\font\hwzs.ttf');
            $font->size(35);
            // $font->color('#fdf6e3');
            // $font->align('center');
            // $font->valign('top');
            // $font->angle(45);
        });

        $img->text('春天在哪里啊春天在哪里', 500, 375, function($font) {
            $font->file(public_path().'\font\hwzs.ttf');
            $font->size(35);
            // $font->color('#fdf6e3');
            // $font->align('center');
            // $font->valign('top');
            // $font->angle(45);
        });

        $img->text('省', 530, 428, function($font) {
            $font->file(public_path().'\font\hwzs.ttf');
            $font->size(35);
            // $font->color('#fdf6e3');
            // $font->align('center');
            // $font->valign('top');
            // $font->angle(45);
        });

        $img->text('壹', 660, 428, function($font) {
            $font->file(public_path().'\font\hwzs.ttf');
            $font->size(35);
            // $font->color('#fdf6e3');
            // $font->align('center');
            // $font->valign('top');
            // $font->angle(45);
        });


        return $img->response("png");
    }

这样就可以了,注意结尾是response()这个是输出到浏览器,这样会节约我们的空间,如果想保存,可以用save()方法哦

这个插件还有很多有用的方法,具体不多赘述了,大家可以去官网进行查询

 

可能有同学说,那有人的作文题目字多,有的人字少,那怎么让题目居中呢?

这里我研究了一下:位置480顶到左边能放14字,位置700在正中2字,算下来是20位置一个字,我们让初始位置为700,超过两个字每多一个字就往左移动20个像素,代码如下

//位置480顶到左边能放14字,位置700在正中2字,20位置一个字
        $x = 700;
        $title = "春天到底在哪里啊";
        $font_len = mb_strlen($title)>14?14:mb_strlen($title);
        $x = 700-20*($font_len-2);

        $img->text($title, $x, 375, function($font) {
            $font->file(public_path().'\font\hwzs.ttf');
            $font->size(35);
            // $font->color('#fdf6e3');
            // $font->align('center');
            // $font->valign('top');
            // $font->angle(45);
        });

 

你可能感兴趣的:(PHP生成证书,PHP图上写字,PHP生成荣誉证书,Laravel生成图片,Laravel,插件,php)