这部分代码是写在项目上的代码,THINKPHP3.1如果迁移到其他的地方应该要稍稍改动一下以适合自己的项目

  1. function get_bbox($text,$fsize,$ffile){
  2. return imagettfbbox($fsize, 0, $ffile, $text);
  3. }
  4.  
  5. function text_height ($text ,$fsize,$ffile) {
  6. $box = $this->get_bbox($text,$fsize,$ffile);
  7. $height = $box[3] - $box[5];
  8. return $height;
  9. }
  10.  
  11. //上传到aliyun
  12. function uploadtooss($bucket, $localfilepath, $objname) {
  13. $uploadoss = new Aliyunoss($bucket);
  14. return $uploadoss->upload_by_file($objname, $localfilepath);
  15. }
  16.  
  17. function getalluser(){
  18.  
  19. $data=M('kuaidati_user')->order('id desc')->select();
  20. dump($data);
  21. }
  22.  
  23. function ifexsitobj($bucket, $objname) {
  24. $uploadoss = new Aliyunoss($bucket);
  25. return $uploadoss->is_object_exist($objname);
  26. }
  27.  
  28. function autowrap($fontsize, $angle, $fontface, $string, $width) {
  29. // 这几个变量分别是 字体大小, 角度, 字体名称, 字符串, 预设宽度
  30. $content = "";
  31.  
  32. // 将字符串拆分成一个个单字 保存到数组 letter 中
  33. for ($i=0;$i<mb_strlen($string,'utf8');$i++) {
  34. $letter[] = mb_substr($string, $i, 1,'utf-8');
  35. }
  36. foreach ($letter as $l) {
  37.  
  38. $teststr = $content." ".$l;
  39. $testbox = imagettfbbox($fontsize, $angle, $fontface, $teststr);//这个里面是磅值
  40. // 判断拼接后的字符串是否超过预设的宽度
  41. if ((($testbox[2]-$testbox[0]) > $width) && ($content !== "")) {
  42. $content .= "\n";
  43.  
  44. }
  45. $content .= $l;
  46. }
  47.  
  48. return $content;
  49. }
  50.  
  51. function api_notice_increment($url, $data){
  52. $ch = curl_init();
  53. $header = "Accept-Charset: utf-8";
  54. curl_setopt($ch, CURLOPT_URL, $url);
  55. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  56. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  57. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  58. curl_setopt($url, CURLOPT_HTTPHEADER, $header);
  59. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  60. curl_setopt($ch, CURLOPT_FOLLOWLOdCATION, 1);
  61. curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  62. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  63. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  64. $tmpInfo = curl_exec($ch);
  65.  
  66. if (curl_errno($ch)) {
  67. return false;
  68. }else{
  69. return $tmpInfo;
  70. }
  71. }
  72.  
  73. public function get_random_key($len=8) {
  74.  
  75. $key = "";
  76. $pattern = '1234567890abcdefghijklmnopqrstuvwxyz';
  77. for ($i = 0; $i < $len; $i++) {
  78. $key.= $pattern{mt_rand(0, 35)}; //生成php随机数
  79. }
  80. return $key;
  81. }
  82.  
  83. public function circle($url,$path){
  84. $w = 430; $h=430; // original size
  85.  
  86. $dest_path=$path;
  87.  
  88. $src = imagecreatefromstring($url);
  89.  
  90. $newpic = imagecreatetruecolor($w,$h);
  91. imagealphablending($newpic,false);
  92. $transparent = imagecolorallocatealpha($newpic, 233, 40, 59, 0);
  93. $r=$w/2;
  94. for($x=0;$x<$w;$x++)
  95. for($y=0;$y<$h;$y++){
  96. $c = imagecolorat($src,$x,$y);
  97. $_x = $x - $w/2;
  98. $_y = $y - $h/2;
  99. if((($_x*$_x) + ($_y*$_y)) < ($r*$r)){
  100. imagesetpixel($newpic,$x,$y,$c);
  101. }else{
  102. imagesetpixel($newpic,$x,$y,$transparent);
  103. }
  104. }
  105. imagesavealpha($newpic, true);
  106. // header('Content-Type: image/png');
  107. imagepng($newpic, $dest_path);
  108. imagedestroy($newpic);
  109. imagedestroy($src);
  110. unlink($url);
  111. // return $dest_path;
  112.  
  113. }
  114.  
  115. public function getimage(){
  116.  
  117. $id=145;
  118.  
  119. $Qsubject="测试PHP在图片上面文字生成";
  120.  
  121. $answer=array(
  122. array('answer_key'=>'测试选项1'),
  123. array('answer_key'=>'测试选项2'),
  124. array('answer_key'=>'测试选项3'),
  125. array('answer_key'=>'测试选项4')
  126. );
  127.  
  128. $customermessage=new WechatSendCustomerMessage("VQSKmPFVS1532925182");
  129. //内部方法获取微信的access_token
  130.  
  131. $access_token=$customermessage->get_token();
  132.  
  133. //获取二维码
  134.  
  135. $path="pages/userceter/userceter?id=".$id;
  136.  
  137. $width=430;
  138.  
  139. $post_data='{"path":"'.$path.'","width":'.$width.'}';
  140.  
  141. //$url="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$access_token;
  142.  
  143. //获取小程序码
  144. $url="https://api.weixin.qq.com/wxa/getwxacode?access_token=".$access_token;
  145.  
  146. $result=$this->api_notice_increment($url,$post_data);
  147.  
  148. if($result){
  149. //服务器上面的路径
  150. $returnpath = dirname(__FILE__) . '/' . '../../../../wemarkdata/updatedata/tmp/';
  151.  
  152. $itype = 'jpg';
  153.  
  154. $filename = time() . $this->get_random_key(8) . '.' . $itype;
  155.  
  156. $filepath = $returnpath . $filename; //文件路径
  157.  
  158. $urlprefix = 'xcximg.xxx.cn'; //域名
  159.  
  160. $bucket = 'cardzan'; //阿里云的bucket
  161.  
  162. // $file_name = time() . $this->get_random_key(8) . '.png';
  163.  
  164. if (!file_exists($filepath)) {
  165.  
  166. $this->circle($result,$filepath); //生成圆形的二维码图片,放在服务器上面
  167.  
  168. $bg2="https://www.xxx.cn/themes/Xcxdati/bg2.png"; //背景图片路径
  169.  
  170. $a="https://www.xxx.cn/themes/Xcxdati/a.png";
  171. $b="https://www.xxx.cn/themes/Xcxdati/b.png";
  172. $c="https://www.xxx.cn/themes/Xcxdati/c.png";
  173. $d="https://www.xxx.cn/themes/Xcxdati/d.png";
  174.  
  175. $txturl="http://test.xxx.cn/themes/Xcxdati/txt.png"; //文字图片路径
  176.  
  177. $txt = imagecreatefromstring(file_get_contents($txturl));
  178.  
  179. $answeroption=array(
  180. imagecreatefromstring(file_get_contents($a)),
  181. imagecreatefromstring(file_get_contents($b)),
  182. imagecreatefromstring(file_get_contents($c)),
  183. imagecreatefromstring(file_get_contents($d))
  184. );
  185.  
  186. $tempfilename=time().$this->get_random_key(8).'.png';
  187.  
  188. $tempurl = $returnpath.$tempfilename;
  189.  
  190. $BGT = imagecreatefromstring(file_get_contents($bg2));
  191.  
  192. $code = imagecreatefromstring(file_get_contents($filepath));
  193.  
  194. //注意字体文件的位置,自己文件路径错误文字显示不出来
  195. $font = dirname(__FILE__)."/./../Xcxdati/fonts/msyh.ttc"; // 字体文件
  196.  
  197. $color = imagecolorallocate($BGT,102,102,102); // 文字颜色
  198.  
  199. $content=$this->autowrap(28,0,$font,$Qsubject,620);
  200.  
  201. //autowrap($fontsize, $angle, $fontface, $string, $width)
  202. // 这几个变量分别是 字体大小, 角度, 字体名称, 字符串, 预设宽度
  203.  
  204. $posY = $this->text_height($content,28,$font); //问题的高度
  205.  
  206. imagettftext($BGT,28,0,70,300,$color,$font,$content); // 创建文字
  207.  
  208. $posY+=300;
  209.  
  210. for($k=0;$k<count($answer);$k++){
  211.  
  212. $text = $this->autowrap(28,0,$font,$answer[$k]['answer_key'],620);
  213. $posY =$posY+$this->text_height($answer[$k]['answer_key'],28,$font)+40;
  214. imagettftext($BGT,28,0,130,$posY,$color,$font,$text); // 创建文字
  215. imagecopyresampled($BGT, $answeroption[$k], 70,($posY-35), 0, 0,42,42,42,42);
  216.  
  217. }
  218.  
  219. $logo_width = imagesx($code);//二维码图片宽度
  220.  
  221. $logo_height = imagesy($code);//二维码图片高度
  222.  
  223. imagecopyresampled($BGT, $code, 290,1030, 0, 0,170,170,$logo_width,$logo_height);
  224.  
  225. imagecopyresampled($BGT, $txt, 160,1245, 0, 0,418,37,836,74);
  226.  
  227. imagepng($BGT, $tempurl); //合成图片
  228.  
  229. $filename = time() . $this->get_random_key(8) . '.png';
  230.  
  231. $ossfilename = 'xcximg/pic/' . $filename;
  232.  
  233. $status = $this->uploadtooss($bucket, $tempurl, $ossfilename); //上传到阿里云
  234.  
  235. $src= 'https://'.$urlprefix .'/'.$ossfilename;
  236.  
  237. if($status){
  238.  
  239. // $res['status']=1;
  240. // $res['data']=array('src'=>$src);
  241. // echo json_encode($res);
  242.  
  243. echo '<img src="'.$src.'">';
  244. }
  245.  
  246. }else{
  247. echo 0;
  248. }
  249.  
  250. }else{
  251. echo 0;
  252. }
  253.  
  254. }

