链街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.

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