Dai Chong's blog

简介

 近年小程序、微信发展迅速。微信、微博病毒式传播使用户从传统的APP上向即扫即用、无需安装的小程序上转化。而微信公众号、小程序的迅速发展离不开转发、二维码分享。

 但是二维码形式单一,对用户来说毫无吸引力。纯粹依靠‘吆喝、金钱、礼物吸引’方式已经变得愈来愈无价值。

 对于’视觉动物’和大部分用户来说,精美的图片加上二维码的组合更具有吸引力。

php生成海报图前提条件

  • gd库扩展

上代码

生成方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// 生成方法
private function createPoster($data)
{
$poster = new Poster();
//创建画布
$im = $poster->createTrueColor(672, 1060);
//字体文件
$font_file = $this->typeface;
// 商品图片
$goodsImg = $poster->createImgBoard($im, $data['goodsPath'], 0, 0, 672, 668);
//二维码
$codeImg = $poster->createImgBoard($im, $data['qrcode'], 410, 710, 225, 225);
//headimg
$headImg = $poster->createImgBoard($im, $data['headimg'], 46, 942, 65, 65);
//btnimg
$btnimg = $poster->createImgBoard($im, $data['btn'], 420, 960, 220, 48);

// 价格
$price_font = ImageColorAllocate($im, 248, 6, 74);
imagettftext($im, 25, 0, 41, 759, $price_font, $font_file, '¥');
imagettftext($im, 38, 0, 60, 760, $price_font, $font_file, $data["price"]);

// 商品名称
$goodsName = trim(mb_substr($data['title'], 0, 20));
$good_font = ImageColorAllocate($im, 1, 1, 1);
imagettftext($im, 24, 0, 40, 820, $good_font, $font_file, $poster->AutoWrap(12, 0, $font_file, $goodsName, 160));

// 昵称
$nickname = trim(mb_substr($data['nickname'], 0, 8));
$nick_font = ImageColorAllocate($im, 109, 114, 120);
imagettftext($im, 16, 0, 130, 965, $nick_font, $font_file, $poster->AutoWrap(12, 0, $font_file, $nickname, 80));
imagettftext($im, 16, 0, 130, 1000, $nick_font, $font_file, $data['time']);
ob_clean();
ob_end_clean();
// 输出图片
$poster->lookImg($im);

//释放空间
imagedestroy($im);
imagedestroy($btnimg);
imagedestroy($goodsImg);
imagedestroy($codeImg);
imagedestroy($headImg);
// 删除本地二维码
Qrcode::deleteCode($data['qrcode']);
}
实现类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// 具体类
<?php
namespace app\library;

/**
* 生成海报图
* @author daichongweb
* @url daichongweb.cn,daichongweb.com
*/
class Poster
{
/**
* 从图片文件创建Image资源
* @param $file 图片文件,支持url
* @return bool|resource 成功返回图片image资源,失败返回false
*/
public function createImageFromFile($file)
{
if (preg_match('/http(s)?:\/\//', $file)) {
$fileSuffix = self::getNetworkImgType($file);
} else {
$file_info = getimagesize($file);
$fileSuffix = $file_info[2];
// $fileSuffix = pathinfo($file, PATHINFO_EXTENSION);
}

if (!$fileSuffix) return false;

switch ($fileSuffix) {
/*case 'jpeg':
$theImage = @imagecreatefromjpeg($file);
break;*/
case '2':
$theImage = imagecreatefromjpeg($file);
break;
case '3':
$theImage = @imagecreatefrompng($file);
break;
case '1':
$theImage = @imagecreatefromgif($file);
break;
default:
$theImage = @imagecreatefromstring(file_get_contents($file));
break;
}

return $theImage;
}

// 文字换行
public function AutoWrap($fontsize, $angle, $fontface, $string, $width)
{
$content = "";
for ($i = 0; $i < mb_strlen($string); $i++) {
$letter[] = mb_substr($string, $i, 1);
}

foreach ($letter as $l) {
$teststr = $content . "" . $l;
$testbox = imagettfbbox($fontsize, $angle, $fontface, $teststr);
if (($testbox[2] > $width) && ($content !== "")) {
$content .= "\n";
}
$content .= $l;
}
$content = mb_convert_encoding($content, "html-entities", "utf-8");
return $content;
}

public function getNetworkImgType($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_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);
curl_exec($ch);
$http_code = curl_getinfo($ch);
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 int $width
* @param int $height
* @param int $r
* @param int $g
* @param int $b
* @return false|resource
*/
public function createTrueColor(int $width, int $height, $r = 255, $g = 255, $b = 255)
{
$im = imagecreatetruecolor($width, $height);
$color = imagecolorallocate($im, $r, $g, $b);
imagefill($im, 0, 0, $color);
return $im;
}

/**
* 创建图像画布
* @param $im
* @param $imgUrl
* @param $dst_w
* @param $dst_h
* @return bool|resource
*/
public function createImgBoard($im, $imgUrl, $dst_x, $dst_y, $dst_w, $dst_h)
{
list($width, $height) = getimagesize($imgUrl);
$imgBoard = Poster::createImageFromFile($imgUrl);
imagecopyresized($im, $imgBoard, $dst_x, $dst_y, 0, 0, $dst_w, $dst_h, $width, $height);
return $imgBoard;
}

/**
* @param $im
*/
public function lookImg($im)
{
header("Content-type:text/html;charset=utf-8");
Header("Content-Type: image/jpeg"); // jpeg 生成会更快,png生成速度慢但是高清
imagejpeg($im);
}
}
使用方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 获取微信二维码
$codePath = Qrcode::createQrcode([
'scene' => $scene,
'page' => $path,
'width' => '112',
'auto_color' => false,
'line_color' => [
'r' => 0,
'g' => 0,
'b' => 0
]
]);
$data = [
'bg' => $this->bg,
'goodsPath' => $goodsPath,
'title' => $goodsName,
'price' => $goodsPrice,
'btn' => $this->btn,
'time' => date('Y-m-d H:i'),
'qrcode' => $codePath,
'headimg' => $userInfo->headimgurl ? $userInfo->headimgurl : $this->defaultHeadImg,
'nickname' => $userInfo->nickname ? $userInfo->nickname : '特抱抱'
];
$this->createPoster($data);

效果图



 从上面两个图很明显可以看出,第二张图下面被切掉了,而且电脑是打不开的。解决这个问题的关键在于ob_clean();ob_end_clean();两个函数。

解决方法

1
2
3
// 清空缓冲区
ob_clean();
ob_end_clean();

 评论