Intervention/image - 图片添加图片/文字水印
public function generatePoster()
{
// 检查是否需要重新生成
if (!$this->needGenerateNewPoster()) {
return null;
}
// 图片合成
$text = $this->self_sn;
// 字体大小
$font_size = 50;
// 获取文字大小
$font = new Font($text);
$font->file(public_path('fonts/YaHei.ttf'));
$font->size($font_size);
$size = $font->getBoxSize();
// 自适应图片宽高
$width = $size['width'] > 640 ? $size['width'] + 40 : 640;
$height = $width + 100;
// 动态生成画布
$canvas = Image::canvas($width, $height, 'fff');
// 更改小程序码大小
$mini_code = Image::make(storage_path('app/public/' . $this->mini_code));
$mini_code->resize($width - 40, $width - 40);
// 插入小程序码
$canvas->insert($mini_code, 'top-left', 20, 20);
// 插入文字
$canvas->text($text, ($width - $size['width']) / 2, $height - 30, function (Font $font) use ($font_size) {
$font->file(public_path('fonts/YaHei.ttf'));
$font->size($font_size);
});
// 图片保存路径
$path = storage_path('app/public/equipment_poster');
if (!is_dir($path)) {
mkdir($path);
}
$filename = md5($this->id . microtime());
$canvas->save($path . "/{$filename}.png");
$this->mini_code_poster = "equipment_poster/{$filename}.png";
$this->save();
$this->savePosterMd5();
return null;
}
推荐文章: