Blogger Information
Blog 47
fans 0
comment 2
visits 102530
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php将微信小程序二维码和用户头像结合,并输出
拾一枝樱花的博客
Original
1737 people have browsed it
  1. <?php
  2. /**
  3. * 步骤:
  4. * 1.获得微信返回的二维码图片数据流
  5. * 2.把用户头像变成圆形,覆盖到小程序中间的空白部分
  6. * 3.把二维码与圆形头像结合
  7. * 3.返回客户端,或者输出显示
  8. */
  9. header('content-type:text/html;charset=utf-8');
  10. if(!empty($_GET['id'])){
  11. $id = $_GET['id'];
  12. }else{
  13. $id = 0;
  14. }
  15. if(!empty($_GET['avatar'])){
  16. $avatarUrl = $_GET['avatar'];
  17. }else{
  18. $avatarUrl = 'https://mp.weixin.qq.com/wxopen/basicprofile?action=get_headimg&token=token&t=1587056274299';
  19. }
  20. $appid = "appid";//appid
  21. $appsecret = "app secret";//app secret
  22. $send = array('scene' => $id, 'path' => 'lionfish_comshop/pages/index/index', 'width' => '600');//传给微信的参数
  23. //请求微信,获取小程序二维码
  24. $resWxQrCode = getWxQrcode($send, $appid, $appsecret);
  25. //用户头像图片变圆形
  26. $avatar = file_get_contents($avatarUrl);
  27. $logo = yuanImg($avatar);//返回的是图片数据流
  28. //二维码与头像结合
  29. $sharePic = qrcodeWithLogo($resWxQrCode, $logo);
  30. $img = "qrcode_" . $id. ".png";
  31. $base64_image = "data:image/png;base64," . base64_encode($sharePic);
  32. $link = mysqli_connect('localhost','zhengyijing ','zhengyijing_2019');
  33. mysqli_select_db($link,'zhengyijing');
  34. mysqli_query($link," SET NAMES utf-8");
  35. $sql = "update ims_lionfish_company_user set wx_code='php/".$img."' WHERE company_type=".$id;
  36. $rs = mysqli_query($link,$sql);
  37. mysqli_close($link);
  38. file_put_contents('../php/'.$img, $sharePic);
  39. /******************************* 下面是功能函数 **********************************/
  40. /**
  41. * curl方法
  42. * @param $url 请求url
  43. * @param $data 传送数据,有数据时使用post传递
  44. * @param type 为2时,设置json传递
  45. */
  46. function curlRequest($url, $data = null, $type = 1)
  47. {
  48. $curl = curl_init();
  49. curl_setopt($curl, CURLOPT_URL, $url);
  50. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  51. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  52. if (!empty($data)) {
  53. curl_setopt($curl, CURLOPT_POST, 1);
  54. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  55. if ($type == 2) {
  56. curl_setopt($curl, CURLOPT_HTTPHEADER,
  57. array('Content-Type: application/json', 'Content-Length: ' . strlen($data)));
  58. }
  59. }
  60. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  61. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
  62. $output = curl_exec($curl);
  63. curl_close($curl);
  64. return $output;
  65. }
  66. /**
  67. * 请求微信服务器,生成二维码
  68. * @param $data array('scene'=>$setid, 'path' =>'pages/question/question', 'width'=>'100');
  69. */
  70. function getWxQrcode($data, $appid, $appsecret)
  71. {
  72. //get access_token
  73. $wxTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret;
  74. $access_token = curlRequest($wxTokenUrl);
  75. $access_token = json_decode($access_token, true);
  76. //get qrcode 微信B接口
  77. $wxQrcodeUrl = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $access_token['access_token'];
  78. $data = json_encode($data, JSON_UNESCAPED_UNICODE);
  79. return curlRequest($wxQrcodeUrl, $data);
  80. }
  81. /**
  82. * 在二维码的中间区域镶嵌图片
  83. * @param $QR 二维码数据流。比如file_get_contents(imageurl)返回的东东,或者微信给返回的东东
  84. * @param $logo 中间显示图片的数据流。比如file_get_contents(imageurl)返回的东东
  85. * @return 返回图片数据流
  86. */
  87. function qrcodeWithLogo($QR, $logo)
  88. {
  89. $QR = imagecreatefromstring($QR);
  90. $logo = imagecreatefromstring($logo);
  91. $QR_width = imagesx($QR);//二维码图片宽度
  92. $QR_height = imagesy($QR);//二维码图片高度
  93. $logo_width = imagesx($logo);//logo图片宽度
  94. $logo_height = imagesy($logo);//logo图片高度
  95. $logo_qr_width = $QR_width / 2.2;//组合之后logo的宽度(占二维码的1/2.2)
  96. $scale = $logo_width / $logo_qr_width;//logo的宽度缩放比(本身宽度/组合后的宽度)
  97. $logo_qr_height = $logo_height / $scale;//组合之后logo的高度
  98. $from_width = ($QR_width - $logo_qr_width) / 2;//组合之后logo左上角所在坐标点
  99. /**
  100. * 重新组合图片并调整大小
  101. * imagecopyresampled() 将一幅图像(源图象)中的一块正方形区域拷贝到另一个图像中
  102. */
  103. imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
  104. /**
  105. * 如果想要直接输出图片,应该先设header。header("Content-Type: image/png; charset=utf-8");
  106. * 并且去掉缓存区函数
  107. */
  108. //获取输出缓存,否则imagepng会把图片输出到浏览器
  109. ob_start();
  110. imagepng($QR);
  111. imagedestroy($QR);
  112. imagedestroy($logo);
  113. $contents = ob_get_contents();
  114. ob_end_clean();
  115. return $contents;
  116. }
  117. /**
  118. * 剪切图片为圆形
  119. * @param $picture 图片数据流 比如file_get_contents(imageurl)返回的东东
  120. * @return 图片数据流
  121. */
  122. function yuanImg($picture)
  123. {
  124. $src_img = imagecreatefromstring($picture);
  125. $w = imagesx($src_img);
  126. $h = imagesy($src_img);
  127. $w = min($w, $h);
  128. $h = $w;
  129. $img = imagecreatetruecolor($w, $h);
  130. //这一句一定要有
  131. imagesavealpha($img, true);
  132. //拾取一个完全透明的颜色,最后一个参数127为全透明
  133. $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
  134. imagefill($img, 0, 0, $bg);
  135. $r = $w / 2; //圆半径
  136. $y_x = $r; //圆心X坐标
  137. $y_y = $r; //圆心Y坐标
  138. for ($x = 0; $x < $w; $x++) {
  139. for ($y = 0; $y < $h; $y++) {
  140. $rgbColor = imagecolorat($src_img, $x, $y);
  141. if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
  142. imagesetpixel($img, $x, $y, $rgbColor);
  143. }
  144. }
  145. }
  146. /**
  147. * 如果想要直接输出图片,应该先设header。header("Content-Type: image/png; charset=utf-8");
  148. * 并且去掉缓存区函数
  149. */
  150. //获取输出缓存,否则imagepng会把图片输出到浏览器
  151. ob_start();
  152. imagepng($img);
  153. imagedestroy($img);
  154. $contents = ob_get_contents();
  155. ob_end_clean();
  156. return $contents;
  157. }
  158. ?>
  159. <img width="300" src="<?php echo $base64_image; ?>"/>
  160. <a class="down" href="../php/<?php echo $img; ?>" download="qrcode">下载小程序码</a>
  161. <style>
  162. .down{
  163. display: block;
  164. width: 200px; height: 40px; line-height: 40px;
  165. text-align: center; margin-left: 50px;
  166. border-radius: 30px; background: #367bb5; color: #ffffff;
  167. text-decoration: none;
  168. margin-top: 20px;
  169. }
  170. </style>
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post