当前位置:网站首页>Get the QR code of wechat applet with parameters - and share the source code of modifying the QR code logo
Get the QR code of wechat applet with parameters - and share the source code of modifying the QR code logo
2022-06-24 09:57:00 【BigChen_ up】
Specific methods used , It is recommended to look at the code first and then find the corresponding method from the directory
logic analysis :
1. First, prepare the applet APPID and SECRET, Available on wechat official account . And prepare the parameters you want to bring into the QR code
2.logo Upload : Receive the uploaded logo picture , First draw the picture into a circle , Then get the QR code , hold logo The picture is embedded on the QR code . See the code demonstration : The important way to use is to look at the table of contents
Method of obtaining QR code :
public function recreateOp(){
if (request()->ispost() && request()->isAjax()) {
$logo = request()->file('file'); // The uploaded logo
$width = input('width'); // Width
$shape = input('shape'); // QR code shape
if($width < 280 || $width > 1280){
return json_encode(['status'=>ERROR,'msg'=>' Wrong width ']);
}
if($shape != 1 && $shape != 2){
return json_encode(['status' => ERROR, 'msg' => ' Wrong type ']);
}
$appid = " Your own appid Find... In wechat official account ";
$appsecret = " Yours appsecret Find... In wechat official account ";
$usertoken = " You need to bring in the parameters in the QR code ";
if (!$usertoken) {
return json_encode(['status' => ERROR, 'msg' => ' The information is wrong ']);
}
$Applet = new Applet(); // Applet() class
if (!empty($logo)) {
$filename = uniqid('qrlogo_img_' . false);
$retImg = upload_files($logo, USER_QRLOGO_PATH, $filename); // Upload files
if ($retImg['status'] == 9) {
$head_img = USER_QRLOGO_PATH.$retImg['msg'];
} else {
return json_encode(['status' => 'error', 'msg' => $retImg['msg']]);
}
$logo = $Applet->yuanImg($head_img); // Upload logo Use PHP Self contained GD The library is drawn in a circle
@unlink($head_img); // Delete the uploaded logo picture
}
$Applet->creatCode($appid, $appsecret, $usertoken, $this->userinfo['uid'], $shape, $width,$logo);
return json_encode(['status' => SUCCESS, 'msg' => ' QR code has been regenerated ','url'=>url('jieru')]);
}
}
Applet() class :
/* * Generate an applet to access the exclusive QR code * $appid Applet appid * $appsecret Applet appsecret * $usertoken Access code * $shape QR code shape :1- Chrysanthemum type ,2- square * $width QR code width */
public function creatCode($appid, $appsecret, $usertoken, $uid, $shape = 1, $width=280,$logo = '') {
// obtain access_token
$access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
$json = $this->httpRequest($access_token);
$token = json_decode($json, true);
$token = $token['access_token'];
// Build request QR code parameters
switch ($shape) {
case 1: // Chrysanthemum type
$qcode = "https://api.weixin.qq.com/wxa/getwxacode?access_token=$token"; // Chrysanthemum QR code
break;
default: // Square type
$qcode = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=$token"; // Square QR code
break;
}
// Send the parameter combination in the QR code to the wechat interface
$param = json_encode(array("path" => "pages/login/login?tongxingma=$usertoken", "width" => $width));
// $res Is the acquired QR code , You can save the QR code to the database
$res = $this->httpRequest($qcode, $param, "POST"); // send out http Request for a QR code with parameters
if (!empty($logo)) {
$res = $this->qrcodeWithLogo($res,$logo,$shape); // Mosaic the picture in the middle of the QR code
}
// Save QR code picture
$file_name = time(). createStr(6) . ".png"; // Generate QR code picture name
$path = ROOT_PATH . 'public/uploads/appLetCode/'. $usertoken.'/'; // Save the path
$upload = $path . $file_name;
if (!file_exists($path)) mkdir($path, 0777, true);
file_put_contents($upload, $res);
// Save data to database
$olddata = model('Applet')->where(['uid' => $uid])->field('numid,name')->find();
if(!$olddata){
// The new data
$data = [
'uid' => $uid,
'name' => $file_name,
'width' => $width,
'shape' => $shape,
'addtime' => time(),
'status' => 9,
];
model('Applet')->allowField(true)->save($data);
}else{
// Update data
if (file_exists($path . $olddata['name'])) {
@unlink($path . $olddata['name']);
}
$data = [
'numid' => $olddata['numid'],
'name' => $file_name,
'width' => $width,
'shape' => $shape,
'addtime' => time(),
'status' => 9,
];
model('Applet')->allowField(true)->isUpdate(true)->save($data);
}
return [
// Turn into base64
'base64_image' => "data:image/jpeg;base64," . base64_encode($res),
'upload' => $file_name,
];
}
http Request method :
// Send the request to wechat server for QR code
private 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;
}
Cut Uploaded logo The picture is a circular method :
/** * Cut the picture into a circle * @param $picture Picture data stream such as file_get_contents(imageurl) Return to the East * @return Picture data stream */
public function yuanImg($picture) {
$picture = file_get_contents($picture);
$src_img = imagecreatefromstring($picture);
$w = imagesx($src_img);
$h = imagesy($src_img);
$w = min($w, $h);
$h = $w;
$img = imagecreatetruecolor($w, $h);
// This sentence must have
imagesavealpha($img, true);
// Pick a completely transparent color , Last parameter 127 For full transparency
$bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
imagefill($img, 0, 0, $bg);
$r = $w / 2; // Circle radius
$y_x = $r; // center of a circle X coordinate
$y_y = $r; // center of a circle Y coordinate
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);
}
}
}
/** * If you want to output pictures directly , It should be set first header.header("Content-Type: image/png; charset=utf-8"); * And remove the buffer function */
// Get output cache , otherwise imagepng Will output the picture to the browser
ob_start();
imagepng($img);
imagedestroy($img);
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
The method of mosaic picture in the middle area of QR code :
/** * Mosaic the picture in the middle of the QR code * @param $QR QR code data stream . such as file_get_contents(imageurl) Return to the East , Or wechat to the returned Dongdong * @param $logo The data flow of the picture in the middle . such as file_get_contents(imageurl) Return to the East * @return Return picture data stream */
public function qrcodeWithLogo($QR, $logo,$shape) {
$QR = imagecreatefromstring($QR);
$logo = imagecreatefromstring($logo);
$QR_width = imagesx($QR);// QR code image width
$QR_height = imagesy($QR);// QR code image height
$logo_width = imagesx($logo);//logo Image width
$logo_height = imagesy($logo);//logo Picture height
if ($shape == 1 ) {
$logo_qr_width = $QR_width / 2.2;// After the combination logo Width ( Take up... Of QR code 1/2.2)
} else {
$logo_qr_width = $QR_width / 4;// After the combination logo Width ( Take up... Of QR code 1/4)
}
$scale = $logo_width / $logo_qr_width;//logo Width scaling ratio ( The width of itself / Combined width )
$logo_qr_height = $logo_height / $scale;// After the combination logo Height
$from_width = ($QR_width - $logo_qr_width) / 2;// After the combination logo The coordinate point of the upper left corner
/** * Recombine the picture and resize * imagecopyresampled() Put an image ( Source image ) Copy a square area in to another image */
imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
/** * If you want to output pictures directly , It should be set first header.header("Content-Type: image/png; charset=utf-8"); * And remove the buffer function */
// Get output cache , otherwise imagepng Will output the picture to the browser
ob_start();
imagepng($QR);
imagedestroy($QR);
imagedestroy($logo);
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
边栏推荐
猜你喜欢

How to manage massive network infrastructure?

Geogebra instance clock

5分钟,客服聊天处理技巧,炉火纯青

PTA monkey chooses King (Joseph Ring problem)

记录一下MySql update会锁定哪些范围的数据

Reasons for the failure of digital transformation and the way to success

二叉树第一部分

In depth study paper reading target detection (VII) Chinese English Bilingual Edition: yolov4 optimal speed and accuracy of object detection

医学图像开源数据集汇总(二)

canvas 绘制图片
随机推荐
Reasons for the failure of digital transformation and the way to success
Is there a reliable and low commission futures account opening channel in China? Is it safe to open an account online?
Talking about the knowledge of digital transformation
Codeforces Round #392 (Div. 2) D. Ability To Convert
Oracle数据文件头SCN不一致处理方法
二叉树第一部分
英伟达这篇CVPR 2022 Oral火了!2D图像秒变逼真3D物体!虚拟爵士乐队来了!
413-二叉树基础
IDEA 无法保存设置 源根 D:XXXX在模块XXX中重复
How to solve multi-channel customer communication problems in independent stations? This cross-border e-commerce plug-in must be known!
深度学习论文阅读目标检测篇(七)中英对照版:YOLOv4《Optimal Speed and Accuracy of Object Detection》
[Eureka registry]
[custom endpoint and implementation principle]
Analysis of 43 cases of MATLAB neural network: Chapter 32 time series prediction of wavelet neural network - short-term traffic flow prediction
impdp导schema报ORA-31625异常处理
医学图像开源数据集汇总(二)
物联网?快来看 Arduino 上云啦
使用Live Chat促進業務銷售的驚人技巧
Summary of medical image open source datasets (II)
2021-08-17