SmallQrCode.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: yunzhong
  5. * Date: 2020/7/8
  6. * Time: 14:12
  7. */
  8. namespace app\common\services;
  9. class SmallQrCode
  10. {
  11. public function getSmallQrCode($small_name,$postData)
  12. {
  13. //$small_url = "packageD/preferential_volume/preferential_volume";
  14. //$small_url = "pages/index/index";
  15. $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?";
  16. $token = $this->getToken();
  17. \Log::debug('===========access_token===========', $token);
  18. $url .= "access_token=" . $token;
  19. // $postdata = [
  20. // "scene" => 'vgid=' . $goods_id . ',vcpn='.$member_coupon. ',mid=' . $member_id,
  21. // "page" => $small_url,
  22. // ];
  23. $path = storage_path('static/qrcode/coupon/' . \YunShop::app()->uniacid);
  24. if (!is_dir($path)) {
  25. Utils::mkdirs($path);
  26. }
  27. \Log::debug('=====地址信息=======', $postData);
  28. $res = $this->curl_post($url, json_encode($postData), $options = array());
  29. $erroe = json_decode($res);
  30. $data['message'] = "";
  31. $data['file_path'] = "";
  32. $data['code'] = 0;
  33. if (isset($erroe->errcode)) {
  34. $data['message'] = '错误码' . $erroe->errcode . ';错误信息' . $erroe->errmsg;
  35. $data['code'] = 1;
  36. return $data;
  37. }
  38. \Log::debug('===========生成小程序二维码===========', $res);
  39. $file = date('Ymd').'_code_qr_small_'.$small_name.'.png';
  40. file_put_contents($path . '/' . $file, $res);
  41. $imgPath = $path . '/' . $file;
  42. if (config('app.framework') == 'platform') {
  43. $urlPath = request()->getSchemeAndHttpHost() . '/storage/' . substr($imgPath, strpos($imgPath, 'static'));
  44. } else {
  45. $urlPath = request()->getSchemeAndHttpHost() . '/' . substr($imgPath, strpos($imgPath, 'addons'));
  46. }
  47. $data['file_path'] = $urlPath;
  48. return $data;
  49. }
  50. private function curl_post($url = '', $postdata = '', $options = array())
  51. {
  52. $ch = curl_init($url);
  53. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  54. curl_setopt($ch, CURLOPT_POST, 1);
  55. curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
  56. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  57. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  58. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  59. if (!empty($options)) {
  60. curl_setopt_array($ch, $options);
  61. }
  62. $data = curl_exec($ch);
  63. curl_close($ch);
  64. return $data;
  65. }
  66. //发送获取token请求,获取token(2小时)
  67. private function getToken()
  68. {
  69. $url = $this->getTokenUrlStr();
  70. $res = $this->curl_post($url, $postdata = '', $options = array());
  71. $data = json_decode($res, JSON_FORCE_OBJECT);
  72. return $data['access_token'];
  73. }
  74. //获取token的url参数拼接
  75. private function getTokenUrlStr()
  76. {
  77. $set = \Setting::get('plugin.min_app');
  78. $getTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?"; //获取token的url
  79. $WXappid = $set['key']; //APPID
  80. $WXsecret = $set['secret']; //secret
  81. $str = $getTokenUrl;
  82. $str .= "grant_type=client_credential&";
  83. $str .= "appid=" . $WXappid . "&";
  84. $str .= "secret=" . $WXsecret;
  85. return $str;
  86. }
  87. /*
  88. * 将图片转换为圆形
  89. */
  90. public static function drawCircle($imgpath)
  91. {
  92. $src_img = imagecreatefromstring($imgpath);
  93. $w = imagesx($src_img);
  94. $h = imagesy($src_img);
  95. $w = min($w, $h);
  96. $h = $w;
  97. $img = imagecreatetruecolor($w, $h);
  98. //必须
  99. imagesavealpha($img, true);
  100. //全透明
  101. $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
  102. imagefill($img, 0, 0, $bg);
  103. $r = $w / 2; //圆半径
  104. for ($x = 0; $x < $w; $x++) {
  105. for ($y = 0; $y < $h; $y++) {
  106. $rgbColor = imagecolorat($src_img, $x, $y);
  107. // $rgbColor = imagecolorsforindex($src_img,$rgbColor);
  108. if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
  109. imagesetpixel($img, $x, $y, $rgbColor);
  110. }
  111. }
  112. }
  113. ob_start();
  114. imagepng($img);
  115. imagedestroy($img);
  116. $contents = ob_get_contents();
  117. ob_end_clean();
  118. return $contents;
  119. }
  120. //替换小程序二维码图片中间的logo
  121. public static function replaceMiddleLogo($target,$logo)
  122. {
  123. $target = imagecreatefromstring ($target);
  124. $logo = imagecreatefromstring ($logo);
  125. $target_width = imagesx($target);//二维码图片宽度
  126. //$target_height = imagesy($target);//二维码图片高度
  127. $logo_width = imagesx($logo);//logo图片宽度
  128. $logo_height = imagesy($logo);//logo图片高度
  129. $logo_qr_width = $target_width / 2.2;//组合之后logo的宽度(占二维码的1/2.2)
  130. $scale = $logo_width / $logo_qr_width;//logo的宽度缩放比(本身宽度/组合后的宽度)
  131. $logo_qr_height = $logo_height / $scale;//组合之后logo的高度
  132. $from_width = ($target_width - $logo_qr_width) / 2;//组合之后logo左上角所在坐标点
  133. //重新组合图片并调整大小
  134. //imagecopyresampled() 将一幅图像(源图象)中的一块正方形区域拷贝到另一个图像中
  135. imagecopyresampled($target, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
  136. /**
  137. * 如果想要直接输出图片,应该先设header。header("Content-Type: image/png; charset=utf-8");
  138. * 并且去掉缓存区函数
  139. */
  140. //获取输出缓存,否则imagepng会把图片输出到浏览器
  141. ob_start();
  142. imagepng($target);
  143. imagedestroy($target);
  144. imagedestroy($logo);
  145. $contents = ob_get_contents();
  146. ob_end_clean();
  147. return $contents;
  148. }
  149. }