链街Dcat后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
5.3 KiB

  1. <?php
  2. namespace App\Admin\Common;
  3. use Dcat\Admin\Controllers\AdminController;
  4. use GuzzleHttp\Client;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Storage;
  7. class Images extends AdminController
  8. {
  9. /**
  10. * 获取token
  11. */
  12. private function getWxToken()
  13. {
  14. $appid= env('WECHAT_MINI_PROGRAM_APPID');
  15. $secret=env('WECHAT_MINI_PROGRAM_SECRET');
  16. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
  17. $ch = curl_init();
  18. curl_setopt($ch, CURLOPT_URL,$url);
  19. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  20. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
  21. $data = curl_exec($ch);
  22. curl_close($ch);
  23. $data = json_decode($data,true);
  24. return $data['access_token'];
  25. }
  26. /**
  27. * 请求生成微信小程序码
  28. */
  29. public function createQrCode($param,$path)
  30. {
  31. $access_token = $this->getWxToken();
  32. $data=array(
  33. 'scene'=>$param,
  34. "page"=>$path,
  35. "width"=>100
  36. );
  37. $data = json_encode($data);
  38. $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$access_token."";
  39. $ch = curl_init();
  40. curl_setopt($ch, CURLOPT_URL,$url);
  41. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  42. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
  43. curl_setopt($ch, CURLOPT_POST,1);
  44. curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
  45. $data = curl_exec($ch);
  46. curl_close($ch);
  47. return $data;
  48. }
  49. /**
  50. * 请求生成微信二维码
  51. * @param $param 参数
  52. */
  53. public function createWeChatQrCode($param,$path)
  54. {
  55. $access_token = $this->getWxToken();
  56. $data=array(
  57. "path"=>$path.$param,
  58. "width"=>280
  59. );
  60. $data = json_encode($data);
  61. $url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$access_token."";
  62. $ch = curl_init();
  63. curl_setopt($ch, CURLOPT_URL,$url);
  64. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  65. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
  66. curl_setopt($ch, CURLOPT_POST,1);
  67. curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
  68. $data = curl_exec($ch);
  69. curl_close($ch);
  70. return $data;
  71. }
  72. /**
  73. * 上传文件到oss
  74. * @param $base64 //文件流
  75. * @param $fileName //文件名称
  76. * @return bool
  77. */
  78. public function uploadOss($base64,$fileName)
  79. {
  80. return Storage::put($fileName,$base64);
  81. }
  82. public function test()
  83. {
  84. $wx_head = 'http://www.marketmanage.com/uploads/20200728/d5a491cd3d8d071e3212c3478e8e35a1.jpg';
  85. $avatar_file = file_get_contents($wx_head);
  86. file_put_contents('./logo.jpg',$avatar_file);
  87. $logo = $this->changeAvatar($avatar_file);
  88. file_put_contents('./logo_new.jpg',$logo);
  89. //$qr_code = $this->createQrCode('','');
  90. }
  91. public function changeAvatar($avatar)
  92. {
  93. //处理用户头像为圆形icon
  94. $avatar = imagecreatefromstring($avatar);
  95. $w = imagesx($avatar)-5;
  96. $h = imagesy($avatar)-5;
  97. $w = min($w, $h);
  98. $h = $w;
  99. $img = imagecreatetruecolor($w, $h);
  100. imagesavealpha($img, true);
  101. $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
  102. imagefill($img, 0, 0, $bg);
  103. $r = $w / 2; //圆半径
  104. $y_x = $r; //圆心X坐标
  105. $y_y = $r; //圆心Y坐标
  106. for ($x = 0; $x < $w; $x++) {
  107. for ($y = 0; $y < $h; $y++) {
  108. $rgbColor = imagecolorat($avatar, $x, $y);
  109. if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
  110. imagesetpixel($img, $x, $y, $rgbColor);
  111. }
  112. }
  113. }
  114. ob_start();
  115. imagepng($img);
  116. imagedestroy($img);
  117. imagedestroy($avatar);
  118. $contents = ob_get_contents(); //读取缓存区的内容
  119. ob_end_clean(); //清空缓存区
  120. return $contents;
  121. }
  122. public function makeOnePic($qr_code, $logo) //二维码与头像组合
  123. {
  124. $qr_code = imagecreatefromstring($qr_code); //生成的二维码底色为白色
  125. //设置二维码为透明底
  126. imagesavealpha($qr_code, true); //这个设置一定要加上
  127. $bg = imagecolorallocatealpha($qr_code, 255, 255, 255, 127); //拾取一个完全透明的颜色,最后一个参数127为全透明
  128. imagefill($qr_code, 0, 0, $bg);
  129. $icon = imagecreatefromstring($logo); //生成中间圆形logo (微信头像获取到的logo的大小为132px 132px)
  130. $qr_width = imagesx($qr_code); //二维码图片宽度
  131. $lg_width = imagesx($icon); //logo图片宽度
  132. $lg_height = imagesy($icon); //logo图片高度
  133. $qr_lg_width = $qr_width / 2.2;
  134. $scale = $lg_width / $qr_lg_width;
  135. $qr_lg_height = $lg_height / $scale;
  136. $start_width = ($qr_width - $lg_width) / 2 ; //(获取logo的左上方的位置:( 外部的正方形-logo的宽 ) / 2,我这边存在1px的偏差 我就给+2啦)
  137. imagecopyresampled($qr_code, $icon, $start_width, $start_width, 0, 0, $qr_lg_width, $qr_lg_height, $lg_width, $lg_height);
  138. imagejpeg($qr_code,'./qrcode.png'); //保存
  139. imagedestroy($qr_code);
  140. imagedestroy($icon);
  141. }
  142. }