思路:

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. UITableView 在没有数据时候分割线问题

    在iOS4.3中可以直接设置footer为nil,但是在5.0不行,因为UITableView会默认生成一个Footer. (详见iOS Release Notes中的说明:Returning nil ...

  2. AFNetworking2.0源码解析<二>

    本篇我们继续来看看AFNetworking的下一个模块 — AFURLRequestSerialization.   AFURLRequestSerialization用于帮助构建NSURLReque ...

  3. MySQL索引的分类、结构、使用场景

    MySQL索引分类 1.主键索引:设定为主键后数据库会自动建立索引,innodb为聚簇索引 语法: 随表一起建索引: CREATE TABLE customer (id INT(10) UNSIGNE ...

  4. Eclipse从远程仓库的工程克隆到本地仓库

    在Eclipse中,File→Import→Git→Projects from Git 点击Next→Clone URI Next,将工厂地址复制过来 Next,再点击Next, 点击Browse,选 ...

  5. python并发编程中的多进程(代码实现)

    一.multipricessing模块的介绍 python中的多线程无法利用多核优势,如果想要充分的使用多核CPU资源,在python中大部分情况下需要用多线程,python提供了multiproce ...

  6. NLP 中任务及相关概念

    命名实体识别 命名实体识别(Named Entity Recognition,简称NER),又称作"专名识别",是指识别文本中具有特定意义的实体,主要包括人名.地名.机构名.专有名 ...

  7. C#设置欢迎窗体由不透明变透明

    public Form1()        {            InitializeComponent();        } private bool isForm1 = true;  //设 ...

  8. php sockets扩展安装

    php sockets扩展安装   今天安装cacti发现需要php sockets扩展,而现在的lnmp没有安装,于是想到了phpzie工具安装扩展,安装方法如下: cd php-5.3.8/ext ...

  9. 快速幂(Fast Pow)

    定义 快速求a^b%c的算法 原理 指数可以被二进制分解 那么a^b可以分解为a^2^k1*a^2^k2*…… 又显然a^2^(k+1)=a^(2^k*2)=(a^2^k)^2 所以可以将指数在二进制 ...

  10. 关于Ubuntu 14.04 安装Oracle 11gR2安装步骤(从开始到放弃--最终使用docker获取)

    最近在复习Oracle的相关内容,好准备在下一份工作中能够熟练一些,所以准备在自己虚拟机中安装Oracle的11gR2版本,主要参考内容为: https://blog.csdn.net/qq_4025 ...