小程序导航 wq.xmaht.top

  1. 假设代码中用到的资源文件夹在当前code_png目录下:
  2.  
  3. /**
  4. * 分享图片生成
  5. * @param $gData 商品数据,array
  6. * @param $codeName 二维码图片
  7. * @param $fileName string 保存文件名,默认空则直接输入图片
  8. */
  9. function createSharePng($gData,$codeName,$fileName = ''){
  10. //创建画布
  11. $im = imagecreatetruecolor(618, 1000);
  12.  
  13. //填充画布背景色
  14. $color = imagecolorallocate($im, 255, 255, 255);
  15. imagefill($im, 0, 0, $color);
  16.  
  17. //字体文件
  18. $font_file = "code_png/msyh.ttf";
  19. $font_file_bold = "code_png/msyh_bold.ttf";
  20.  
  21. //设定字体的颜色
  22. $font_color_1 = ImageColorAllocate ($im, 140, 140, 140);
  23. $font_color_2 = ImageColorAllocate ($im, 28, 28, 28);
  24. $font_color_3 = ImageColorAllocate ($im, 129, 129, 129);
  25. $font_color_red = ImageColorAllocate ($im, 217, 45, 32);
  26.  
  27. $fang_bg_color = ImageColorAllocate ($im, 254, 216, 217);
  28.  
  29. //Logo
  30. list($l_w,$l_h) = getimagesize('code_png/logo100_100.png');
  31. $logoImg = @imagecreatefrompng('code_png/logo100_100.png');
  32. imagecopyresized($im, $logoImg, 274, 28, 0, 0, 70, 70, $l_w, $l_h);
  33.  
  34. //温馨提示
  35. imagettftext($im, 14,0, 100, 130, $font_color_1 ,$font_file, '温馨提示:喜欢长按图片识别二维码即可前往购买');
  36.  
  37. //商品图片
  38. list($g_w,$g_h) = getimagesize($gData['pic']);
  39. $goodImg = createImageFromFile($gData['pic']);
  40. imagecopyresized($im, $goodImg, 0, 185, 0, 0, 618, 618, $g_w, $g_h);
  41.  
  42. //二维码
  43. list($code_w,$code_h) = getimagesize($codeName);
  44. $codeImg = createImageFromFile($codeName);
  45. imagecopyresized($im, $codeImg, 440, 820, 0, 0, 170, 170, $code_w, $code_h);
  46.  
  47. //商品描述
  48. $theTitle = cn_row_substr($gData['title'],2,19);
  49. imagettftext($im, 14,0, 8, 845, $font_color_2 ,$font_file, $theTitle[1]);
  50. imagettftext($im, 14,0, 8, 875, $font_color_2 ,$font_file, $theTitle[2]);
  51.  
  52. imagettftext($im, 14,0, 8, 935, $font_color_2 ,$font_file, "券后价¥");
  53. imagettftext($im, 28,0, 80, 935, $font_color_red ,$font_file_bold, $gData["price"]);
  54. imagettftext($im, 14,0, 8,970, $font_color_3 ,$font_file, "现价¥".$gData["original_price"]);
  55.  
  56. //优惠券
  57. if($gData['coupon_price']){
  58. imagerectangle ($im, 125 , 950 , 160 , 975 , $font_color_3);
  59. imagefilledrectangle ($im, 126 , 951 , 159 , 974 , $fang_bg_color);
  60. imagettftext($im, 14,0, 135,970, $font_color_3 ,$font_file, "券");
  61.  
  62. $coupon_price = strval($gData['coupon_price']);
  63. imagerectangle ($im, 160 , 950 , 198 + (strlen($coupon_price)* 10), 975 , $font_color_3);
  64. imagettftext($im, 14,0, 170,970, $font_color_3 ,$font_file, $coupon_price."元");
  65. }
  66.  
  67. //输出图片
  68. if($fileName){
  69. imagepng ($im,$fileName);
  70. }else{
  71. Header("Content-Type: image/png");
  72. imagepng ($im);
  73. }
  74.  
  75. //释放空间
  76. imagedestroy($im);
  77. imagedestroy($goodImg);
  78. imagedestroy($codeImg);
  79. }
  80.  
  81. /**
  82. * 从图片文件创建Image资源
  83. * @param $file 图片文件,支持url
  84. * @return bool|resource 成功返回图片image资源,失败返回false
  85. */
  86. function createImageFromFile($file){
  87. if(preg_match('/http(s)?:\/\//',$file)){
  88. $fileSuffix = getNetworkImgType($file);
  89. }else{
  90. $fileSuffix = pathinfo($file, PATHINFO_EXTENSION);
  91. }
  92.  
  93. if(!$fileSuffix) return false;
  94.  
  95. switch ($fileSuffix){
  96. case 'jpeg':
  97. $theImage = @imagecreatefromjpeg($file);
  98. break;
  99. case 'jpg':
  100. $theImage = @imagecreatefromjpeg($file);
  101. break;
  102. case 'png':
  103. $theImage = @imagecreatefrompng($file);
  104. break;
  105. case 'gif':
  106. $theImage = @imagecreatefromgif($file);
  107. break;
  108. default:
  109. $theImage = @imagecreatefromstring(file_get_contents($file));
  110. break;
  111. }
  112.  
  113. return $theImage;
  114. }
  115.  
  116. /**
  117. * 获取网络图片类型
  118. * @param $url 网络图片url,支持不带后缀名url
  119. * @return bool
  120. */
  121. function getNetworkImgType($url){
  122. $ch = curl_init(); //初始化curl
  123. curl_setopt($ch, CURLOPT_URL, $url); //设置需要获取的URL
  124. curl_setopt($ch, CURLOPT_NOBODY, 1);
  125. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);//设置超时
  126. curl_setopt($ch, CURLOPT_TIMEOUT, 3);
  127. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //支持https
  128. curl_exec($ch);//执行curl会话
  129. $http_code = curl_getinfo($ch);//获取curl连接资源句柄信息
  130. curl_close($ch);//关闭资源连接
  131.  
  132. if ($http_code['http_code'] == 200) {
  133. $theImgType = explode('/',$http_code['content_type']);
  134.  
  135. if($theImgType[0] == 'image'){
  136. return $theImgType[1];
  137. }else{
  138. return false;
  139. }
  140. }else{
  141. return false;
  142. }
  143. }
  144.  
  145. /**
  146. * 分行连续截取字符串
  147. * @param $str 需要截取的字符串,UTF-8
  148. * @param int $row 截取的行数
  149. * @param int $number 每行截取的字数,中文长度
  150. * @param bool $suffix 最后行是否添加‘...’后缀
  151. * @return array 返回数组共$row个元素,下标1到$row
  152. */
  153. function cn_row_substr($str,$row = 1,$number = 10,$suffix = true){
  154. $result = array();
  155. for ($r=1;$r<=$row;$r++){
  156. $result[$r] = '';
  157. }
  158.  
  159. $str = trim($str);
  160. if(!$str) return $result;
  161.  
  162. $theStrlen = strlen($str);
  163.  
  164. //每行实际字节长度
  165. $oneRowNum = $number * 3;
  166. for($r=1;$r<=$row;$r++){
  167. if($r == $row and $theStrlen > $r * $oneRowNum and $suffix){
  168. $result[$r] = mg_cn_substr($str,$oneRowNum-6,($r-1)* $oneRowNum).'...';
  169. }else{
  170. $result[$r] = mg_cn_substr($str,$oneRowNum,($r-1)* $oneRowNum);
  171. }
  172. if($theStrlen < $r * $oneRowNum) break;
  173. }
  174.  
  175. return $result;
  176. }
  177.  
  178. /**
  179. * 按字节截取utf-8字符串
  180. * 识别汉字全角符号,全角中文3个字节,半角英文1个字节
  181. * @param $str 需要切取的字符串
  182. * @param $len 截取长度[字节]
  183. * @param int $start 截取开始位置,默认0
  184. * @return string
  185. */
  186. function mg_cn_substr($str,$len,$start = 0){
  187. $q_str = '';
  188. $q_strlen = ($start + $len)>strlen($str) ? strlen($str) : ($start + $len);
  189.  
  190. //如果start不为起始位置,若起始位置为乱码就按照UTF-8编码获取新start
  191. if($start and json_encode(substr($str,$start,1)) === false){
  192. for($a=0;$a<3;$a++){
  193. $new_start = $start + $a;
  194. $m_str = substr($str,$new_start,3);
  195. if(json_encode($m_str) !== false) {
  196. $start = $new_start;
  197. break;
  198. }
  199. }
  200. }
  201.  
  202. //切取内容
  203. for($i=$start;$i<$q_strlen;$i++){
  204. //ord()函数取得substr()的第一个字符的ASCII码,如果大于0xa0的话则是中文字符
  205. if(ord(substr($str,$i,1))>0xa0){
  206. $q_str .= substr($str,$i,3);
  207. $i+=2;
  208. }else{
  209. $q_str .= substr($str,$i,1);
  210. }
  211. }
  212. return $q_str;
  213. }
  214.  
  215. //使用方法-------------------------------------------------
  216. //数据格式,如没有优惠券coupon_price值为0。
  217. $gData = [
  218. 'pic' => 'code_png/nv_img.jpg',
  219. 'title' =>'chic韩版工装羽绒棉服女冬中长款2017新款棉袄大毛领收腰棉衣外套',
  220. 'price' => 19.8,
  221. 'original_price' => 119.8,
  222. 'coupon_price' => 100
  223. ];
  224. //直接输出
  225. createSharePng($gData,'code_png/php_code.jpg');
  226. //输出到图片
  227. createSharePng($gData,'code_png/php_code.jpg','share.png');

