<?php
/**
* 获取小程序二维码
*/
class getMiniQrcode { public $db = ''; public function __construct() {
$this->db = mysqli_connect('', '', '', '');
} public function index($id = 0) {
if (!$id) {
echo 'no input ID';
return;
}
$res = mysqli_query($this->db, "select id,command,device_num from sys_equipment where id=" . $id);
$res = mysqli_fetch_assoc($res);
$ARRAY = [];
$ARRAY[] = $res;
// 获取token
$ACCESS_TOKEN = $this->getAccesstoken();
// 准备进入小程序的参数
$color = [
'r' => 0,
'g' => 0,
'b' => 0,
];
foreach ($ARRAY as $key => $value) {
$param = [
'path' => "pages/shop/shop?mac=" . $value['command'],
'auto_color' => false,
'line_color' => $color,
'is_hyaline' => false,
'width' => 1280,
];
// 请求微信生成二维码接口
$request_url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" . $ACCESS_TOKEN;
$result = $this->httpRequest($request_url, json_encode($param), "POST"); $myPic = 'http://www.yoursite.com/path/to/logo.jpg';
// 生成圆形自定义图片
$logo = $this->yuanImg($myPic);
//二维码与自定义图片结合
$sharePic = $this->qrcodeWithLogo($result, $logo); // 准备文件名
// $filename = date('YmdHis') . md5(time() . mt_rand(10, 99)) . '.png';
$filename = $value['id'] . '_small.png';
$filepath = "./" . $filename;
// 将二进制图片写入文件
if (@$fp = fopen($filepath, 'w+')) {
fwrite($fp, $sharePic);
fclose($fp);
} if (file_exists($filepath)) {
echo 'success';
// 这里的background.png为比二维码大的一张白色图片, 多出的空白的地方,用来写文字
$background = imagecreatefrompng('http://www.yoursite.com/path/to/background.png');
$img = imagecreatefrompng('./' . $filename);
// 合并
imagecopy($background, $img, 0, 0, 0, 0, 1280, 1280);
// 字体文件包
$font = './font.ttf';
$color = imagecolorallocate($background, 0, 0, 0);
// 要写入的文本
$text = 'SN: ' . $value['device_num'];
imagettftext($background, 60, 0, 170, 1350, $color, $font, $text); //生成图像
imagepng($background);
imagepng($background, './' . $value['id'] . '.png');
}
}
} /**
* [获取AccessToken]
* @return [type] [description]
*/
public function getAccesstoken() {
header('content-type:text/html;charset=utf-8');
//配置APPID、APPSECRET
$APPID = '';
$APPSECRET = '';
// 请求地址
$getTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$APPID&secret=$APPSECRET"; $ACCESS_TOKEN = ""; $jsonResult = $this->httpRequest($getTokenUrl);
$jsonResult = json_decode($jsonResult, true); $ACCESS_TOKEN = $jsonResult["access_token"];
return $ACCESS_TOKEN;
}
/**
* 剪切图片为圆形
* @param $picture 图片数据流 比如file_get_contents(imageurl)返回的东东
* @return 图片数据流
*/
public function yuanImg($picture) {
$src_img = imagecreatefromstring(file_get_contents($picture));
$w = imagesx($src_img);
$h = imagesy($src_img);
$w = min($w, $h);
$h = $w;
$img = imagecreatetruecolor($w, $h);
//这一句一定要有
imagesavealpha($img, true);
//拾取一个完全透明的颜色,最后一个参数127为全透明
$bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
imagefill($img, 0, 0, $bg);
$r = $w / 2; //圆半径
$y_x = $r; //圆心X坐标
$y_y = $r; //圆心Y坐标
for ($x = 0; $x < $w; $x++) {
for ($y = 0; $y < $h; $y++) {
$rgbColor = imagecolorat($src_img, $x, $y);
if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
}
}
/**
* 如果想要直接输出图片,应该先设header。header("Content-Type: image/png; charset=utf-8");
* 并且去掉缓存区函数
*/
//获取输出缓存,否则imagepng会把图片输出到浏览器
ob_start();
imagepng($img);
imagedestroy($img);
$contents = ob_get_contents();
ob_end_clean();
return $contents;
} /**
* 在二维码的中间区域镶嵌图片
* @param $QR 二维码数据流。比如file_get_contents(imageurl)返回的东东,或者微信给返回的东东
* @param $logo 中间显示图片的数据流。比如file_get_contents(imageurl)返回的东东
* @return 返回图片数据流
*/
public function qrcodeWithLogo($QR, $logo) {
$QR = imagecreatefromstring($QR);
$logo = imagecreatefromstring($logo);
$QR_width = imagesx($QR); //二维码图片宽度
$QR_height = imagesy($QR); //二维码图片高度
$logo_width = imagesx($logo); //logo图片宽度
$logo_height = imagesy($logo); //logo图片高度
$logo_qr_width = $QR_width / 2.2; //组合之后logo的宽度(占二维码的1/2.2)
$scale = $logo_width / $logo_qr_width; //logo的宽度缩放比(本身宽度/组合后的宽度)
$logo_qr_height = $logo_height / $scale; //组合之后logo的高度
$from_width = ($QR_width - $logo_qr_width) / 2; //组合之后logo左上角所在坐标点
/**
* 重新组合图片并调整大小
* imagecopyresampled() 将一幅图像(源图象)中的一块正方形区域拷贝到另一个图像中
*/
imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
/**
* 如果想要直接输出图片,应该先设header。header("Content-Type: image/png; charset=utf-8");
* 并且去掉缓存区函数
*/
//获取输出缓存,否则imagepng会把图片输出到浏览器
ob_start();
imagepng($QR);
imagedestroy($QR);
imagedestroy($logo);
$contents = ob_get_contents();
ob_end_clean();
return $contents;
} /**
* curl 请求
* @param [type] $url [请求地址]
* @param string $data [参数]
* @param string $method [请求方式]
* @return [type] [description]
*/
public function httpRequest($url, $data = '', $method = 'GET') {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
if ($method == 'POST') {
curl_setopt($curl, CURLOPT_POST, 1);
if ($data != '') {
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
} curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
}
date_default_timezone_set('PRC');
$a = new getMiniQrcode();
$id = $_GET['id'];
$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. 【JZOJ6236】【20190628】启程的日子

    题目 给你一个\(n \times m\)的01矩阵 你需要用一些矩阵加减出这个矩阵 求最少的步数,并输出方案 需要满足构造出的01矩阵是一个四联通块 $ n ,  m \le 500 $ 题解 答案 ...

  2. 【JZOJ100207】【20190705】决心

    题目 你需要构造一个排列 初始时\(p_i=i\),一次操作定义为: 选择一些\((x_i,y_i)\),满足每个数字只能出现一次 依次交换\(p_{x_i},p_{y_i}\) 定义一个排列 \(P ...

  3. ZROI 暑期高端峰会 A班 Day4 生成函数

    一般生成函数 很普及组,不讲了 生成函数是一种形式幂级数,也就是我们只关心系数,不关心未知数具体的值. 比如 \(\sum\limits_{i\ge 0}x^i=\frac{1}{1-x}\).虽然只 ...

  4. Java 并发系列之六:java 并发容器(4个)

    1. ConcurrentHashMap 2. ConcurrentLinkedQueue 3. ConcurrentSkipListMap 4. ConcurrentSkipListSet 5. t ...

  5. 灵魂拷问:Java对象的内存分配过程是如何保证线程安全的?(阿里面试)

    JVM内存结构,是很重要的知识,相信每一个静心准备过面试的程序员都可以清楚的把堆.栈.方法区等介绍的比较清楚. 上图,是一张在作者根据<Java虚拟机规范(Java SE 8)>中描述的J ...

  6. Python爬虫爬取BT之家找电影资源

    一.写在前面 最近看新闻说圣城家园(SCG)倒了,之前BT天堂倒了,暴风影音也不行了,可以说看个电影越来越费力,国内大厂如企鹅和爱奇艺最近也出现一些幺蛾子,虽然目前版权意识虽然越来越强,但是很多资源在 ...

  7. 理解UnrealBuildTool

    转自:https://zhuanlan.zhihu.com/p/57186557 介绍 虚幻引擎是当前比较流行的游戏开发引擎之一,许多流行的游戏都是虚幻引擎开发的. 然而“引擎”这个词在行业中的定义比 ...

  8. [记录]mscorlib recursive resource lookup bug解决方法

    [Content]Expression: [mscorlib recursive resource lookup bug]Description: Infinite recursion during ...

  9. html5点击input没有出现光标完美解决方案

    html5点击input没有出现光标完美解决方案 <pre> <input type="text" placeholder="输入姓名" cl ...

  10. mapreduce 变量共享

    mapreduce 全局变量共享 在编写MapReduce程序时,经常会遇到这样的问题,全局变量如何保存?如何让每个处理都能获取保存的这些全局变量?使用全局变量是不可避免的,但是 在MapRdeuce ...