PHP生成小程序二维码合成图片生成文字
这部分代码是写在项目上的代码,THINKPHP3.1如果迁移到其他的地方应该要稍稍改动一下以适合自己的项目
function get_bbox($text,$fsize,$ffile){
return imagettfbbox($fsize, 0, $ffile, $text);
}
function text_height ($text ,$fsize,$ffile) {
$box = $this->get_bbox($text,$fsize,$ffile);
$height = $box[3] - $box[5];
return $height;
}
//上传到aliyun
function uploadtooss($bucket, $localfilepath, $objname) {
$uploadoss = new Aliyunoss($bucket);
return $uploadoss->upload_by_file($objname, $localfilepath);
}
function getalluser(){
$data=M('kuaidati_user')->order('id desc')->select();
dump($data);
}
function ifexsitobj($bucket, $objname) {
$uploadoss = new Aliyunoss($bucket);
return $uploadoss->is_object_exist($objname);
}
function autowrap($fontsize, $angle, $fontface, $string, $width) {
// 这几个变量分别是 字体大小, 角度, 字体名称, 字符串, 预设宽度
$content = "";
// 将字符串拆分成一个个单字 保存到数组 letter 中
for ($i=0;$i<mb_strlen($string,'utf8');$i++) {
$letter[] = mb_substr($string, $i, 1,'utf-8');
}
foreach ($letter as $l) {
$teststr = $content." ".$l;
$testbox = imagettfbbox($fontsize, $angle, $fontface, $teststr);//这个里面是磅值
// 判断拼接后的字符串是否超过预设的宽度
if ((($testbox[2]-$testbox[0]) > $width) && ($content !== "")) {
$content .= "\n";
}
$content .= $l;
}
return $content;
}
function api_notice_increment($url, $data){
$ch = curl_init();
$header = "Accept-Charset: utf-8";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($url, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOdCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);
if (curl_errno($ch)) {
return false;
}else{
return $tmpInfo;
}
}
public function get_random_key($len=8) {
$key = "";
$pattern = '1234567890abcdefghijklmnopqrstuvwxyz';
for ($i = 0; $i < $len; $i++) {
$key.= $pattern{mt_rand(0, 35)}; //生成php随机数
}
return $key;
}
public function circle($url,$path){
$w = 430; $h=430; // original size
$dest_path=$path;
$src = imagecreatefromstring($url);
$newpic = imagecreatetruecolor($w,$h);
imagealphablending($newpic,false);
$transparent = imagecolorallocatealpha($newpic, 233, 40, 59, 0);
$r=$w/2;
for($x=0;$x<$w;$x++)
for($y=0;$y<$h;$y++){
$c = imagecolorat($src,$x,$y);
$_x = $x - $w/2;
$_y = $y - $h/2;
if((($_x*$_x) + ($_y*$_y)) < ($r*$r)){
imagesetpixel($newpic,$x,$y,$c);
}else{
imagesetpixel($newpic,$x,$y,$transparent);
}
}
imagesavealpha($newpic, true);
// header('Content-Type: image/png');
imagepng($newpic, $dest_path);
imagedestroy($newpic);
imagedestroy($src);
unlink($url);
// return $dest_path;
}
public function getimage(){
$id=145;
$Qsubject="测试PHP在图片上面文字生成";
$answer=array(
array('answer_key'=>'测试选项1'),
array('answer_key'=>'测试选项2'),
array('answer_key'=>'测试选项3'),
array('answer_key'=>'测试选项4')
);
$customermessage=new WechatSendCustomerMessage("VQSKmPFVS1532925182");
//内部方法获取微信的access_token
$access_token=$customermessage->get_token();
//获取二维码
$path="pages/userceter/userceter?id=".$id;
$width=430;
$post_data='{"path":"'.$path.'","width":'.$width.'}';
//$url="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$access_token;
//获取小程序码
$url="https://api.weixin.qq.com/wxa/getwxacode?access_token=".$access_token;
$result=$this->api_notice_increment($url,$post_data);
if($result){
//服务器上面的路径
$returnpath = dirname(__FILE__) . '/' . '../../../../wemarkdata/updatedata/tmp/';
$itype = 'jpg';
$filename = time() . $this->get_random_key(8) . '.' . $itype;
$filepath = $returnpath . $filename; //文件路径
$urlprefix = 'xcximg.xxx.cn'; //域名
$bucket = 'cardzan'; //阿里云的bucket
// $file_name = time() . $this->get_random_key(8) . '.png';
if (!file_exists($filepath)) {
$this->circle($result,$filepath); //生成圆形的二维码图片,放在服务器上面
$bg2="https://www.xxx.cn/themes/Xcxdati/bg2.png"; //背景图片路径
$a="https://www.xxx.cn/themes/Xcxdati/a.png";
$b="https://www.xxx.cn/themes/Xcxdati/b.png";
$c="https://www.xxx.cn/themes/Xcxdati/c.png";
$d="https://www.xxx.cn/themes/Xcxdati/d.png";
$txturl="http://test.xxx.cn/themes/Xcxdati/txt.png"; //文字图片路径
$txt = imagecreatefromstring(file_get_contents($txturl));
$answeroption=array(
imagecreatefromstring(file_get_contents($a)),
imagecreatefromstring(file_get_contents($b)),
imagecreatefromstring(file_get_contents($c)),
imagecreatefromstring(file_get_contents($d))
);
$tempfilename=time().$this->get_random_key(8).'.png';
$tempurl = $returnpath.$tempfilename;
$BGT = imagecreatefromstring(file_get_contents($bg2));
$code = imagecreatefromstring(file_get_contents($filepath));
//注意字体文件的位置,自己文件路径错误文字显示不出来
$font = dirname(__FILE__)."/./../Xcxdati/fonts/msyh.ttc"; // 字体文件
$color = imagecolorallocate($BGT,102,102,102); // 文字颜色
$content=$this->autowrap(28,0,$font,$Qsubject,620);
//autowrap($fontsize, $angle, $fontface, $string, $width)
// 这几个变量分别是 字体大小, 角度, 字体名称, 字符串, 预设宽度
$posY = $this->text_height($content,28,$font); //问题的高度
imagettftext($BGT,28,0,70,300,$color,$font,$content); // 创建文字
$posY+=300;
for($k=0;$k<count($answer);$k++){
$text = $this->autowrap(28,0,$font,$answer[$k]['answer_key'],620);
$posY =$posY+$this->text_height($answer[$k]['answer_key'],28,$font)+40;
imagettftext($BGT,28,0,130,$posY,$color,$font,$text); // 创建文字
imagecopyresampled($BGT, $answeroption[$k], 70,($posY-35), 0, 0,42,42,42,42);
}
$logo_width = imagesx($code);//二维码图片宽度
$logo_height = imagesy($code);//二维码图片高度
imagecopyresampled($BGT, $code, 290,1030, 0, 0,170,170,$logo_width,$logo_height);
imagecopyresampled($BGT, $txt, 160,1245, 0, 0,418,37,836,74);
imagepng($BGT, $tempurl); //合成图片
$filename = time() . $this->get_random_key(8) . '.png';
$ossfilename = 'xcximg/pic/' . $filename;
$status = $this->uploadtooss($bucket, $tempurl, $ossfilename); //上传到阿里云
$src= 'https://'.$urlprefix .'/'.$ossfilename;
if($status){
// $res['status']=1;
// $res['data']=array('src'=>$src);
// echo json_encode($res);
echo '<img src="'.$src.'">';
}
}else{
echo 0;
}
}else{
echo 0;
}
}
PHP生成小程序二维码合成图片生成文字的更多相关文章
- PHP生成小程序二维码
/** * [生成小程序二维码] * @return [type] [description] */ public function makeMiniQrcode_do() { begin: $id ...
- node 无脑生成小程序二维码图
RT 新建createwxaqrcode.js: const request = require('request') const fs = require('fs') // eg:生成购物车列表圆形 ...
- Java 获取微信小程序二维码(可以指定小程序页面 与 动态参数)
一.准备工作 微信公众平台接口调试工具 小程序的唯一标识(appid) 小程序的密钥(secret) 二.获取access_token 打开微信公众平台接口调试工具,在参数列表中输入小程序的appid ...
- .NET生成小程序码,并合自定义背景图生成推广小程序二维码
前言: 对于小程序大家可能都非常熟悉了,随着小程序的不断普及越来越多的公司都开始推广使用起来了.今天接到一个需求就是生成小程序码,并且于运营给的推广图片合并在一起做成一张漂亮美观的推广二维码,扫码这种 ...
- 小程序二维码生成接口API
获取小程序码 我们推荐生成并使用小程序码,它具有更好的辨识度.目前有两个接口可以生成小程序码,开发者可以根据自己的需要选择合适的接口. 接口A: 适用于需要的码数量较少的业务场景 接口地址: http ...
- php生成微信小程序二维码源码
目前有3个接口可以生成小程序码,开发者可以根据自己的需要选择合适的接口.第一步:获取 access_token public function getWxAccessToken(){ $appid ...
- 微信小程序二维码推广统计
微信小程序可以通过生成带参数的二维码,那么这个参数是可以通过APP的页面进行监控的 这样就可以统计每个二维码的推广效果. 今天由好推二维码推出的小程序统计工具HotApp小程序统计也推出了带参数二维码 ...
- 微信小程序-二维码汇总
小程序二维码在生活中的应用场景很多,比如营销类一物一码,扫码开门,扫码付款等...小程序二维码分两种? 1.普通链接二维码 即跟普通的网站链接生成的二维码是一个意思,这种二维码的局限性如下: 对于普通 ...
- 图片的URL上传至阿里云OSS操作(微信小程序二维码返回的二进制上传到OSS)
当我们从网络中获取一个URL的图片我们要存储到本地或者是私有的云时,我们可以这样操作 把url中的图片文件下载到本地(或者上传到私有云中) public String uploadUrlToOss ...
随机推荐
- Connector for Python
连接mysql, 需要mysql connector, conntector是一种驱动程序,python连接mysql的驱动程序,mysql官方给出的名称为connector/python, 可参考m ...
- Oracle GoldenGate 18.1发布
软件下载地址:https://www.oracle.com/technetwork/middleware/goldengate/downloads/index.html 文档下载地址: https:/ ...
- Linux error numbers
Linux error numbers, straight from the horse's mouth. #define EPERM 1 /* Operation not permitted */ ...
- iOS绘制坐标图,折线图-Swift
坐标图,经常会在各种各样的App中使用,最常用的一种坐标图就是折线图,根据给定的点绘制出对应的坐标图是最基本的需求.由于本人的项目需要使用折线图,第一反应就是搜索已经存在的解决方案,因为这种需求应该很 ...
- 踩坑之路---JWT验证
使用JWT验证客户的携带的token 客户端在请求接口时,需要在request的head中携带一个token令牌 服务器拿到这个token解析获取用户资源,这里的资源是非重要的用户信息 目前我的理解, ...
- 第二课丶pygame
学号:2017*****1024 姓名:王劲松 我的码云贪吃蛇项目仓库:https://gitee.com/Danieljs/sesnake 分析游戏中的备注和问题:10分钟 游戏名称.分数改动:3分 ...
- javascript 之 第七章第一节(递归)
先举例: function factorial(num) { ) { return num; } else { ); } } //输出120 //进一步去思考有名字的函数,且名字不会有变化的情况下,这 ...
- P3224 [HNOI2012]永无乡
思路 平衡树+启发式合并 貌似也可以线段树合并 连边就是合并两个Treap,查询就是第k大 使用Treap,好写好调 代码 #include <cstdio> #include <a ...
- Java基础知识盘点(一)- 基础篇
基本功 面向对象特征 封装.继承.多态和抽象 1.封装:给对象提供了隐藏内部特性和行为的能力.对象提供一些能被其他对象访问的方法,来改变它内部的数据. 在Java中,其访问权限有3种修饰符:publi ...
- sourcetree,创建工作流报错:Fatal: Not a gitflow-enabled repo yet. Please run 'git flow init' first.-》解决办法
1.打开项目下.git/config文件,或者如下图操作: 2.打开config文件以后,删除所有 [gitflow *条目并保存文件 3.关闭并重新打开sourcetree 4.仓库->Git ...