php生成海报图片,方便商品的分享、证书生成等。
效果:

直接上案例:
<?php
/**
* @email tech@alipay168.cn
* @author 小韦
* @link https://www.alipay168.cn
* @Date: 2020/2/20 13:50
*/
class poster
{
//图片质量,0~100,default=75
private static $quality = 100;
//字体路径
//private static $font_path = 'C:/Windows/Fonts/simfang.ttf';
private static $font_path = __DIR__.'/simkai.ttf';
//背景图片
private static $backgroud_img = __DIR__ . "/../imgs/bg.jpg";
//二维码/水印图片
private static $code_img = __DIR__ . "/../imgs/qr.png";
//开始制作
public static function start($data)
{
//输入文件的路径和名称
$output_file = $data['output_file'];
//商品图片
$goods_img = $data['goods_img'];
// 添加二维码
self::addPic(self::$backgroud_img, self::$code_img, 100, 100, 275, 400, $output_file);
//添加文字描述
self::addWord('长按识别', 275, 520, 18, 'grey1', $output_file);
// 添加产品
self::addPic($output_file, $goods_img, 375, 400, 0, 0, $output_file);
// 添加产品描述,对描述进行分行
$theTitle = self::cn_row_substr($data['title'], 2, 11);
self::addWord($theTitle[1], 20, 425, 16, 'black', $output_file);
self::addWord($theTitle[2], 20, 450, 16, 'black', $output_file);
// 文字1
self::addWord('原价' . $data['price_member'], 20, 480, 16, 'black', $output_file);
// 文字2
self::addWord('————', 65, 480, 16, 'red', $output_file,3);
self::addWord('————', 65, 481, 16, 'red', $output_file,3);
self::addWord('————', 65, 482, 16, 'red', $output_file,3);
// 文字3
self::addWord('特价' . $data['price_market'], 20, 510, 18, 'red', $output_file);
return $output_file;
}
/**
* 添加图片
* @param $path_base string 原图
* @param $path_logo string 添加图
* @param $imgWidth int 添加图宽
* @param $imgHeight int 添加图高
* @param $dst_x int 在原图宽x处添加
* @param $dst_y int 在原图高y处添加
* @param $output_file string 生成图
* return resource 返回图片image资源
*/
private static function addPic($path_base, $path_logo, $imgWidth, $imgHeight, $dst_x, $dst_y, $output_file)
{
if (!file_exists($path_logo)) {
echo $path_logo;
exit();
} elseif (!file_exists($path_base)) {
echo $path_base;
exit();
}
$image_base = self::ImgInfo($path_base);
$image_logo = self::ImgInfo($path_logo);
imagecopyresampled($image_base, $image_logo, $dst_x, $dst_y, 0, 0, $imgWidth, $imgHeight, imagesx($image_logo), imagesy($image_logo));
// 生成一个合并后的新图
imagejpeg($image_base, $output_file, self::$quality);
// 载入新图像资源
$new_pic = imagecreatefromjpeg($output_file);
// 生成写入文字的的新图
imagejpeg($new_pic, $output_file, self::$quality);
}
/**
* 从图片文件创建Image资源
* @param $file string 图片文件,支持url
* @return bool|resource 成功返回图片image资源,失败返回false
*/
private static function ImgInfo($img)
{
if (preg_match('/http(s)?:\/\//', $img)) {
$fileSuffix = self::getNetworkImgType($img);
} else {
$fileSuffix = pathinfo($img, PATHINFO_EXTENSION);
}
if (!$fileSuffix) return false;
switch ($fileSuffix) {
case 'jpeg':
$theImage = @imagecreatefromjpeg($img);
break;
case 'jpg':
$theImage = @imagecreatefromjpeg($img);
break;
case 'png':
$theImage = @imagecreatefrompng($img);
break;
case 'gif':
$theImage = @imagecreatefromgif($img);
break;
default:
$theImage = @imagecreatefromstring(file_get_contents($img));
break;
}
return $theImage;
}
/**
* 获取网络图片类型
* @param $url string 网络图片url,支持不带后缀名url
* @return bool
*/
private static function getNetworkImgType($url)
{
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_URL, $url); //设置需要获取的URL
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);//设置超时
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //支持https
curl_exec($ch);//执行curl会话
$http_code = curl_getinfo($ch);//获取curl连接资源句柄信息
curl_close($ch);//关闭资源连接
if ($http_code['http_code'] == 200) {
$theImgType = explode('/', $http_code['content_type']);
if ($theImgType[0] == 'image') {
return $theImgType[1];
} else {
return false;
}
} else {
return false;
}
}
/**
* 分行连续截取字符串
* @param $str string 需要截取的字符串,UTF-8
* @param int $row 截取的行数
* @param int $number 每行截取的字数,中文长度
* @param bool $suffix 最后行是否添加‘...’后缀
* @return array 返回数组共$row个元素,下标1到$row
*/
private static function cn_row_substr($str, $row = 1, $number = 10, $suffix = true)
{
$result = array();
for ($r = 1; $r <= $row; $r++) {
$result[$r] = '';
}
$str = trim($str);
if (!$str) return $result;
$theStrlen = strlen($str);
//每行实际字节长度
$oneRowNum = $number * 3;
for ($r = 1; $r <= $row; $r++) {
if ($r == $row and $theStrlen > $r * $oneRowNum and $suffix) {
$result[$r] = self::mg_cn_substr($str, $oneRowNum - 6, ($r - 1) * $oneRowNum) . '...';
} else {
$result[$r] = self::mg_cn_substr($str, $oneRowNum, ($r - 1) * $oneRowNum);
}
if ($theStrlen < $r * $oneRowNum) break;
}
return $result;
}
/**
* 按字节截取utf-8字符串
* 识别汉字全角符号,全角中文3个字节,半角英文1个字节
* @param $str string 需要切取的字符串
* @param $len int 截取长度[字节]
* @param int $start 截取开始位置,默认0
* @return string
*/
private static function mg_cn_substr($str, $len, $start = 0)
{
$q_str = '';
$q_strlen = ($start + $len) > strlen($str) ? strlen($str) : ($start + $len);
//如果start不为起始位置,若起始位置为乱码就按照UTF-8编码获取新start
if ($start and json_encode(substr($str, $start, 1)) === false) {
for ($a = 0; $a < 3; $a++) {
$new_start = $start + $a;
$m_str = substr($str, $new_start, 3);
if (json_encode($m_str) !== false) {
$start = $new_start;
break;
}
}
}
//切取内容
for ($i = $start; $i < $q_strlen; $i++) {
//ord()函数取得substr()的第一个字符的ASCII码,如果大于0xa0的话则是中文字符
if (ord(substr($str, $i, 1)) > 0xa0) {
$q_str .= substr($str, $i, 3);
$i += 2;
} else {
$q_str .= substr($str, $i, 1);
}
}
return $q_str;
}
/**
* 添加文字
* @param $str string 要添加的文字
* @param $posX int 在宽x处添加
* @param $poxY int 在高y处添加
* @param $font int 字体大小
* @param $color string 字体颜色
* @param $output_file string 生成图
* @param $tilt int 文字倾斜度
* return resource 返回图片image资源
*/
private static function addWord($str, $posX, $poxY, $font, $color, $output_file,$tilt = 0)
{
$ori_img = $output_file; //原图
$new_img = $output_file; //生成水印后的图片
$s_original = self::ImgInfo($ori_img);
$ImgColor = [ //为一幅图像分配颜色
'black' => imagecolorallocate($s_original, 0, 0, 0),
'red' => imagecolorallocate($s_original, 255, 0, 0),
'grey1' => imagecolorallocate($s_original, 31, 31, 18),
];
imagettftext($s_original, $font, $tilt, $posX, $poxY, $ImgColor[$color], self::$font_path, $str);
imagejpeg($s_original, $new_img, self::$quality); //生成新的图片(jpg格式)
}
}
$data = [
'title' => '【VIP培训版】扫码登录/注册/授权思维导图',
'price_market' => '¥0.99',
'price_member' => '¥99.99',
'goods_img' => __DIR__ . '/../imgs/4.jpg',
//'goods_img' => __DIR__.'/../imgs/1581859831334804.png',
'output_file' => 'b.jpg'
];
//$newImg = poster::start($data);
//echo "<img src=" . $newImg . " />";
?>
<!doctype html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>海报详情</title>
<style>
img {
max-width: 100%
}
</style>
</head>
<body>
<div class="img">
<img src="<?php echo poster::start($data); ?>" alt="">
</div>
</body>
</html>
推荐阅读:PHP用gd库分隔图片