qrCodeUtil.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. namespace common\components;
  3. use Yii;
  4. use common\components\stringUtil;
  5. class qrCodeUtil
  6. {
  7. //客户向供货商充值二维码 ssh 20240507
  8. public static function generateRechargeQrCode($url, $unique, $logo = '', $size = 5)
  9. {
  10. $saveDir = dirUtil::getImgUploadDir() . '/qrcode/';
  11. include_once Yii::getAlias("@vendor/qrcode") . '/phpqrcode.php';
  12. $random = $unique;
  13. //表示容错率,也就是有被覆盖的区域还能识别,分别是 L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%)
  14. $errorCorrectionLevel = 'H';
  15. $matrixPointSize = $size;//生成图片大小
  16. if (!file_exists($saveDir)) {
  17. mkdir($saveDir, 0777, true);
  18. }
  19. $QR = $saveDir . $random . '.png';
  20. $shortUrl = 'qrcode/' . $random . '.png';
  21. if (file_exists($QR)) {
  22. return $shortUrl;
  23. }
  24. \QRcode::png($url, $QR, $errorCorrectionLevel, $matrixPointSize, 1);//最后的参数1表示空白间距
  25. if (!empty($logo) && file_exists($logo)) {
  26. $QR = imagecreatefromstring(file_get_contents($QR));
  27. $logo = imagecreatefromstring(file_get_contents($logo));
  28. $QR_width = imagesx($QR);//二维码图片宽度
  29. $QR_height = imagesy($QR);//二维码图片高度
  30. $logo_width = imagesx($logo);//logo图片宽度
  31. $logo_height = imagesy($logo);//logo图片高度
  32. $logo_qr_width = $QR_width / 5;
  33. $scale = $logo_width / $logo_qr_width;
  34. $logo_qr_height = $logo_height / $scale;
  35. $from_width = ($QR_width - $logo_qr_width) / 2;
  36. //重新组合图片并调整大小
  37. imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
  38. imagepng($QR, $saveDir . $random . '.png');//输出图片
  39. }
  40. oss::uploadImage($shortUrl, $QR);
  41. return $shortUrl;
  42. }
  43. /**
  44. * 生成二维码
  45. * $unique 举例 userId_189 merchant_20等
  46. * $size 大小 5 195px 8 312px
  47. */
  48. public static function generate($url, $unique = '', $logo = '', $size = 5)
  49. {
  50. $app = Yii::getAlias("@app");
  51. $saveDir = $app . '/../../resource/images/qrcode/';
  52. include_once Yii::getAlias("@vendor/qrcode") . '/phpqrcode.php';
  53. $rand = stringUtil::buildOrderNo();
  54. $random = empty($unique) ? $rand : $unique . '_' . $rand;
  55. //表示容错率,也就是有被覆盖的区域还能识别,分别是 L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%)
  56. $errorCorrectionLevel = 'H';
  57. $matrixPointSize = $size;//生成图片大小
  58. $QR = $saveDir . $random . '.png';
  59. \QRcode::png($url, $QR, $errorCorrectionLevel, $matrixPointSize, 1);//最后的参数1表示空白间距
  60. if (empty($logo) || file_exists($logo) == false) {
  61. return '/qrcode/' . $random . '.png';//没有logo输出的二维码
  62. }
  63. $QR = imagecreatefromstring(file_get_contents($QR));
  64. $logo = imagecreatefromstring(file_get_contents($logo));
  65. $QR_width = imagesx($QR);//二维码图片宽度
  66. $QR_height = imagesy($QR);//二维码图片高度
  67. $logo_width = imagesx($logo);//logo图片宽度
  68. $logo_height = imagesy($logo);//logo图片高度
  69. $logo_qr_width = $QR_width / 5;
  70. $scale = $logo_width / $logo_qr_width;
  71. $logo_qr_height = $logo_height / $scale;
  72. $from_width = ($QR_width - $logo_qr_width) / 2;
  73. //重新组合图片并调整大小
  74. imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
  75. imagepng($QR, $saveDir . $random . '_combine.png');//输出图片
  76. return '/qrcode/' . $random . '_combine.png';//组合logo后输出的二维码
  77. }
  78. /**
  79. * 生成固定名称的图片二维码
  80. * $unique 举例 userId_189 merchant_20等
  81. * $size 大小 5 195px 8 312px
  82. */
  83. public static function generateFixQrCode($url, $unique, $logo = '', $size = 5)
  84. {
  85. $app = Yii::getAlias("@app");
  86. $saveDir = $app . '/../resource/images/qrcode/';
  87. include_once Yii::getAlias("@vendor/qrcode") . '/phpqrcode.php';
  88. $random = $unique;
  89. //表示容错率,也就是有被覆盖的区域还能识别,分别是 L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%)
  90. $errorCorrectionLevel = 'H';
  91. $matrixPointSize = $size;//生成图片大小
  92. $QR = $saveDir . $random . '.png';
  93. if (file_exists($QR)) {
  94. return '/qrcode/' . $random . '.png';
  95. }
  96. \QRcode::png($url, $QR, $errorCorrectionLevel, $matrixPointSize, 1);//最后的参数1表示空白间距
  97. if (!empty($logo) && file_exists($logo)) {
  98. $QR = imagecreatefromstring(file_get_contents($QR));
  99. $logo = imagecreatefromstring(file_get_contents($logo));
  100. $QR_width = imagesx($QR);//二维码图片宽度
  101. $QR_height = imagesy($QR);//二维码图片高度
  102. $logo_width = imagesx($logo);//logo图片宽度
  103. $logo_height = imagesy($logo);//logo图片高度
  104. $logo_qr_width = $QR_width / 5;
  105. $scale = $logo_width / $logo_qr_width;
  106. $logo_qr_height = $logo_height / $scale;
  107. $from_width = ($QR_width - $logo_qr_width) / 2;
  108. //重新组合图片并调整大小
  109. imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
  110. imagepng($QR, $saveDir . $random . '.png');//输出图片
  111. }
  112. return '/qrcode/' . $random . '.png';
  113. }
  114. /**
  115. * 生成会员二维码
  116. * $unique 举例 userId_189 merchant_20等
  117. * $size 大小 5 195px 8 312px
  118. */
  119. public static function card($url, $unique = '', $logo = '', $size = 5)
  120. {
  121. $app = Yii::getAlias("@app");
  122. $saveDir = $app . '/../../resource/images/card/';
  123. include_once Yii::getAlias("@vendor/qrcode") . '/phpqrcode.php';
  124. $rand = stringUtil::buildOrderNo();
  125. $random = empty($unique) ? $rand : $unique . '_' . $rand;
  126. //表示容错率,也就是有被覆盖的区域还能识别,分别是 L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%)
  127. $errorCorrectionLevel = 'H';
  128. $matrixPointSize = $size;//生成图片大小
  129. $QR = $saveDir . $random . '.png';
  130. \QRcode::png($url, $QR, $errorCorrectionLevel, $matrixPointSize, 1);//最后的参数1表示空白间距
  131. if (empty($logo) || file_exists($logo) == false) {
  132. return '/card/' . $random . '.png';//没有logo输出的二维码
  133. }
  134. $QR = imagecreatefromstring(file_get_contents($QR));
  135. $logo = imagecreatefromstring(file_get_contents($logo));
  136. $QR_width = imagesx($QR);//二维码图片宽度
  137. $QR_height = imagesy($QR);//二维码图片高度
  138. $logo_width = imagesx($logo);//logo图片宽度
  139. $logo_height = imagesy($logo);//logo图片高度
  140. $logo_qr_width = $QR_width / 5;
  141. $scale = $logo_width / $logo_qr_width;
  142. $logo_qr_height = $logo_height / $scale;
  143. $from_width = ($QR_width - $logo_qr_width) / 2;
  144. //重新组合图片并调整大小
  145. imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
  146. imagepng($QR, $saveDir . $random . '_combine.png');//输出图片
  147. return '/card/' . $random . '_combine.png';//组合logo后输出的二维码
  148. }
  149. public static function generatePayQrCode($url, $unique = '', $logo = '', $size = 5)
  150. {
  151. $app = Yii::getAlias("@app");
  152. $saveDir = $app . '/../resource/images/qrcode/';
  153. include_once Yii::getAlias("@vendor/qrcode") . '/phpqrcode.php';
  154. $rand = stringUtil::buildOrderNo();
  155. $random = empty($unique) ? $rand : $unique . '_' . $rand;
  156. //表示容错率,也就是有被覆盖的区域还能识别,分别是 L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%)
  157. $errorCorrectionLevel = 'H';
  158. $matrixPointSize = $size;//生成图片大小
  159. $QR = $saveDir . $random . '.png';
  160. \QRcode::png($url, $QR, $errorCorrectionLevel, $matrixPointSize, 1);//最后的参数1表示空白间距
  161. if (empty($logo) || file_exists($logo) == false) {
  162. return '/qrcode/' . $random . '.png';//没有logo输出的二维码
  163. }
  164. $QR = imagecreatefromstring(file_get_contents($QR));
  165. $logo = imagecreatefromstring(file_get_contents($logo));
  166. $QR_width = imagesx($QR);//二维码图片宽度
  167. $QR_height = imagesy($QR);//二维码图片高度
  168. $logo_width = imagesx($logo);//logo图片宽度
  169. $logo_height = imagesy($logo);//logo图片高度
  170. $logo_qr_width = $QR_width / 5;
  171. $scale = $logo_width / $logo_qr_width;
  172. $logo_qr_height = $logo_height / $scale;
  173. $from_width = ($QR_width - $logo_qr_width) / 2;
  174. //重新组合图片并调整大小
  175. imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
  176. imagepng($QR, $saveDir . $random . '_combine.png');//输出图片
  177. //return '/qrcode/'.$random.'_combine.png';//组合logo后输出的二维码
  178. $weixinImg = $app . '/web/images/merchant/wxPay.png';
  179. $combineQR = $saveDir . $random . '_combine.png';
  180. $weixinImgSource = imagecreatefromstring(file_get_contents($weixinImg));
  181. $combineQRSource = imagecreatefromstring(file_get_contents($combineQR));
  182. $weixinWidth = imagesx($weixinImgSource);
  183. $weixinHeight = imagesy($weixinImgSource);
  184. $combineQRWidth = imagesx($combineQRSource);
  185. $combineQRHeight = imagesy($combineQRSource);
  186. $combineQRQRWidth = $weixinWidth / 5;
  187. $scale = $combineQRWidth / $combineQRQRHeight;
  188. $combineQRQRWidth = '';
  189. }
  190. /**
  191. * 生成收款二维码
  192. * $unique 举例 userId_189 merchant_20等
  193. * $size 大小 5 195px 8 312px
  194. */
  195. public static function generateGatheringQrCode($url, $sjId, $shopId, $logo = '', $size = 5)
  196. {
  197. include_once Yii::getAlias("@vendor/qrcode") . '/phpqrcode.php';
  198. //表示容错率,也就是有被覆盖的区域还能识别,分别是 L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%)
  199. $errorCorrectionLevel = 'H';
  200. $matrixPointSize = $size;//生成图片大小
  201. $prefix = dirUtil::getImgDir();
  202. $middle = 'retail/qrCode/gathering/';
  203. if (file_exists($prefix . $middle) == false) {
  204. mkdir($prefix . $middle, 0777, true);
  205. }
  206. $behind = $middle . $sjId . '_' . $shopId . '.png';
  207. $saveFile = $prefix . $behind;
  208. if (file_exists($saveFile) == false) {
  209. \QRcode::png($url, $saveFile, $errorCorrectionLevel, $matrixPointSize, 1);//最后的参数1表示空白间距
  210. oss::uploadImage($behind, $saveFile);
  211. }
  212. return $behind;
  213. }
  214. public static function generateShortTimeGatheringQrCode($url, $sjId, $shopId, $payWay, $logo = '', $size = 5)
  215. {
  216. include_once Yii::getAlias("@vendor/qrcode") . '/phpqrcode.php';
  217. //表示容错率,也就是有被覆盖的区域还能识别,分别是 L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%)
  218. $errorCorrectionLevel = 'H';
  219. $matrixPointSize = $size;//生成图片大小
  220. $prefix = dirUtil::getImgDir();
  221. $middle = 'retail/qrCode/gathering2/' . $sjId . '/' . $shopId . '/';
  222. if (file_exists($prefix . $middle) == false) {
  223. mkdir($prefix . $middle, 0777, true);
  224. }
  225. $behind = $middle . $payWay . '_' . md5($url) . '.png';
  226. $saveFile = $prefix . $behind;
  227. if (file_exists($saveFile) == false) {
  228. \QRcode::png($url, $saveFile, $errorCorrectionLevel, $matrixPointSize, 1);//最后的参数1表示空白间距
  229. oss::uploadImage($behind, $saveFile);
  230. }
  231. return $behind;
  232. }
  233. //h5商城 ssh 2020.4.30
  234. public static function generateH5MallQrCode($url, $logo = '', $size = 5)
  235. {
  236. include_once Yii::getAlias("@vendor/qrcode") . '/phpqrcode.php';
  237. //表示容错率,也就是有被覆盖的区域还能识别,分别是 L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%)
  238. $errorCorrectionLevel = 'H';
  239. $matrixPointSize = $size;//生成图片大小
  240. $prefix = dirUtil::getImgDir();
  241. $middle = 'retail/qrCode/h5Mall/';
  242. if (file_exists($prefix . $middle) == false) {
  243. mkdir($prefix . $middle, 0777, true);
  244. }
  245. $behind = $middle . md5($url) . '.png';
  246. $file = $prefix . $behind;
  247. $imgUrl = imgUtil::getPrefix() . $behind;
  248. if (file_exists($file) == false) {
  249. \QRcode::png($url, $file, $errorCorrectionLevel, $matrixPointSize, 1);//最后的参数1表示空白间距
  250. }
  251. return ['shortUrl' => $behind, 'url' => $imgUrl];
  252. }
  253. }