赶知识网

Laravel 使用 intervention/image 扩展包,生成带文字的海报

2022-06-05 / 732次点击 php/mysql/apache

文字本身都有哪些属性呢?例如:

文字的字体。

文字的大小。

文字的颜色。

文字相对于图片的有哪些属性呢?例如:

文字的内容。

文字的排版。

文字相对图片的位置。

其次根据上面的介绍来写对应的代码

设置文字的字体:从 可以使用的字体库 下载下来,将文件名修改为英文名字。


// 获取字体 .ttf 文件。 

$fontPath = storage_path('font/shoujinti.TTF');

使用添加文字的方法:文档地址 Image::text




实例化图片对象


$img = Image::make('public/foo.jpg');

调用实例化对象的 text 方法。


// 使用回调函数定义详细内容

$textConent = "五花马千金裘,呼儿将出换美酒!!";

$img->text($textConent, 400, 120, function($font) {

     // 设置字体文件

     $font->file(storage_path('font/shoujinti.ttf'));


     // 设置写入文字的颜色

     $font->color('#FFB400');    // 16进制颜色,字母需大写。

     $font->color([255, 0, 0, 1]); // 也可以 RGB 颜色。


     // 设置字体水平位置 - 设置相对于给定基点的水平文本对齐方式。

     // 有三个值可选,left、center、right,默认是 left

     $font->align('left');        // 用的不多,也不好用


     // 设置字体垂直位置 - 设置相对于给定基点的垂直文本对齐方式。

     // 有三个值可选,top、middle、buttom,默认是 buttom

     $font->valign('buttom');    // 用的不多,也不好用


     // 旋转字体 - 以度为单位设置文本的旋转角度。文本将围绕垂直和水平对齐点逆时针旋转。

     $font->angle(5);

})

最终的代码。


<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

use Illuminate\Support\Facades\Storage;

use Illuminate\Support\Str;

use Intervention\Image\Facades\Image;

class createPoster extends Command

{

 /**

  * The name and signature of the console command.

  *

  * @var string

  */

 protected $signature = 'create:poster';

 /**

  * The console command description.

  *

  * @var string

  */

 protected $description = '创建海报';

 /**

  * Create a new command instance.

  *

  * @return void

  */

 public function __construct()

 {

     parent::__construct();

 }

 /**

  * 处理方法

  *

  * @return void

  */

 public function handle()

 {

     $this->handleMergeImage(storage_path('background.png'), storage_path('qrcode.png'));

     $this->info('执行完毕!');

 }

 /**

  * 合并二维码到背景图片处理

  * @param string $backgroundImg  背景图片链接

  * @param string $qrImg  二维码图片链接

  */

 public function handleMergeImage($backgroundImg = '', $qrImg = '')

 {

     // 图片文件不存在跳出。

     if (!is_file($backgroundImg) || !is_file($qrImg)) {

         return true;

     }

     // 生成随机字符串

     $qrRandomStr = Str::random(11);

     Storage::makeDirectory('qrcode');

     $qrcodeFileName = storage_path('app/public') . '/qrcode/' . $qrRandomStr . '.png';


     $posterRandomStr = Str::random(11);

     Storage::makeDirectory('poster');

     $posterFileName = storage_path('app/public') . '/poster/' . $posterRandomStr . '.png';

     // 1. Image::make($qrImg) 实例化二维码图片生成对象。

     // 2. resize(width, height) 调整二维码宽高, 去适应背景图片空白处大小。

     // 3. save($arImg); 保存二维码到原来路径(相当于覆盖文件), 路径自己配置。

     Image::make($qrImg)->resize(900, 900)->save($qrcodeFileName);

     // 进行图片的拼接

     // 1. Image::make($backgroundImg) 实例化背景图片生成对象。

     // 2. insert($qrImg, 'center', left, top) 将二维码放置背景图片中央 center,距离左边 0 像素,距离上部 450 像素,这些可根据背景图片真实大小是需要调整的。

     // 3. 将拼接后的图片保存到二维码路径(相当于覆盖文件), 当然路径可以自己修改。

     $image = Image::make($backgroundImg)->insert($qrcodeFileName, 'center', 0, 450);


     // 获取字体文件地址,文件地址须使用绝对地址 - 字体文件使用英文命名

     $fontPath = storage_path('thin.ttf');

     $image->text('五花马千金裘,呼儿将出换美酒!', 400, 120, function ($font) use ($fontPath) {

         $font->file($fontPath); // 字体文件地址

         $font->size(60); // 字体大小

         $font->color('#FFB400');

         // $font->color([255, 0, 0, 1]);

         $font->align('left');

         $font->valign('bottom');

         $font->angle(5);

     });

     // 将文件保存

     $image->save($posterFileName);

 }

}


————————————————

原文作者:laravel_peng

转自链接:https://learnku.com/articles/67689

版权声明:著作权归作者所有。商业转载请联系作者获得授权,非商业转载请保留以上作者信息和原文链接。


有用 没用

Top10

沪ICP备09053415号 © 赶知识网