思路:

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();//初始化curl
curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
curl_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);
//刷新token
ls('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);

PHP 图片+文字+二维码生成小程序分享海报的更多相关文章

  1. 微信小程序扫描普通二维码打开小程序的方法

    很久没有写博客了,之前换了一份工作,很久没有做Android开发了,现在转做前端开发了,记录一下遇到的问题及解决的方法. 最近做微信小程序开发,遇到一个需求,后台管理系统生成的问卷和投票会有一个二维码 ...

  2. 微信小程序 - 配置普通二维码跳小程序

    普通二维码跳小程序规则: https://developers.weixin.qq.com/miniprogram/introduction/qrcode.html#%E5%8A%9F%E8%83%B ...

  3. 带logo图片或不带logo图片的二维码生成与解析,亲测成功

    最近公司需要实现二维码功能,本人经过一顿百度,终于实现了,因有3个功能:不带logo图片.带logo图片.解析二维码,篇幅较长,请耐心读之,直接复制粘贴即可. 前提:myeclipse10:jar包: ...

  4. 微信小程序生成带参数的二维码(小程序码)独家asp.net的服务端c#完整代码

    一)我先用的小程序端的wx.request去调用API,发现竟然是一个坑! wx.request({ url: 'https://api.weixin.qq.com/wxa/getwxacodeunl ...

  5. C# - VS2019调用ZXing.NET实现条码、二维码和带有Logo的二维码生成

    前言 C# WinFrm程序调用ZXing.NET实现条码.二维码和带有Logo的二维码生成. ZXing.NET导入 GitHub开源库 ZXing.NET开源库githib下载地址:https:/ ...

  6. PHP批量生成底部带编号二维码(二维码生成+文字生成图片+图片拼接合并)

    PHP批量生成带底部编号二维码(二维码生成+文字生成图片+图片拼接合并) 需求: 输入编号如 : cb05-0000001  至 cb05-0000500 批量生成 以编号为名称的下图二维码,然后压缩 ...

  7. .NET使用ZXing.NET生成中间带图片的二维码

    很久之前就有写这样的代码了,只是一直没记录下来,偶然想写成博客. 把之前的代码封装成函数,以方便理解以及调用. 基于开源的 ZXing.NET 组件,代码如下: 先添加对ZXing.NET的引用,然后 ...

  8. 利用phpqrcode二维码生成类库和imagecopymerge函数制拼接图片的经验

    前期准备 引入phpqrcode类库(下载地址:https://sourceforge.net/projects/phpqrcode/) PHP开启GD扩展库支持 1.利用phpqrcode生成二维码 ...

  9. java 二维码生成(可带图片)springboot版

    本文(2019年6月29日 飞快的蜗牛博客) 有时候,男人和女人是两个完全不同的世界,男人的玩笑和女人的玩笑也完全是两码事,爱的人完全不了解你,你也不要指望一个女人了解你,所以男的不是要求别人怎么样, ...

随机推荐

  1. postman中x-www-form-urlencoded与form-data的区别

    这是W3C定义的两种不同的表格类型,如果你想发送简单的text/ASCII数据,使用x-www-form-urlencoded , 这是默认的形式. 如果你想发送非ASCII文本或者大的二进制数据,使 ...

  2. Linux--Linux的网络--05

    一层: HUB --- 集线器 总线型结构,使用泛洪方式 二层: 在早期,pc通信只需要MAC地址进行数据转发 网桥 --- 交换机  :维护MAC地址表 三层: 网络的增大,就需要逻辑地址(IP地址 ...

  3. 微信授权获取code/openid

    微信网页授权 如果用户在微信客户端中访问第三方网页,公众号可以通过微信网页授权机制,来获取用户基本信息,进而实现业务逻辑. 关于网页授权回调域名的说明 1.在微信公众号请求用户网页授权之前,开发者需要 ...

  4. linux tomcat 中文文件上传乱码

    找到tomcat文件夹下的conf/server.xml,在connector节点下新增URIEncoding="utf-8"参数 <Connector connection ...

  5. 阅读《Effective Java》每条tips的理解和总结(2)(持续更新)

    15. 使类和成员的可访问性最小化 一个好用的类的属性必须要隐藏起来,干净的将它与类的api分离开来,类之间只通过api相互使用,降低他们之间的耦合性.为了做到这一点,建议根据情况选择尽可能低的访问级 ...

  6. apache-2.4.x 编译安装方法

    apache-2.4.x 编译安装方法 作者:朱 茂海 /分类:Apache 字号:L M S apache-.2与新出的apache-.4安装不同的地方在于,.4版的已经不自带apr库,所以在安装a ...

  7. weighted choice in python

    对列表按概率采样 Input: a collection C of elements and a probability distribution p over C; Output: an eleme ...

  8. C++STL手写版

    手写STL,卡常专用. node为变量类型,可以自由定义,以下不再赘述. 1.stack(栈) 开一个数组,和一个top指针,压栈时++,弹栈时--即可. struct stack{ int tp;n ...

  9. 三、angular7登录请求和路由带参传递

    在 app.module.ts 中引入 HttpClientModule 并注入 import {HttpClientModule} from '@angular/common/http'; impo ...

  10. 在github pages网站下用jekyll制作博客教程

    https://www.jekyll.com.cn/ https://github.com/onevcat/vno-jekyll https://help.github.com/articles/us ...