PHP生成小程序二维码合成图片生成文字的更多相关文章

  1. PHP生成小程序二维码

    /** * [生成小程序二维码] * @return [type] [description] */ public function makeMiniQrcode_do() { begin: $id ...

  2. node 无脑生成小程序二维码图

    RT 新建createwxaqrcode.js: const request = require('request') const fs = require('fs') // eg:生成购物车列表圆形 ...

  3. Java 获取微信小程序二维码(可以指定小程序页面 与 动态参数)

    一.准备工作 微信公众平台接口调试工具 小程序的唯一标识(appid) 小程序的密钥(secret) 二.获取access_token 打开微信公众平台接口调试工具,在参数列表中输入小程序的appid ...

  4. .NET生成小程序码,并合自定义背景图生成推广小程序二维码

    前言: 对于小程序大家可能都非常熟悉了,随着小程序的不断普及越来越多的公司都开始推广使用起来了.今天接到一个需求就是生成小程序码,并且于运营给的推广图片合并在一起做成一张漂亮美观的推广二维码,扫码这种 ...

  5. 小程序二维码生成接口API

    获取小程序码 我们推荐生成并使用小程序码,它具有更好的辨识度.目前有两个接口可以生成小程序码,开发者可以根据自己的需要选择合适的接口. 接口A: 适用于需要的码数量较少的业务场景 接口地址: http ...

  6. php生成微信小程序二维码源码

    目前有3个接口可以生成小程序码,开发者可以根据自己的需要选择合适的接口.第一步:获取   access_token public function getWxAccessToken(){ $appid ...

  7. 微信小程序二维码推广统计

    微信小程序可以通过生成带参数的二维码,那么这个参数是可以通过APP的页面进行监控的 这样就可以统计每个二维码的推广效果. 今天由好推二维码推出的小程序统计工具HotApp小程序统计也推出了带参数二维码 ...

  8. 微信小程序-二维码汇总

    小程序二维码在生活中的应用场景很多,比如营销类一物一码,扫码开门,扫码付款等...小程序二维码分两种? 1.普通链接二维码 即跟普通的网站链接生成的二维码是一个意思,这种二维码的局限性如下: 对于普通 ...

  9. 图片的URL上传至阿里云OSS操作(微信小程序二维码返回的二进制上传到OSS)

    当我们从网络中获取一个URL的图片我们要存储到本地或者是私有的云时,我们可以这样操作  把url中的图片文件下载到本地(或者上传到私有云中)  public String uploadUrlToOss ...

随机推荐

  1. css img 隐藏的边距

    因为图片存在浏览器默认的边距,正常的情况下,增加这样的属性来消除多余的边距或者多1px的情况 img { display: block; border: node; } 但是这样的话img在父元素里设 ...

  2. 为什么越来越多的人偏爱go语言

    如果你是一个开发者或者程序员,你大概应该听过Go语言或者Golang语言.当然,如果没有听过也没关系,看到这篇文章的同学,就说明你对Golang是关注的,只需要这一点就够了.今天来聊聊关于Golang ...

  3. 比较两个Date类型的数据相差几年

    package com.utils; import java.text.DecimalFormat; import java.text.ParseException; import java.text ...

  4. Linux学习笔记之Linux添加/删除用户和用户组

    本文总结了Linux添加或者删除用户和用户组时常用的一些命令和参数. 1.建用户: adduser phpq //新建phpq用户 passwd phpq //给phpq用户设置密码 2.建工作组 g ...

  5. 【mac微信小助手】WeChatPlugin使用教程!

    微信小助手 mac版集微信防撤回和微信多开等诸多功能于一身,可以有效的阻止朋友微信撤回消息,还能开启无手机验证登录,再也不用每次登录扫码验证啦,非常方便!   wechatplugin mac版安装教 ...

  6. vue 去中心化的路由拆分方案:require.context

    代码地址:https://github.com/lisiyizu/vue-router-dynamic

  7. 破解360doc个人图书馆网站的右键、复制方法

    chrome浏览器如下做法: 右上角菜单按钮→设置→显示高级设置→隐私设置下的 内容设置按钮→javascript下的管理例外情况→添加 [*.]360doc.com 设置为禁止 →完成

  8. Python3 tkinter基础 Scale orient 横竖 resolution单步步长 length 长度 tickinterval 指示刻度

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  9. restorecon【转】

    本文转载自:https://blog.csdn.net/sinat_36888624/article/details/6076650 estorecon命令用来恢复SELinux文件属性即恢复文件的安 ...

  10. java解压多层目录中多个压缩文件和处理压缩文件中有内层目录的情况

    代码: package com.xiaobai; import java.io.File; import java.io.FileOutputStream; import java.io.IOExce ...