php 绘制商品海报

  1. /**

  2. * 分享图片生成

  3. * @param $gData 商品数据,array

  4. * @param $codeName 二维码图片

  5. * @param $fileName string 保存文件名,默认空则直接输入图片

  6. */

  7. function createSharePng($gData,$codeName,$fileName = ''){

  8. //创建画布

  9. $im = imagecreatetruecolor(618, 1000);

  10.  
  11. //填充画布背景色

  12. $color = imagecolorallocate($im, 255, 255, 255);

  13. imagefill($im, 0, 0, $color);

  14.  
  15. //字体文件

  16. $font_file = "code_png/msyh.ttf";

  17. $font_file_bold = "code_png/msyh_bold.ttf";

  18.  
  19. //设定字体的颜色

  20. $font_color_1 = ImageColorAllocate ($im, 140, 140, 140);

  21. $font_color_2 = ImageColorAllocate ($im, 28, 28, 28);

  22. $font_color_3 = ImageColorAllocate ($im, 129, 129, 129);

  23. $font_color_red = ImageColorAllocate ($im, 217, 45, 32);

  24.  
  25. $fang_bg_color = ImageColorAllocate ($im, 254, 216, 217);

  26.  
  27. //Logo

  28. list($l_w,$l_h) = getimagesize('code_png/logo100_100.png');

  29. $logoImg = @imagecreatefrompng('code_png/logo100_100.png');

  30. imagecopyresized($im, $logoImg, 274, 28, 0, 0, 70, 70, $l_w, $l_h);

  31.  
  32. //温馨提示

  33. imagettftext($im, 14,0, 100, 130, $font_color_1 ,$font_file, '温馨提示:喜欢长按图片识别二维码即可前往购买');

  34.  
  35. //商品图片

  36. list($g_w,$g_h) = getimagesize($gData['pic']);

  37. $goodImg = createImageFromFile($gData['pic']);

  38. imagecopyresized($im, $goodImg, 0, 185, 0, 0, 618, 618, $g_w, $g_h);

  39.  
  40. //二维码

  41. list($code_w,$code_h) = getimagesize($codeName);

  42. $codeImg = createImageFromFile($codeName);

  43. imagecopyresized($im, $codeImg, 440, 820, 0, 0, 170, 170, $code_w, $code_h);

  44.  
  45. //商品描述

  46. $theTitle = cn_row_substr($gData['title'],2,19);

  47. imagettftext($im, 14,0, 8, 845, $font_color_2 ,$font_file, $theTitle[1]);

  48. imagettftext($im, 14,0, 8, 875, $font_color_2 ,$font_file, $theTitle[2]);

  49.  
  50. imagettftext($im, 14,0, 8, 935, $font_color_2 ,$font_file, "券后价¥");

  51. imagettftext($im, 28,0, 80, 935, $font_color_red ,$font_file_bold, $gData["price"]);

  52. imagettftext($im, 14,0, 8,970, $font_color_3 ,$font_file, "现价¥".$gData["original_price"]);

  53.  
  54. //优惠券

  55. if($gData['coupon_price']){

  56. imagerectangle ($im, 125 , 950 , 160 , 975 , $font_color_3);

  57. imagefilledrectangle ($im, 126 , 951 , 159 , 974 , $fang_bg_color);

  58. imagettftext($im, 14,0, 135,970, $font_color_3 ,$font_file, "券");

  59.  
  60. $coupon_price = strval($gData['coupon_price']);

  61. imagerectangle ($im, 160 , 950 , 198 + (strlen($coupon_price)* 10), 975 , $font_color_3);

  62. imagettftext($im, 14,0, 170,970, $font_color_3 ,$font_file, $coupon_price."元");

  63. }

  64.  
  65. //输出图片

  66. if($fileName){

  67. imagepng ($im,$fileName);

  68. }else{

  69. Header("Content-Type: image/png");

  70. imagepng ($im);

  71. }

  72.  
  73. //释放空间

  74. imagedestroy($im);

  75. imagedestroy($goodImg);

  76. imagedestroy($codeImg);

  77. }

  78.  
  79. /**

  80. * 从图片文件创建Image资源

  81. * @param $file 图片文件,支持url

  82. * @return bool|resource 成功返回图片image资源,失败返回false

  83. */

  84. function createImageFromFile($file){

  85. if(preg_match('/http(s)?:\/\//',$file)){

  86. $fileSuffix = getNetworkImgType($file);

  87. }else{

  88. $fileSuffix = pathinfo($file, PATHINFO_EXTENSION);

  89. }

  90.  
  91. if(!$fileSuffix) return false;

  92.  
  93. switch ($fileSuffix){

  94. case 'jpeg':

  95. $theImage = @imagecreatefromjpeg($file);

  96. break;

  97. case 'jpg':

  98. $theImage = @imagecreatefromjpeg($file);

  99. break;

  100. case 'png':

  101. $theImage = @imagecreatefrompng($file);

  102. break;

  103. case 'gif':

  104. $theImage = @imagecreatefromgif($file);

  105. break;

  106. default:

  107. $theImage = @imagecreatefromstring(file_get_contents($file));

  108. break;

  109. }

  110.  
  111. return $theImage;

  112. }

  113.  
  114. /**

  115. * 获取网络图片类型

  116. * @param $url 网络图片url,支持不带后缀名url

  117. * @return bool

  118. */

  119. function getNetworkImgType($url){

  120. $ch = curl_init(); //初始化curl

  121. curl_setopt($ch, CURLOPT_URL, $url); //设置需要获取的URL

  122. curl_setopt($ch, CURLOPT_NOBODY, 1);

  123. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);//设置超时

  124. curl_setopt($ch, CURLOPT_TIMEOUT, 3);

  125. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //支持https

  126. curl_exec($ch);//执行curl会话

  127. $http_code = curl_getinfo($ch);//获取curl连接资源句柄信息

  128. curl_close($ch);//关闭资源连接

  129.  
  130. if ($http_code['http_code'] == 200) {

  131. $theImgType = explode('/',$http_code['content_type']);

  132.  
  133. if($theImgType[0] == 'image'){

  134. return $theImgType[1];

  135. }else{

  136. return false;

  137. }

  138. }else{

  139. return false;

  140. }

  141. }

  142.  
  143. /**

  144. * 分行连续截取字符串

  145. * @param $str 需要截取的字符串,UTF-8

  146. * @param int $row 截取的行数

  147. * @param int $number 每行截取的字数,中文长度

  148. * @param bool $suffix 最后行是否添加‘...’后缀

  149. * @return array 返回数组共$row个元素,下标1到$row

  150. */

  151. function cn_row_substr($str,$row = 1,$number = 10,$suffix = true){

  152. $result = array();

  153. for ($r=1;$r<=$row;$r++){

  154. $result[$r] = '';

  155. }

  156.  
  157. $str = trim($str);

  158. if(!$str) return $result;

  159.  
  160. $theStrlen = strlen($str);

  161.  
  162. //每行实际字节长度

  163. $oneRowNum = $number * 3;

  164. for($r=1;$r<=$row;$r++){

  165. if($r == $row and $theStrlen > $r * $oneRowNum and $suffix){

  166. $result[$r] = mg_cn_substr($str,$oneRowNum-6,($r-1)* $oneRowNum).'...';

  167. }else{

  168. $result[$r] = mg_cn_substr($str,$oneRowNum,($r-1)* $oneRowNum);

  169. }

  170. if($theStrlen < $r * $oneRowNum) break;

  171. }

  172.  
  173. return $result;

  174. }

  175.  
  176. /**

  177. * 按字节截取utf-8字符串

  178. * 识别汉字全角符号,全角中文3个字节,半角英文1个字节

  179. * @param $str 需要切取的字符串

  180. * @param $len 截取长度[字节]

  181. * @param int $start 截取开始位置,默认0

  182. * @return string

  183. */

  184. function mg_cn_substr($str,$len,$start = 0){

  185. $q_str = '';

  186. $q_strlen = ($start + $len)>strlen($str) ? strlen($str) : ($start + $len);

  187.  
  188. //如果start不为起始位置,若起始位置为乱码就按照UTF-8编码获取新start

  189. if($start and json_encode(substr($str,$start,1)) === false){

  190. for($a=0;$a<3;$a++){

  191. $new_start = $start + $a;

  192. $m_str = substr($str,$new_start,3);

  193. if(json_encode($m_str) !== false) {

  194. $start = $new_start;

  195. break;

  196. }

  197. }

  198. }

  199.  
  200. //切取内容

  201. for($i=$start;$i<$q_strlen;$i++){

  202. //ord()函数取得substr()的第一个字符的ASCII码,如果大于0xa0的话则是中文字符

  203. if(ord(substr($str,$i,1))>0xa0){

  204. $q_str .= substr($str,$i,3);

  205. $i+=2;

  206. }else{

  207. $q_str .= substr($str,$i,1);

  208. }

  209. }

  210. return $q_str;

  211. }

  212.  
  213.  
  214. //使用方法-------------------------------------------------

  215. //数据格式,如没有优惠券coupon_price值为0。

  216. $gData = [

  217. 'pic' => 'code_png/nv_img.jpg',

  218. 'title' =>'chic韩版工装羽绒棉服女冬中长款2017新款棉袄大毛领收腰棉衣外套',

  219. 'price' => 19.8,

  220. 'original_price' => 119.8,

  221. 'coupon_price' => 100

  222. ];

  223. //直接输出

  224. createSharePng($gData,'code_png/php_code.jpg');

  225. //输出到图片

  226. createSharePng($gData,'code_png/php_code.jpg','share.png');

  227. 转载地址:https://blog.csdn.net/u012569217/article/details/79075374

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值