因为用户端有多个平台,如果做分享海报生成,需要三端都来做,工作量比较大。
所以这个艰巨的任务就光荣的交给后端了。经过一定时间的研究和调试,最终圆满完成了任务,生成分享海报图片实现笔记如下。
目录
准备字体文件
字体文件需与设计图字体保持一致,所以需要下载使用相同的字体文件,否则效果肯定
不符合设计。这里实现使用的是阿里普惠体,下载到项目静态文件路径中,以备调用。
字体文件如下:
准备海报的图片素材
在真实的业务中,海报所需的图片素材来自于活动、商品、用户、小程序码,直接去准备相应的素材即可。在demo中使用了固定的图片素材。
如下:
创建图片
创建图片使用了PHP语言GD库的画图函数。
为方便调用和修改,已经将参数和最终方法进行了封装调用。
具体代码如下:
php
// 字体
const FONT = '/font/AlibabaPuHuiTi-3-55-Regular.ttf';
const FILEPATH = './test.png';
/**
* 获取字体全路径
* @return string
*/
function getFont()
{
return dirname(__FILE__) . FONT;
}
/**
* 生成活动分享海报
*/
function activity_poster()
{
// 使用参数
$param = [
'id' => 1, //活动id
'title' => '活动标题活动标题活动标题', //活动标题
'text' => '活动描述活动描述活动描述活动描述活动描述活动描述活动描述活动描述活动描述活动描述活动描述活动描述', // 活动描述
'member_nick' => '一个高尚的人,一个脱落了低级趣味的人', // 团长昵称
'member_image' => './img/head.png', // 团长头像
'createTime' => date('m/d H:i:s', time()), // 分享时间
'cover' => './img/big.png', // 封面图片
'mini_logo_img' => './img/qr.png', // 小程序码
'price' => 100,
'mini_bg' => './img/min_bg.png'
];
$refer_qrcode_logo = createPoster($param);
if (file_exists($refer_qrcode_logo)) {
echo FILEPATH;die;
} else {
echo '创建失败';
}
}
/**
* 创建海报
* @param $param
* @return string
*/
function createPoster($param)
{
$font = getFont();
// 存储图片路径
$refer_qrcode_logo = FILEPATH;
// 判断是否已生成海报
if (!file_exists($refer_qrcode_logo)) {
// 创建一个白色背景图
$back_width = 530;
$back_height = 1000;
$t_logo2 = imagecreatetruecolor($back_width, $back_height);
$background2 = imagecolorallocate($t_logo2, 255, 255, 255);
imagefill($t_logo2, 0, 0, $background2);
// 定义边距
$left = 30;
$top = 26;
$textcolor = imagecolorallocate($t_logo2, 24, 24, 24);
$textcolor2 = imagecolorallocate($t_logo2, 189, 189, 189);
$textcolor3 = imagecolorallocate($t_logo2, 165, 165, 165);
$textRed = imagecolorallocate($t_logo2, 254, 48, 49);
// 团长头像
$head_width = 64;
$head_height = 64;
$member_image = imagecreatefromstring(file_get_contents($param['member_image']));
$source_info = getimagesize($param['member_image']);
imagecopyresampled($t_logo2, $member_image, $left, $top, 0, 0, $head_width, $head_height, $source_info[0], $source_info[1]);
// 团长昵称
$nick_x = 106;
$nick_y = 56;
// 昵称过长处理
$member_nick = $param['member_nick'];
if (mb_strlen($member_nick) > 12) {
$member_nick = mb_substr($member_nick, 0, 12) . '...';
}
imagefttext($t_logo2, 20, 0, $nick_x, $nick_y, $textcolor, $font, mb_convert_encoding($member_nick, 'html-entities', 'UTF-8'));
// 分享时间
$time_x = 106;
$time_y = 85;
imagefttext($t_logo2, 18, 0, $time_x, $time_y, $textcolor2, $font, mb_convert_encoding($param['createTime'], 'html-entities', 'UTF-8'));
// 封面
$cover_width = 470;
$cover_height = 516;
$cover_y = 110;
$logo = imagecreatefromstring(file_get_contents($param['cover']));
$source_info = getimagesize($param['cover']);
imagecopyresampled($t_logo2, $logo, $left, $cover_y, 0, 0, $cover_width, $cover_height, $source_info[0], $source_info[1]);
// 活动标题
$y = 642;
$line_split = 5;
$goods_name_size = 30;
$title_width = $back_width - ($left * 2);
// 换行处理
$text_array = draw_txt_to(array('fontsize' => $goods_name_size, 'width' => $title_width, 'left' => 0), $param['title']);
foreach ($text_array as $text) {
$y += $goods_name_size + $line_split;
imagefttext($t_logo2, $goods_name_size, 0, $left, $y, $textcolor, $font, mb_convert_encoding($text, 'html-entities', 'UTF-8'));
}
// 活动描述
$y += 20;
$line_split = 5;
$goods_name_size = 18;
$desc_width = $back_width - ($left * 2);
// 换行处理
if ($param['text']) {
$text_array = draw_txt_to(array('fontsize' => $goods_name_size, 'width' => $desc_width, 'left' => 0), $param['text']);
foreach ($text_array as $text) {
$y += $goods_name_size + $line_split;
imagefttext($t_logo2, $goods_name_size, 0, $left, $y, $textcolor3, $font, mb_convert_encoding($text, 'html-entities', 'UTF-8'));
}
}
// 价格
$goods_name_size = 28;
$priceY = $back_height - $goods_name_size - 61 - 40;
$text = '¥' . $param['price'];
imagefttext($t_logo2, $goods_name_size, 0, $left, $priceY, $textRed, $font, mb_convert_encoding($text, 'html-entities', 'UTF-8'));
// 二维码
$logo_size = 200;
$logo_x = 314;
$logo_y = $back_height - $logo_size - 15;
$logo = imagecreatefromstring(file_get_contents($param['mini_logo_img']));
$source_info = getimagesize($param['mini_logo_img']);
imagecopyresampled($t_logo2, $logo, $logo_x, $logo_y, 0, 0, $logo_size, $logo_size, $source_info[0], $source_info[1]);
/*$text = '长按识别小程序 跟团购买';
$goods_name_size = 10;
$left_top = $y + ($logo_size - $goods_name_size * 2);
imagefttext($t_logo2, $goods_name_size, 0, $left, $left_top, $textcolor2, $font, mb_convert_encoding($text, 'html-entities', 'UTF-8'));*/
// 长按识别小程序 跟团购买
$text_x = 274;
$text_y = 46;
$left_top = $back_height - $text_y - 57;
$mini_bg = imagecreatefromstring(file_get_contents($param['mini_bg']));
$source_info = getimagesize($param['mini_bg']);
imagecopyresampled($t_logo2, $mini_bg, $left, $left_top, 0, 0, $text_x, $text_y, $source_info[0], $source_info[1]);
imagepng($t_logo2, $refer_qrcode_logo);
}
return $refer_qrcode_logo;
}
/**
* 分行处理
* @param $pos
* @param $string
* @param $line
* @return array
*/
function draw_txt_to($pos, $string, $line = 2)
{
$font = getFont();
$_string = '';
$__string = array();
for ($i = 0; $i < mb_strlen($string, 'utf-8'); $i++) {
$box = imagettfbbox($pos['fontsize'], 0, $font, $_string);
$_string_length = $box[2] - $box[0];
$box = imagettfbbox($pos['fontsize'], 0, $font, mb_substr($string, $i, 1, 'utf-8'));
if ($_string_length + $box[2] - $box[0] < ($pos['width'] - $pos['left'])) {
$_string .= mb_substr($string, $i, 1, 'utf-8');
} else {
if (count($__string) >= ($line - 1)) {
$_string = mb_substr($_string, 0, mb_strlen($_string, 'utf-8') - 1, 'utf-8') . '...';
break;
}
$pos['left'] = 0;
$__string[] = $_string;
$_string = mb_substr($string, $i, 1, 'utf-8');
}
}
$__string[] = $_string;
return $__string;
}
调用
php
activity_poster();
海报截图
总结
使用GD库函数,首先创建画布,然后根据画布大小定位加载其他内容。
不同效果可参考上方代码来调整实现。