1. <?php
  2. /**
  3. * 获取小程序二维码
  4. */
  5. class getMiniQrcode {
  6.  
  7. public $db = '';
  8.  
  9. public function __construct() {
  10. $this->db = mysqli_connect('', '', '', '');
  11. }
  12.  
  13. public function index($id = 0) {
  14. if (!$id) {
  15. echo 'no input ID';
  16. return;
  17. }
  18. $res = mysqli_query($this->db, "select id,command,device_num from sys_equipment where id=" . $id);
  19. $res = mysqli_fetch_assoc($res);
  20. $ARRAY = [];
  21. $ARRAY[] = $res;
  22. // 获取token
  23. $ACCESS_TOKEN = $this->getAccesstoken();
  24. // 准备进入小程序的参数
  25. $color = [
  26. 'r' => 0,
  27. 'g' => 0,
  28. 'b' => 0,
  29. ];
  30. foreach ($ARRAY as $key => $value) {
  31. $param = [
  32. 'path' => "pages/shop/shop?mac=" . $value['command'],
  33. 'auto_color' => false,
  34. 'line_color' => $color,
  35. 'is_hyaline' => false,
  36. 'width' => 1280,
  37. ];
  38. // 请求微信生成二维码接口
  39. $request_url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" . $ACCESS_TOKEN;
  40. $result = $this->httpRequest($request_url, json_encode($param), "POST");
  41.  
  42. $myPic = 'http://www.yoursite.com/path/to/logo.jpg';
  43. // 生成圆形自定义图片
  44. $logo = $this->yuanImg($myPic);
  45. //二维码与自定义图片结合
  46. $sharePic = $this->qrcodeWithLogo($result, $logo);
  47.  
  48. // 准备文件名
  49. // $filename = date('YmdHis') . md5(time() . mt_rand(10, 99)) . '.png';
  50. $filename = $value['id'] . '_small.png';
  51. $filepath = "./" . $filename;
  52. // 将二进制图片写入文件
  53. if (@$fp = fopen($filepath, 'w+')) {
  54. fwrite($fp, $sharePic);
  55. fclose($fp);
  56. }
  57.  
  58. if (file_exists($filepath)) {
  59. echo 'success';
  60. // 这里的background.png为比二维码大的一张白色图片, 多出的空白的地方,用来写文字
  61. $background = imagecreatefrompng('http://www.yoursite.com/path/to/background.png');
  62. $img = imagecreatefrompng('./' . $filename);
  63. // 合并
  64. imagecopy($background, $img, 0, 0, 0, 0, 1280, 1280);
  65. // 字体文件包
  66. $font = './font.ttf';
  67. $color = imagecolorallocate($background, 0, 0, 0);
  68. // 要写入的文本
  69. $text = 'SN: ' . $value['device_num'];
  70. imagettftext($background, 60, 0, 170, 1350, $color, $font, $text);
  71.  
  72. //生成图像
  73. imagepng($background);
  74. imagepng($background, './' . $value['id'] . '.png');
  75. }
  76. }
  77. }
  78.  
  79. /**
  80. * [获取AccessToken]
  81. * @return [type] [description]
  82. */
  83. public function getAccesstoken() {
  84. header('content-type:text/html;charset=utf-8');
  85. //配置APPID、APPSECRET
  86. $APPID = '';
  87. $APPSECRET = '';
  88. // 请求地址
  89. $getTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$APPID&secret=$APPSECRET";
  90.  
  91. $ACCESS_TOKEN = "";
  92.  
  93. $jsonResult = $this->httpRequest($getTokenUrl);
  94. $jsonResult = json_decode($jsonResult, true);
  95.  
  96. $ACCESS_TOKEN = $jsonResult["access_token"];
  97. return $ACCESS_TOKEN;
  98. }
  99. /**
  100. * 剪切图片为圆形
  101. * @param $picture 图片数据流 比如file_get_contents(imageurl)返回的东东
  102. * @return 图片数据流
  103. */
  104. public function yuanImg($picture) {
  105. $src_img = imagecreatefromstring(file_get_contents($picture));
  106. $w = imagesx($src_img);
  107. $h = imagesy($src_img);
  108. $w = min($w, $h);
  109. $h = $w;
  110. $img = imagecreatetruecolor($w, $h);
  111. //这一句一定要有
  112. imagesavealpha($img, true);
  113. //拾取一个完全透明的颜色,最后一个参数127为全透明
  114. $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
  115. imagefill($img, 0, 0, $bg);
  116. $r = $w / 2; //圆半径
  117. $y_x = $r; //圆心X坐标
  118. $y_y = $r; //圆心Y坐标
  119. for ($x = 0; $x < $w; $x++) {
  120. for ($y = 0; $y < $h; $y++) {
  121. $rgbColor = imagecolorat($src_img, $x, $y);
  122. if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
  123. imagesetpixel($img, $x, $y, $rgbColor);
  124. }
  125. }
  126. }
  127. /**
  128. * 如果想要直接输出图片,应该先设header。header("Content-Type: image/png; charset=utf-8");
  129. * 并且去掉缓存区函数
  130. */
  131. //获取输出缓存,否则imagepng会把图片输出到浏览器
  132. ob_start();
  133. imagepng($img);
  134. imagedestroy($img);
  135. $contents = ob_get_contents();
  136. ob_end_clean();
  137. return $contents;
  138. }
  139.  
  140. /**
  141. * 在二维码的中间区域镶嵌图片
  142. * @param $QR 二维码数据流。比如file_get_contents(imageurl)返回的东东,或者微信给返回的东东
  143. * @param $logo 中间显示图片的数据流。比如file_get_contents(imageurl)返回的东东
  144. * @return 返回图片数据流
  145. */
  146. public function qrcodeWithLogo($QR, $logo) {
  147. $QR = imagecreatefromstring($QR);
  148. $logo = imagecreatefromstring($logo);
  149. $QR_width = imagesx($QR); //二维码图片宽度
  150. $QR_height = imagesy($QR); //二维码图片高度
  151. $logo_width = imagesx($logo); //logo图片宽度
  152. $logo_height = imagesy($logo); //logo图片高度
  153. $logo_qr_width = $QR_width / 2.2; //组合之后logo的宽度(占二维码的1/2.2)
  154. $scale = $logo_width / $logo_qr_width; //logo的宽度缩放比(本身宽度/组合后的宽度)
  155. $logo_qr_height = $logo_height / $scale; //组合之后logo的高度
  156. $from_width = ($QR_width - $logo_qr_width) / 2; //组合之后logo左上角所在坐标点
  157. /**
  158. * 重新组合图片并调整大小
  159. * imagecopyresampled() 将一幅图像(源图象)中的一块正方形区域拷贝到另一个图像中
  160. */
  161. imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
  162. /**
  163. * 如果想要直接输出图片,应该先设header。header("Content-Type: image/png; charset=utf-8");
  164. * 并且去掉缓存区函数
  165. */
  166. //获取输出缓存,否则imagepng会把图片输出到浏览器
  167. ob_start();
  168. imagepng($QR);
  169. imagedestroy($QR);
  170. imagedestroy($logo);
  171. $contents = ob_get_contents();
  172. ob_end_clean();
  173. return $contents;
  174. }
  175.  
  176. /**
  177. * curl 请求
  178. * @param [type] $url [请求地址]
  179. * @param string $data [参数]
  180. * @param string $method [请求方式]
  181. * @return [type] [description]
  182. */
  183. public function httpRequest($url, $data = '', $method = 'GET') {
  184. $curl = curl_init();
  185. curl_setopt($curl, CURLOPT_URL, $url);
  186. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  187. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
  188. curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  189. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  190. curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  191. if ($method == 'POST') {
  192. curl_setopt($curl, CURLOPT_POST, 1);
  193. if ($data != '') {
  194. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  195. }
  196. }
  197.  
  198. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  199. curl_setopt($curl, CURLOPT_HEADER, 0);
  200. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  201. $result = curl_exec($curl);
  202. curl_close($curl);
  203. return $result;
  204. }
  205. }
  206. date_default_timezone_set('PRC');
  207. $a = new getMiniQrcode();
  208. $id = $_GET['id'];
  209. $a->index($id);

