qrCodeUtil.php 13 KB

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