思路:

1、请求微信接口获取一定尺寸微信二维码

2、准备海报主图,处理尺寸按比例缩放

3、准备分享语录,计算段落高度

4、生成海报:创建画布,分写别入按顺序和位置写入二维码、图片、文字等

5、保存海报

具体如下:

1、请求微信接口获取一定尺寸微信二维码

$access_token = 'jkagfgjkdahfgadhsfkdsj';
$url = sprintf("https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=%s",$access_token);
$postdata = '{"path": "/pages/subActivity/pages/activityDetail/activityDetail?id='.$id.'", "width": 210}';//微信的二维码
$image = http_post($url,$postdata);
//准备微信二维码以备生成海报使用
$wxim = imagecreatefromstring($image);$wxim_size = 270;
function http_post($url = '', $param = '') {if (empty($url) || empty($param)) {return false;}$postUrl = $url;$curlPost = $param;$ch = curl_init();//初始化curlcurl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页curl_setopt($ch, CURLOPT_HEADER, 0);//设置headercurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上curl_setopt($ch, CURLOPT_POST, 1);//post提交方式curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); $data = curl_exec($ch);//运行curl curl_close($ch); return $data; }

2、准备海报主图,处理尺寸按比例缩放

$share_img = '图片路径'$share_img_size = getimagesize($share_img); //图片尺寸$shareim = imagecreatefromstring(file_get_contents($share_img));//计算原图长宽比例
$size_rate = $share_img_size[1]/$share_img_size[0];

//新图尺寸
$share_img_width = 450;
$share_img_height = intval($share_img_width*$size_rate);

//复制生成新图并保存
$new_share_img = imagecreatetruecolor($share_img_width, $share_img_height);imagecopyresampled($new_share_img, $shareim, 0, 0, 0, 0,$share_img_width,$share_img_height,$share_img_size[0], $share_img_size[1]);
$new_share_img_filename = $dir.'/'.$id.'_new_share.jpg';
imagejpeg($new_share_img, $new_share_img_filename);
imagedestroy($new_share_img);
//准备可写入新分享主图 以备生成海报使用
$new_shareim = imagecreatefromstring(file_get_contents($dir.'/'.$id.'_new_share.jpg'));unlink($new_share_img_filename); //删除临时新的分享图

3、准备分享语录文字,计算高度

 //准备分享语录$content = "";// 将字符串拆分成一个个单字 保存到数组 letter 中for ($i=0;$i<mb_strlen($share_title);$i++) {$letter[] = mb_substr($share_title, $i, 1);}foreach ($letter as $l) {$teststr = $content." ".$l;$share_title_size = imagettfbbox(16, 0, $font, $teststr);// 判断拼接后的字符串是否超过预设的宽度if (($share_title_size[2] > $share_img_width) && ($content !== "")) {$content .= "\n";}$content .= $l;} //文字高度$share_title_height = $share_title_size[3];

4、生成海报:创建画布,分写别入按顺序和位置写入二维码、图片、文字等

//生成海报
$font = FCPATH.'/styles/font/wryh.ttf';   //保证字体文件目录正确
$filepath = $dir.'/'.$id.'_p.jpg';
$posters_width = 510;
$posters_height = 130 + $share_img_height+$share_title_height + 280;$newimg = imagecreatetruecolor($posters_width, $posters_height);
$bg = imagecolorallocate($newimg, 255, 255, 255);
imagefill($newimg,0,0,$bg);
$black = imagecolorallocate($newimg, 0, 0, 0);//写宣传言
imagettftext($newimg,18,0,30,47,$black,$font,'美好生活');
imagettftext($newimg,15,0,30,76,$black,$font,'关注美好生活了解社区好生活');//写分享图片
imagecopy($newimg,$new_shareim,30,95,0,0,$share_img_width,$share_img_height);//写分享标题
imagettftext ($newimg, 15, 0, 30, $share_img_height+122, $black, $font, $content );//写二维码
$wxim_start_hight = 130+$share_title_height+$share_img_height;
imagecopy($newimg,$wxim,20,$wxim_start_hight,0,0,$wxim_size,$wxim_size);$gray = imagecolorallocate($newimg, 136, 136, 136);//写提示语
$tips = '长按保存海报至手机相册';
$tips_hight = $wxim_start_hight+200;
imagettftext($newimg,13,0,290,$tips_hight,$gray,$font,$tips);//海报生成保存
imagejpeg($newimg, $filepath);
ImageDestroy($newimg);
ImageDestroy($wxim);

整体代码

         $access_token = '请求微信接口的aceess_token';if($access_token){$url = sprintf("https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=%s",$access_token);$postdata = '{"path": "/pages/subActivity/pages/activityDetail/activityDetail?id='.$id.'", "width": 210}';//微信的二维码$image = http_post($url,$postdata);if(strlen($image) < 200){ //出错了log_message('error',"getTopicQRCodeProcessed, error : " . $image);//刷新tokenls('wx_service');$this->wx_service->refreshToken();return false;}$dir = FCPATH.'upload/qrcode/'.date('Ym');MkFolder($dir);$path = $dir.'/'.$id.'.jpg';file_put_contents($path,$image);$qrcode = pathATOR($path);$share_title = element('share_title',$event);$share_img = element('share_img_moment',$event) ? : element('cover',$event);$share_img = get_oss_file_url($share_img);$font = FCPATH.'/styles/font/wryh.ttf';//准备分享图片  先处理图片尺寸$share_img_size = getimagesize($share_img);$shareim = imagecreatefromstring(file_get_contents($share_img));$size_rate = $share_img_size[1]/$share_img_size[0];$share_img_width = 450;$share_img_height = intval($share_img_width*$size_rate);$new_share_img = imagecreatetruecolor($share_img_width, $share_img_height);imagecopyresampled($new_share_img, $shareim, 0, 0, 0, 0,$share_img_width,$share_img_height,$share_img_size[0], $share_img_size[1]);$new_share_img_filename = $dir.'/'.$id.'_new_share.jpg';imagejpeg($new_share_img, $new_share_img_filename);imagedestroy($new_share_img);$new_shareim = imagecreatefromstring(file_get_contents($dir.'/'.$id.'_new_share.jpg'));unlink($new_share_img_filename);//准备分享标题$content = "";// 将字符串拆分成一个个单字 保存到数组 letter 中for ($i=0;$i<mb_strlen($share_title);$i++) {$letter[] = mb_substr($share_title, $i, 1);}foreach ($letter as $l) {$teststr = $content." ".$l;$share_title_size = imagettfbbox(16, 0, $font, $teststr);// 判断拼接后的字符串是否超过预设的宽度if (($share_title_size[2] > $share_img_width) && ($content !== "")) {$content .= "\n";}$content .= $l;}$share_title_height = $share_title_size[3];//准备微信二维码$wxim = imagecreatefromstring($image);$wxim_size = 270;//生成海报$filepath = $dir.'/'.$id.'_p.jpg';$posters_width = 510;$posters_height = 130 + $share_img_height+$share_title_height + 280;$newimg = imagecreatetruecolor($posters_width, $posters_height);$bg = imagecolorallocate($newimg, 255, 255, 255);imagefill($newimg,0,0,$bg);$black = imagecolorallocate($newimg, 0, 0, 0);//写宣传言imagettftext($newimg,18,0,30,47,$black,$font,'昌品生活');imagettftext($newimg,15,0,30,76,$black,$font,'关注昌品生活了解社区好生活');//写分享图片imagecopy($newimg,$new_shareim,30,95,0,0,$share_img_width,$share_img_height);//写分享标题imagettftext ($newimg, 15, 0, 30, $share_img_height+122, $black, $font, $content );//写二维码$wxim_start_hight = 130+$share_title_height+$share_img_height;imagecopy($newimg,$wxim,20,$wxim_start_hight,0,0,$wxim_size,$wxim_size);$gray = imagecolorallocate($newimg, 136, 136, 136);//写提示语$tips = '长按保存海报至手机相册';$tips_hight = $wxim_start_hight+200;imagettftext($newimg,13,0,290,$tips_hight,$gray,$font,$tips);//海报生成保存imagejpeg($newimg, $filepath);ImageDestroy($newimg);ImageDestroy($wxim);$posters = pathATOR($filepath);

转载于:https://www.cnblogs.com/yimingwang/p/11460829.html

PHP 图片+文字+二维码生成小程序分享海报相关推荐

  1. 聚合二维码生成(小程序二维码、付款码)

    1. 付款码 点击前往 2. 小程序二维码 扫码进入小程序 第一步: 配置微信小程序二维码 微信公众平台 >开发 >开发设置 >扫普通链接二维码打开小程序 点击添加,配置二维码规则 ...

  2. thinkphp 添加图片 文字 水印生成带二维码的小程序推广海报

    $image = new \Think\Image();                 $bgm='./Public/haibao.png'; //背景图                 $post ...

  3. thinkphp 添加图片 文字 水印生成带二维码的小程序推广海报

    $image = new \Think\Image(); $bgm='./Public/haibao.png'; //背景图 poster="./Public/poster/poster=& ...

  4. 微信小程序参数二维码生成朋友圈分享图片

    前言 小程序目前无法分享到微信朋友圈,可朋友圈是一个非常重要的传播途径,所以得想办法把这个资源利用起来 微信小程序支持通过扫描/长按识别二维码或小程序码图片的方式进入一个小程序首页或小程序中某个特定页 ...

  5. 飞鹅小票打印机嵌入生成指定小程序页面二维码的解决方案 | 扫普通链接二维码打开小程序示例 | 生成正方形小程序码

    部分朋友不需要打印机的业务,则 忽略有关打印机的部分 即可. 其他有关 微信小程序配置的介绍是通用的!通用的! 生成正方形小程序码,请看 标题一. 扫普通链接生成的二维码打开小程序,请看 标题二. 目 ...

  6. 开发一个基于ZXing库以及安卓Studio的二维码扫描小程序(二)

    开发一个基于ZXing库以及安卓Studio的二维码扫描小程序(二) 下面我们做一个ZXing扫描二维码的例子,是通过安卓库的方式引用ZXing应用代码. 开发步骤 建立一个安卓工程(Project) ...

  7. 扫普通二维码进入小程序、链接进入小程序

    目录 一.扫普通二维码进入小程序 1.扫码进入小程序管理页 2.点击开发管理-开发设置-扫普通链接二维码打开小程序-添加 3. 完善配置内容 二. 前端获取参数 三.开发本地调试 一.扫普通二维码进入 ...

  8. 微信小程序生成带参数的二维码以及小程序码

    微信小程序生成带参数的二维码 官方共给了三个接口调用,大家可以根据自己的实际情况来使用,我这里使用的是接口B和接口C. 官方文档地址 业务需求: 扫描二维码进入指定商品页面,需要的参数为商品id(go ...

  9. 扫描普通二维码进入小程序

    一.按照官方文档步骤进行配置,官方文档–扫普通链接二维码打开小程序 配置:登录小程序后台,进入"开发-开发管理 - 开发设置 - 扫普通链接二维码打开小程序",开启功能后配置二维码 ...

最新文章

  1. mybatis 代码生成器_spring Boot手把手教学(4): mybatis-plus 代码生成器,自动帮你生成代码
  2. 使用U盘安装win7系统,遇到“无法定位现有系统分区”问题
  3. SIGIR 2021|重新思考Attention在CTR预估中作用
  4. python 相关系数_python如何计算相关系数与协方差
  5. 借钱,一定要有还钱的素质
  6. 利用Seafile搭建私有文件同步云盘
  7. ASP.NET中的FILE对象总结
  8. RTTI VS Reflection
  9. Linux下Docker命令自动补全
  10. Solr(一) Solr 简介及搜索原理
  11. 【NodeJS】20 koa 企业级Cms内容管理系统-XMind功能分析、ERStudio设计数据库ER图
  12. 拆解玩具电池充电器:充久了可能会爆,廉价电路方案让人震惊!
  13. Duplicate keys detected: ‘gggggg‘. This may cause an update error.的解决办法
  14. xshell连接不上虚拟机Linux
  15. 【京东电商网站主界面仿写——HTML第二部分】
  16. 【arcgis10.8最新版安装】
  17. Oracle基础包之DBMS_RLS(八)
  18. 【WiFi破解】WiFi密码不知道了怎么办,一行代码轻松破解
  19. CSS实现三角形原理及过程
  20. 秋招季,写份亮眼的求职简历

热门文章

  1. sqlalchemy case when分组查询统计
  2. UEFI开发探索28 – 用Windbg建立UEFI调试环境
  3. Window效率软件清单
  4. Linux CentOS 7.3 1611 基础命令
  5. 手机销量下降,企业该如何调整使用APS计划排产软件生产计划?
  6. 考研计算器(fx-991CN x 中文版CASIO)求逆
  7. 暗幽***风恋组综合工具解压安装包2013元月27日更新版
  8. Wannafly挑战赛13 B:Jxc军训(逆元)
  9. Shader 能量法球特效
  10. rv1108-evb-v12 sdk的视频拍照的流程(1)