访问该PHP文件  传入 ?id=...

1

生成小程序菊花码(生成菊花码、更换中间logo、更改图片尺寸,加文字)的更多相关文章

  1. nodejs + 小程序云函数 生成小程序码

    前言:这个东西坑死我了 业务需求要生成小程序码 然后我找了两天的资料 运行 生成一堆的乱码 死活就是不能生成 最后看了一遍博客 套用了一下 自己又简单的改了一下  nodejs 我是刚刚接触  有很多 ...

  2. 微信小程序获取Access_token和页面URL生成小程序码或二维码

    1.微信小程序获取Access_token: access_token具体时效看官方文档. using System; using System.Collections.Generic; using ...

  3. 微信小程序条码、二维码生成模块

    代码地址如下:http://www.demodashi.com/demo/13994.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...

  4. PHP生成小程序二维码

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

  5. C# 生成小程序码

    /// <summary> /// B接口-微信小程序带参数二维码的生成 /// </summary> /// <param name="access_toke ...

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

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

  7. 基于olami开放语义平台的微信小程序遥知之源码实现

    概述 实现一个智能生活信息查询的小秘书功能,支持查天气.新闻.日历.汇率.笑话.故事.百科.诗词.邮编.区号.菜谱.股票.节目预告,还支持闲聊.算24点.数学计算.单位换算.购物.搜索等功能. 使用方 ...

  8. 微信小程序,获取二维码

    微信小程序,获取二维码 找到一篇很实用的博客,他已经写得很详细了,自己也懒得写,亲测有效 参考网址

  9. PHP生成小程序二维码合成图片生成文字

    这部分代码是写在项目上的代码,THINKPHP3.1如果迁移到其他的地方应该要稍稍改动一下以适合自己的项目 function get_bbox($text,$fsize,$ffile){ return ...

随机推荐

  1. HashMap和HashTable本质性的区别

    一,HashMap 1.HashMap是键值对key-value形式双列集合.它的底层存储原理是哈希表. 2.对应HashMap采用哈希表存储键值对元素的方式. HashMap.put(key,val ...

  2. pyhton2 and python3 生成随机数字、字母、符号字典(用于撞库测试/验证码等)

    本文介绍Python3中String模块ascii_letters和digits方法,其中ascii_letters是生成所有字母,从a-z和A-Z,digits是生成所有数字0-9.string.p ...

  3. Koa帮我们做了什么

    整理web渲染思路,与KOA作比较 1.开启服务器并监听端口,注册监听事件 // 原生 let http = require('http') const server = http.createSer ...

  4. Python 绘制 柱状图

    用Python 绘制 柱状图,使用的是bar()函数. 一个简单的例子: # 创建一个点数为 8 x 6 的窗口, 并设置分辨率为 80像素/每英寸 plt.figure(figsize=(10, 1 ...

  5. C运算符优先级和结合性

    C中运算符优先级和结合性一览表: 在上表中能总结出一下规律: (1)结合方向只有三个是从右往左,其余都是从左往右: (2)逗号运算符的优先级最低: (3)对于优先级,有一个普遍规律:算术运算符 > ...

  6. linux中用一个.sh文件执行多个.sh文件

      建一个文件夹存放你自己的.sh文件(用命令行操作) 先进入到: cd usr/local/sbin 目录里面 然后再新建一个文件夹: sudo mkdir myshell 建一个文件夹专门存放自己 ...

  7. Python3+PyCryptodome实现各种加密算法教程

    一.说明 PyCryptodome是python一个强大的加密算法库,可以实现常见的单向加密.对称加密.非对称加密和流加密算法.直接pip安装即可: pip install pycryptodome ...

  8. 【译】RAID的概念和RAID对于SQL性能的影响

    简介 我们都听说过RAID,也经常作为SQL DBA.开发人员或构架师在工作中讨论RAID.但是,其实我们很多人都对RAID的原理,等级,以及RAID是如何影响SQL Server性能并不甚了解. 本 ...

  9. Docker 搭建简单 LVS

    LVS简介 LVS(Linux Virtual Server)即Linux虚拟服务器,是由章文嵩博士主导的开源负载均衡项目,目前LVS已经被集成到Linux内核模块中.该项目在Linux内核中实现了基 ...

  10. vs2017添加引用提示“找不到 Microsoft.VisualStudio.Shell.Interop.IVsReferenceManager 服务的实例”解决方案

    vs2017添加引用提示“找不到 Microsoft.VisualStudio.Shell.Interop.IVsReferenceManager 服务的实例” 不知道是不是安装时候的问题?解决方法: ...