侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

PHP分享图片的生成方法

php  /  管理员 发布于 7年前   155

最近工作需求需要生成分享图片,最初用js的html2canvas截图插件各种问题,后来干脆PHP的PG库在后台生成图片,很愉快的解决了各种问题,我们要实现的效果如下图:

假设代码中用到的资源文件夹在当前code_png目录下:

php代码:

/**  * 分享图片生成  * @param $gData 商品数据,array  * @param $codeName 二维码图片  * @param $fileName string 保存文件名,默认空则直接输入图片  */ function createSharePng($gData,$codeName,$fileName = ''){   //创建画布   $im = imagecreatetruecolor(618, 1000);    //填充画布背景色   $color = imagecolorallocate($im, 255, 255, 255);   imagefill($im, 0, 0, $color);    //字体文件   $font_file = "code_png/msyh.ttf";   $font_file_bold = "code_png/msyh_bold.ttf";    //设定字体的颜色   $font_color_1 = ImageColorAllocate ($im, 140, 140, 140);   $font_color_2 = ImageColorAllocate ($im, 28, 28, 28);   $font_color_3 = ImageColorAllocate ($im, 129, 129, 129);   $font_color_red = ImageColorAllocate ($im, 217, 45, 32);    $fang_bg_color = ImageColorAllocate ($im, 254, 216, 217);    //Logo   list($l_w,$l_h) = getimagesize('code_png/logo100_100.png');   $logoImg = @imagecreatefrompng('code_png/logo100_100.png');   imagecopyresized($im, $logoImg, 274, 28, 0, 0, 70, 70, $l_w, $l_h);    //温馨提示   imagettftext($im, 14,0, 100, 130, $font_color_1 ,$font_file, '温馨提示:喜欢长按图片识别二维码即可前往购买');    //商品图片   list($g_w,$g_h) = getimagesize($gData['pic']);   $goodImg = createImageFromFile($gData['pic']);   imagecopyresized($im, $goodImg, 0, 185, 0, 0, 618, 618, $g_w, $g_h);    //二维码   list($code_w,$code_h) = getimagesize($codeName);   $codeImg = createImageFromFile($codeName);   imagecopyresized($im, $codeImg, 440, 820, 0, 0, 170, 170, $code_w, $code_h);    //商品描述   $theTitle = cn_row_substr($gData['title'],2,19);   imagettftext($im, 14,0, 8, 845, $font_color_2 ,$font_file, $theTitle[1]);   imagettftext($im, 14,0, 8, 875, $font_color_2 ,$font_file, $theTitle[2]);    imagettftext($im, 14,0, 8, 935, $font_color_2 ,$font_file, "券后价¥");   imagettftext($im, 28,0, 80, 935, $font_color_red ,$font_file_bold, $gData["price"]);   imagettftext($im, 14,0, 8,970, $font_color_3 ,$font_file, "现价¥".$gData["original_price"]);    //优惠券   if($gData['coupon_price']){     imagerectangle ($im, 125 , 950 , 160 , 975 , $font_color_3);     imagefilledrectangle ($im, 126 , 951 , 159 , 974 , $fang_bg_color);     imagettftext($im, 14,0, 135,970, $font_color_3 ,$font_file, "券");      $coupon_price = strval($gData['coupon_price']);     imagerectangle ($im, 160 , 950 , 198 + (strlen($coupon_price)* 10), 975 , $font_color_3);     imagettftext($im, 14,0, 170,970, $font_color_3 ,$font_file, $coupon_price."元");   }    //输出图片   if($fileName){     imagepng ($im,$fileName);   }else{     Header("Content-Type: image/png");     imagepng ($im);   }    //释放空间   imagedestroy($im);   imagedestroy($goodImg);   imagedestroy($codeImg); }  /**  * 从图片文件创建Image资源  * @param $file 图片文件,支持url  * @return bool|resource  成功返回图片image资源,失败返回false  */ function createImageFromFile($file){   if(preg_match('/http(s)?:\/\//',$file)){     $fileSuffix = getNetworkImgType($file);   }else{     $fileSuffix = pathinfo($file, PATHINFO_EXTENSION);   }    if(!$fileSuffix) return false;    switch ($fileSuffix){     case 'jpeg':       $theImage = @imagecreatefromjpeg($file);       break;     case 'jpg':       $theImage = @imagecreatefromjpeg($file);       break;     case 'png':       $theImage = @imagecreatefrompng($file);       break;     case 'gif':       $theImage = @imagecreatefromgif($file);       break;     default:       $theImage = @imagecreatefromstring(file_get_contents($file));       break;   }    return $theImage; }  /**  * 获取网络图片类型  * @param $url 网络图片url,支持不带后缀名url  * @return bool  */ 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 需要截取的字符串,UTF-8  * @param int $row 截取的行数  * @param int $number  每行截取的字数,中文长度  * @param bool $suffix 最后行是否添加‘...'后缀  * @return array  返回数组共$row个元素,下标1到$row  */ 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] = mg_cn_substr($str,$oneRowNum-6,($r-1)* $oneRowNum).'...';     }else{       $result[$r] = mg_cn_substr($str,$oneRowNum,($r-1)* $oneRowNum);     }     if($theStrlen < $r * $oneRowNum) break;   }    return $result; }  /**  * 按字节截取utf-8字符串  * 识别汉字全角符号,全角中文3个字节,半角英文1个字节  * @param $str 需要切取的字符串  * @param $len 截取长度[字节]  * @param int $start  截取开始位置,默认0  * @return string  */ 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; }   //使用方法------------------------------------------------- //数据格式,如没有优惠券coupon_price值为0。 $gData = [   'pic' => 'code_png/nv_img.jpg',   'title' =>'chic韩版工装羽绒棉服女冬中长款2017新款棉袄大毛领收腰棉衣外套',   'price' => 19.8,   'original_price' => 119.8,   'coupon_price' => 100 ]; //直接输出 createSharePng($gData,'code_png/php_code.jpg'); //输出到图片 createSharePng($gData,'code_png/php_code.jpg','share.png'); 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

您可能感兴趣的文章:

  • 使用PHP生成二维码的两种方法(带logo图像)
  • php生成二维码的几种方式整理及使用实例
  • PHP下通过QRCode类库创建中间带网站LOGO的二维码
  • php微信开发之带参数二维码的使用


  • 上一条:
    php-fpm添加service服务的例子
    下一条:
    PHP receiveMail实现收邮件功能
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(95个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客