qrCodeUtil.php 13 KB

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