使用PHP生成分享图片的更多相关文章

  1. 使用pillow生成分享图片

    重复性的工作一定要交给计算机去做! 有时候要为公司做一张宣传用的分享图片,很简单交给设计通过ps.AI做好就行了,但是如果一个网站要为每个用户生成一张专属的分享图片,如果让设计师一张一张的去做,哪设计 ...

  2. 微信小程序利用canvas生成海报分享图片

    一 . 效果 这是借用女神照生成的分享的海报,图片来自网络. 新增了poster组件和更新图片自适应 二 . 准备 准备两张图片连接,最好是自己开发账号验证的https图片链接. 三 . 实现思路 其 ...

  3. [深入浅出WP8.1(Runtime)]生成图片和存储生成的图片文件

    7.2.3 使用RenderTargetBitmap类生成图片 RenderTargetBitmap类可以将可视化对象转换为位图,也就是说它可以将任意的UIElement以位图的形式呈现.那么我们在实 ...

  4. 【转】DelphiXE10.2.3——跨平台生成验证码图片

    原文地址 Java.PHP.C#等很容易在网上找到生成验证码图片的代码,Delphi却寥寥无几,昨天花了一整天时间,做了个跨平台的验证码,可以用在C/S和B/S端,支持Windows.Linux.An ...

  5. Android分享图片失败解决方案

    前言:在做图片分享到微博或是用彩信分享的时候,会遇到“无法将图片添加到信息中”,其实这个问题的原因是创建的那个图片默认是,只能被当前应用调用,无法被其他应用调用,即分享的时候,无法读取到图片,并提示I ...

  6. 小程序(Wepy)--生成海报图片

    对于小程序的分享, 除了分享给朋友, 好友群,是可以直接做到的, 但是要想扩大推广范围, 通过生成海报图片, 将自己小程序码带进去,应该是目前我所知的好办法了. 但是海报也不是那么好搞.之前自己手写出 ...

  7. Python数据展示 - 生成表格图片

    前言 前一篇文章介绍了推送信息到企业微信群里,其中一个项目推送的信息是使用Python自动生成的表格,本文来讲讲如何用Python生成表格图片. 选一个合适库 Python最大的优点就是第三方库丰富, ...

  8. Android微信分享图片大于32k进行压缩

    微信分享视频的时候,需要传一个图片数组,大小不能大于32k. 解决方案:使用Bitmap自带的compress方法解决了这个问题. 源码如下: <span style="font-si ...

  9. java web学习总结(九) -------------------通过Servlet生成验证码图片

    一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:

