我是靠谱客的博主 笨笨菠萝,这篇文章主要介绍小程序合成海报图片,高度自适应,现在分享给大家,希望可以做个参考。

将一篇带有图片的文章合成一张图片,由于无法直接一起添加图片和文字,将图片和文字拆分开,挨个添加到画布上,可根据文字和图片的高度计算出需要创建画布的高度,并生成合适大小的画布 

复制代码
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
/** * 获取要添加到画布的展示内容 * @param $text 内容 * @param int $fontsize 文字大小 * @param int $width 要展示的最大宽度 * @param int $height 要展示的最大高度 * @param int $fontHeight 字体高度 * @param int $eol_height EOL高度 * @return array */ public static function getShowText($text, $fontsize = 24, $width = 650, $height = 800, $fontHeight = 60, $eol_height = 10) { $fontFile = public_path() . '/static/fonts/msyh.ttf'; $content = str_replace("&nbsp;", "", $text); $arr = Util::spiltHtml($content); $h = 0; $contArr = array(); foreach ($arr as $item) { if ($item['type'] == 'text') { $conTxt = self::autoWrap($fontsize, $fontFile, $item['text'], $width); $size = imagettfbbox($fontsize, 0, $fontFile, $conTxt); $strArr = explode(PHP_EOL, $conTxt); foreach ($strArr as $key => $item) { if (empty($item)) { $h += $eol_height; $contArr[] = ['type' => 'text', 'str' => $item, 'lineheight' => $eol_height]; } else { $h += $fontHeight; $contArr[] = ['type' => 'text', 'str' => $item, 'lineheight' => $fontHeight]; } if ($h >= $height) { break 2; } } } else if ($item['type'] == 'img') { try{ $imginfo = getimagesize($item['src']); $maxImgWidth = ceil($width / 2); if ($imginfo[0] > $maxImgWidth) { $srcH = ceil($width * $imginfo[1] / $imginfo[0]); $h += intval(20 + $srcH); $contArr[] = ['type' => 'img', 'str' => $item['src'], 'w' => $width, 'h' => $srcH]; } else { $h += $imginfo[1]; $contArr[] = ['type' => 'img', 'str' => $item['src'], 'w' => $imginfo[0], 'h' => $imginfo[1]]; } if ($h >= $height) { break; } } catch (Exception $e){ continue; } } } $res = ['h' => $h, 'txtArr' => $contArr]; return $res; } /** * @param $fontsize 字体大小 * @param $ttfpath 字体文件 * @param $str 字符串 * @param $width 最大宽度 * @param int $fontangle 将被度量的角度大小 * @param string $charset 编码 * @return string */ public static function autoWrap($fontsize, $ttfpath, $str, $width, $fontangle = 0, $charset = 'utf-8') { $len = mb_strlen($str); $arr = array(); $tmpArr = array(); $tmpStr = ''; for ($i = 1; $i <= $len; $i++) { $tmpStr = mb_substr($str, 0, $i); $size = imagettfbbox($fontsize, $fontangle, public_path() . '/static/fonts/msyh.ttf', $tmpStr); $w = $size[2]; if ($w < $width) { if ($i < $len) { continue; } else { return $tmpStr; } } else { $arr[] = $tmpStr; $str2 = mb_substr($str, $i); $tmpStr .= PHP_EOL . self::autoWrap($fontsize, $ttfpath, $str2, $width); } break; } return trim($tmpStr, PHP_EOL); } /** * 将html文字和图片分类成数组 * @param $str * @return array */ public static function spiltHtml($str){ $content = stripslashes(strip_tags($str, "<img>")); preg_match_all('/<img.*?src=["|'](.*?)["|']/',$content, $match); $imgs = $match[1]; $res = preg_split('/<img(.*?)>/',$content); foreach ($res as $key => $value) { if($value != ""){ $result[] = array( 'type' => 'text', 'text' => htmlspecialchars($value, ENT_QUOTES, 'utf-8') ); } if(!empty($imgs[$key])){ $src = htmlspecialchars($imgs[$key], ENT_COMPAT , 'utf-8'); $result[] = array( 'type' => 'img', 'src' => $src ); } } return $result; }

 

最后

以上就是笨笨菠萝最近收集整理的关于小程序合成海报图片,高度自适应的全部内容,更多相关小程序合成海报图片内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部