MiniCodeHelper.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2021-12-24
  8. * Time: 16:08
  9. */
  10. namespace app\common\helpers;
  11. use app\common\exceptions\ShopException;
  12. use app\common\facades\Setting;
  13. use EasyWeChat\Factory;
  14. class MiniCodeHelper
  15. {
  16. private $mini_set;
  17. private $scene;
  18. private $dir;
  19. private $page;
  20. private $file_name;
  21. private $width;
  22. /**
  23. * MiniCodeHelper constructor.
  24. * @param string $dir 文件目录 商城根目录开始
  25. * @param string $file_name 文件名
  26. * @param string $page 二维码页面路由
  27. * @param string $scene 二维码链接参数
  28. * @param int $width 二维码宽度
  29. */
  30. public function __construct(string $dir, string $file_name, string $page, string $scene, int $width = 300)
  31. {
  32. $this->mini_set = $this->getMiniSet();
  33. $this->dir = $dir;
  34. $this->file_name = $file_name;
  35. $this->page = $page;
  36. $this->scene = $scene;
  37. $this->width = $width;
  38. }
  39. public function url()
  40. {
  41. $config = [
  42. 'app_id' => $this->mini_set['key'],
  43. 'secret' => $this->mini_set['secret'],
  44. ];
  45. if (!$config['app_id'] || !$config['secret']) {
  46. throw new ShopException('小程序未配置');
  47. }
  48. $app = Factory::miniProgram($config);
  49. $parameter = [
  50. 'page' => $this->page,
  51. 'scene' => $this->scene,
  52. 'width' => $this->width,
  53. ];
  54. $res = $app->app_code->getUnlimit('scene-value', $parameter);
  55. if (is_array($res) && isset($res['errcode'])) {
  56. \Log::debug('-------小程序二维码生成失败-------', [$res['errcode'], $res['errmsg']]);
  57. throw new ShopException($res['errmsg']);
  58. }
  59. $absolute_dir = base_path($this->dir);
  60. $this->recursionDir($absolute_dir);
  61. $filename = $res->saveAs($absolute_dir, $this->file_name);
  62. $mini_code_url = request()->getSchemeAndHttpHost().config('app.webPath').'/'.$this->dir.'/'.$filename;
  63. return $mini_code_url;
  64. }
  65. private function getMiniSet()
  66. {
  67. return Setting::get('plugin.min_app');
  68. }
  69. private function recursionDir($dir)
  70. {
  71. return is_dir($dir) or self::recursionDir(dirname($dir)) and mkdir($dir, 0777);
  72. }
  73. public function drawCircle($target)
  74. {
  75. $src_img = imagecreatefromstring($target);
  76. $w = imagesx($src_img);
  77. $h = imagesy($src_img);
  78. $w = min($w, $h);
  79. $h = $w;
  80. $img = imagecreatetruecolor($w, $h);
  81. imagesavealpha($img, true);
  82. $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
  83. imagefill($img, 0, 0, $bg);
  84. $r = $w / 2; //圆半径
  85. for ($x = 0; $x < $w; $x++) {
  86. for ($y = 0; $y < $h; $y++) {
  87. $rgbColor = imagecolorat($src_img, $x, $y);
  88. if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
  89. imagesetpixel($img, $x, $y, $rgbColor);
  90. }
  91. }
  92. }
  93. ob_start();
  94. imagepng($img);
  95. imagedestroy($img);
  96. $contents = ob_get_contents();
  97. ob_end_clean();
  98. return $contents;
  99. }
  100. public function replaceMiddleLogo($target, $logo)
  101. {
  102. $target = imagecreatefromstring($target);
  103. $logo = imagecreatefromstring($logo);
  104. $target_width = imagesx($target);
  105. $logo_width = imagesx($logo);
  106. $logo_height = imagesy($logo);
  107. $logo_qr_width = $target_width / 2.2;
  108. $scale = $logo_width / $logo_qr_width;
  109. $logo_qr_height = $logo_height / $scale;
  110. $from_width = ($target_width - $logo_qr_width) / 2;
  111. imagecopyresampled($target, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
  112. ob_start();
  113. imagepng($target);
  114. imagedestroy($target);
  115. imagedestroy($logo);
  116. $contents = ob_get_contents();
  117. ob_end_clean();
  118. return $contents;
  119. }
  120. // private function handleTarget($file_name)
  121. // {
  122. // $type = exif_imagetype($file_name);
  123. // switch ($type) {
  124. // case 1 :
  125. // $target = imagecreatefromgif($file_name);
  126. // break;
  127. // case 2 :
  128. // $target = imagecreatefromjpeg($file_name);
  129. // break;
  130. // case 3 :
  131. // $target = imagecreatefrompng($file_name);
  132. // break;
  133. // case 4 :
  134. // $target = imagecreatefromswf($file_name);
  135. // break;
  136. // case 5 :
  137. // $target = imagecreatefromgpsd($file_name);
  138. // break;
  139. // case 6 :
  140. // $target = imagecreatefrombmp($file_name);
  141. // break;
  142. // case 7 :
  143. // $target = imagecreatefromtiffii($file_name);
  144. // break;
  145. // case 8 :
  146. // $target = imagecreatefromtiffmm($file_name);
  147. // break;
  148. // case 9 :
  149. // $target = imagecreatefromjpc($file_name);
  150. // break;
  151. // case 10 :
  152. // $target = imagecreatefromjp2($file_name);
  153. // break;
  154. // case 11 :
  155. // $target = imagecreatefromjpx($file_name);
  156. // break;
  157. // case 12 :
  158. // $target = imagecreatefromjb2($file_name);
  159. // break;
  160. // case 13 :
  161. // $target = imagecreatefromswc($file_name);
  162. // break;
  163. // case 14 :
  164. // $target = imagecreatefromiff($file_name);
  165. // break;
  166. // case 15 :
  167. // $target = imagecreatefromwbmp($file_name);
  168. // break;
  169. // case 16 :
  170. // $target = imagecreatefromxbm($file_name);
  171. // break;
  172. // case 17 :
  173. // $target = imagecreatefromico($file_name);
  174. // break;
  175. // case 18 :
  176. // $target = imagecreatefromwebp($file_name);
  177. // break;
  178. // default :
  179. // $target = false;
  180. // break;
  181. // }
  182. // return $target;
  183. // }
  184. }