随机推荐

  1. c#真正判断文件类型

    //真正判断文件类型的关键函数 public static bool IsAllowedExtension2(FileUpload hifile) { if (hifile != null) { Sy ...

  2. MongoDB之mongodb.cnf配置

    # mongodb3.2.1 的主配置文件,将此文件放置于 mongodb3.2.1/bin 目录下 # hapday 2016-01-27-16:55 start # 数据文件存放目录 dbpath ...

  3. [C#]为什么Interface里的成员不能使用static修饰?

    首先引用MSDN里的原文 Interface members are automatically public, and they can't include any access modifiers ...

  4. Linq to Sql 左连接 , 取右表可能为 null的 int类型字段

    linq to sql , linq to entity 遇到一个问题, 主表, 从表 一对一 关系,  主表有记录, 从表 可能没有记录. 现在要查询 主表+从表 的某几个字段. 从表字段 有的是 ...

  5. jsp:jsp包含文件的两种方式

    第一种:include指令 include指令:当JSP转换成Servlet时引入指定文件(指令元素),这是一种静态包含,它运行的时候不会单独编译成.class文件,它生成一个新的整体.class文件 ...

  6. 笨办法学Python(四)

    习题 4: 变量(variable)和命名 你已经学会了 print 和算术运算.下一步你要学的是“变量”.在编程中,变量只不过是用来指代某个东西的名字.程序员通过使用变量名可以让他们的程序读起来更像 ...

  7. 使用Excel调用ABAP系统的函数

    效果:在excel里创建一个按钮,开发一些VB script,可以连接指定的ABAP系统并执行系统里的ABAP function module. 在这里例子里执行ABAP系统的函数TH_USER_LI ...

  8. 在jupyter notebook 中同时使用安装不同版本的python内核-从而可以进行切换

    在安装anaconda的时候,默认安装的是python3.6 但是cs231n课程作业是在py2.7环境下运行的.所以需要在jupyter notebook中安装并启用python2.7版本 方法: ...

  9. 利用API设置桌面背景

    实现效果: 知识运用: API函数SystemParametersInfo 实现代码: [DllImport("user32.dll", EntryPoint = "Sy ...

  10. [转] JAVA中读取网络中的图片资源导入到EXCEL中

    需求 导出人员的信息并且加上人员的照片至EXCEL中 完整的代码 //创建一个表格 HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb ...