PHP 图片+文字+二维码生成小程序分享海报
思路:
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 图片+文字+二维码生成小程序分享海报的更多相关文章
- 微信小程序扫描普通二维码打开小程序的方法
很久没有写博客了,之前换了一份工作,很久没有做Android开发了,现在转做前端开发了,记录一下遇到的问题及解决的方法. 最近做微信小程序开发,遇到一个需求,后台管理系统生成的问卷和投票会有一个二维码 ...
- 微信小程序 - 配置普通二维码跳小程序
普通二维码跳小程序规则: https://developers.weixin.qq.com/miniprogram/introduction/qrcode.html#%E5%8A%9F%E8%83%B ...
- 带logo图片或不带logo图片的二维码生成与解析,亲测成功
最近公司需要实现二维码功能,本人经过一顿百度,终于实现了,因有3个功能:不带logo图片.带logo图片.解析二维码,篇幅较长,请耐心读之,直接复制粘贴即可. 前提:myeclipse10:jar包: ...
- 微信小程序生成带参数的二维码(小程序码)独家asp.net的服务端c#完整代码
一)我先用的小程序端的wx.request去调用API,发现竟然是一个坑! wx.request({ url: 'https://api.weixin.qq.com/wxa/getwxacodeunl ...
- C# - VS2019调用ZXing.NET实现条码、二维码和带有Logo的二维码生成
前言 C# WinFrm程序调用ZXing.NET实现条码.二维码和带有Logo的二维码生成. ZXing.NET导入 GitHub开源库 ZXing.NET开源库githib下载地址:https:/ ...
- PHP批量生成底部带编号二维码(二维码生成+文字生成图片+图片拼接合并)
PHP批量生成带底部编号二维码(二维码生成+文字生成图片+图片拼接合并) 需求: 输入编号如 : cb05-0000001 至 cb05-0000500 批量生成 以编号为名称的下图二维码,然后压缩 ...
- .NET使用ZXing.NET生成中间带图片的二维码
很久之前就有写这样的代码了,只是一直没记录下来,偶然想写成博客. 把之前的代码封装成函数,以方便理解以及调用. 基于开源的 ZXing.NET 组件,代码如下: 先添加对ZXing.NET的引用,然后 ...
- 利用phpqrcode二维码生成类库和imagecopymerge函数制拼接图片的经验
前期准备 引入phpqrcode类库(下载地址:https://sourceforge.net/projects/phpqrcode/) PHP开启GD扩展库支持 1.利用phpqrcode生成二维码 ...
- java 二维码生成(可带图片)springboot版
本文(2019年6月29日 飞快的蜗牛博客) 有时候,男人和女人是两个完全不同的世界,男人的玩笑和女人的玩笑也完全是两码事,爱的人完全不了解你,你也不要指望一个女人了解你,所以男的不是要求别人怎么样, ...
随机推荐
- 解决Chrome浏览器无法自动播放音频视频的问题,Uncaught (in promise) DOMException
转载自:http://www.nooong.com/docs/chrome_video_autoplay.htm 在最新版的Chrome浏览器(以及所有以Chromium为内核的浏览器)中,已不再允许 ...
- TensorFlow机器学习实战指南之第二章2
TensorFlow实现反向传播 本节先举个简单的回归算法的例子.这里先举一个简单的例子,从均值1,标准差为0.1的正态分布中随机抽样100个数,然后乘以变量A,损失函数L2正则函数,也就是实现函数X ...
- java File获取字节流
/*文件64位编码*/ public static void main(String[] args) { byte[] fileByte = toByteArray(newFile); String ...
- Zookeeper学习笔记(上)
Zookeeper学习笔记 本篇主要是一些基本的介绍和API的使用介绍, 有些只是记录了知识点,而没有完全在笔记中详细解释, 需要自行查找资料补充相关概念 主要参考了课程中的内容: Zookeeper ...
- node项目实战-用node-koa2-mysql-bootstrap搭建一个前端论坛
前言 在学习了koa2和express并写了一些demo后,打算自己写一个项目练练手,由于是在校生,没什么好的项目做,即以开发一个前端论坛为目标,功能需求参照一下一些社区拟定,主要有: 登录注册 个人 ...
- eclipse把函数内容折叠的方法
eclipse 将方法折叠要先启动折叠功能启用方法:Ctrl+ / (小键盘) 或者:右键点击行号左边的空白,弹出的选项中,选择“Folding”下的“Enable Folding”这样启动foldi ...
- vue组件学习(一)
1, vue中的 is 的用法,有时候我们需要把一个组件绑定到指定的标签下,比如把tr组件放到table下,直接这样写是不行的, <!DOCTYPE html> <html lang ...
- 【JavaScript】包装类
包装类 String().Number().Boolean() String() 可以将基本数据类型的字符串转换为String对象 var string = new String("hell ...
- table的td设置1px的方法,亲测有效
第一种方法: 1.将table的属性设置为:BORDER=0 .cellspacing=1 : 2.设置table的背景色为即你要设置的table的边框颜色: 3.设置所有td背景色为#ffffff白 ...
- HTML中的表单<form>标签
一.HTML表单 HTML 表单用于搜集不同类型的用户输入. HTML 表单包含表单元素,表单元素指的是不同类型的 input 元素.复选框.单选按钮.提交按钮等等. 关于表单的更多内容可以参考htt ...