qrCodeUtil.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace common\components;
  3. use Yii;
  4. use common\components\stringUtil;
  5. class qrCodeUtil
  6. {
  7. /**
  8. * 生成二维码
  9. * $unique 举例 userId_189 merchant_20等
  10. * $size 大小 5 195px 8 312px
  11. */
  12. public static function generate($url, $unique = '', $logo = '', $size = 5)
  13. {
  14. $app = Yii::getAlias("@app");
  15. $saveDir = $app . '/../../resource/images/qrcode/';
  16. include_once Yii::getAlias("@vendor/qrcode") . '/phpqrcode.php';
  17. $rand = stringUtil::buildOrderNo();
  18. $random = empty($unique) ? $rand : $unique . '_' . $rand;
  19. //表示容错率,也就是有被覆盖的区域还能识别,分别是 L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%)
  20. $errorCorrectionLevel = 'H';
  21. $matrixPointSize = $size;//生成图片大小
  22. $QR = $saveDir . $random . '.png';
  23. \QRcode::png($url, $QR, $errorCorrectionLevel, $matrixPointSize, 1);//最后的参数1表示空白间距
  24. if (empty($logo) || file_exists($logo) == false) {
  25. return '/qrcode/' . $random . '.png';//没有logo输出的二维码
  26. }
  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. imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);//重新组合图片并调整大小
  38. imagepng($QR, $saveDir . $random . '_combine.png');//输出图片
  39. return '/qrcode/' . $random . '_combine.png';//组合logo后输出的二维码
  40. }
  41. /**
  42. * 生成固定名称的图片二维码
  43. * $unique 举例 userId_189 merchant_20等
  44. * $size 大小 5 195px 8 312px
  45. */
  46. public static function generateFixQrCode($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. $random = $unique;
  52. //表示容错率,也就是有被覆盖的区域还能识别,分别是 L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%)
  53. $errorCorrectionLevel = 'H';
  54. $matrixPointSize = $size;//生成图片大小
  55. $QR = $saveDir . $random . '.png';
  56. if (file_exists($QR)) {
  57. return '/qrcode/' . $random . '.png';
  58. }
  59. \QRcode::png($url, $QR, $errorCorrectionLevel, $matrixPointSize, 1);//最后的参数1表示空白间距
  60. if (!empty($logo) && file_exists($logo)) {
  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. imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);//重新组合图片并调整大小
  72. imagepng($QR, $saveDir . $random . '.png');//输出图片
  73. }
  74. return '/qrcode/' . $random . '.png';
  75. }
  76. /**
  77. * 生成会员二维码
  78. * $unique 举例 userId_189 merchant_20等
  79. * $size 大小 5 195px 8 312px
  80. */
  81. public static function card($url, $unique = '', $logo = '', $size = 5)
  82. {
  83. $app = Yii::getAlias("@app");
  84. $saveDir = $app . '/../../resource/images/card/';
  85. include_once Yii::getAlias("@vendor/qrcode") . '/phpqrcode.php';
  86. $rand = stringUtil::buildOrderNo();
  87. $random = empty($unique) ? $rand : $unique . '_' . $rand;
  88. //表示容错率,也就是有被覆盖的区域还能识别,分别是 L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%)
  89. $errorCorrectionLevel = 'H';
  90. $matrixPointSize = $size;//生成图片大小
  91. $QR = $saveDir . $random . '.png';
  92. \QRcode::png($url, $QR, $errorCorrectionLevel, $matrixPointSize, 1);//最后的参数1表示空白间距
  93. if (empty($logo) || file_exists($logo) == false) {
  94. return '/card/' . $random . '.png';//没有logo输出的二维码
  95. }
  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. imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);//重新组合图片并调整大小
  107. imagepng($QR, $saveDir . $random . '_combine.png');//输出图片
  108. return '/card/' . $random . '_combine.png';//组合logo后输出的二维码
  109. }
  110. public static function generatePayQrCode($url, $unique = '', $logo = '', $size = 5)
  111. {
  112. $app = Yii::getAlias("@app");
  113. $saveDir = $app . '/../resource/images/qrcode/';
  114. include_once Yii::getAlias("@vendor/qrcode") . '/phpqrcode.php';
  115. $rand = stringUtil::buildOrderNo();
  116. $random = empty($unique) ? $rand : $unique . '_' . $rand;
  117. //表示容错率,也就是有被覆盖的区域还能识别,分别是 L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%)
  118. $errorCorrectionLevel = 'H';
  119. $matrixPointSize = $size;//生成图片大小
  120. $QR = $saveDir . $random . '.png';
  121. \QRcode::png($url, $QR, $errorCorrectionLevel, $matrixPointSize, 1);//最后的参数1表示空白间距
  122. if (empty($logo) || file_exists($logo) == false) {
  123. return '/qrcode/' . $random . '.png';//没有logo输出的二维码
  124. }
  125. $QR = imagecreatefromstring(file_get_contents($QR));
  126. $logo = imagecreatefromstring(file_get_contents($logo));
  127. $QR_width = imagesx($QR);//二维码图片宽度
  128. $QR_height = imagesy($QR);//二维码图片高度
  129. $logo_width = imagesx($logo);//logo图片宽度
  130. $logo_height = imagesy($logo);//logo图片高度
  131. $logo_qr_width = $QR_width / 5;
  132. $scale = $logo_width / $logo_qr_width;
  133. $logo_qr_height = $logo_height / $scale;
  134. $from_width = ($QR_width - $logo_qr_width) / 2;
  135. imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);//重新组合图片并调整大小
  136. imagepng($QR, $saveDir . $random . '_combine.png');//输出图片
  137. //return '/qrcode/'.$random.'_combine.png';//组合logo后输出的二维码
  138. $weixinImg = $app . '/web/images/merchant/wxPay.png';
  139. $combineQR = $saveDir . $random . '_combine.png';
  140. $weixinImgSource = imagecreatefromstring(file_get_contents($weixinImg));
  141. $combineQRSource = imagecreatefromstring(file_get_contents($combineQR));
  142. $weixinWidth = imagesx($weixinImgSource);
  143. $weixinHeight = imagesy($weixinImgSource);
  144. $combineQRWidth = imagesx($combineQRSource);
  145. $combineQRHeight = imagesy($combineQRSource);
  146. $combineQRQRWidth = $weixinWidth / 5;
  147. $scale = $combineQRWidth / $combineQRQRHeight;
  148. $combineQRQRWidth = '';
  149. }
  150. /**
  151. * 生成收款二维码
  152. * $unique 举例 userId_189 merchant_20等
  153. * $size 大小 5 195px 8 312px
  154. */
  155. public static function generateGatheringQrCode($url, $merchantId, $shopId, $logo = '', $size = 5)
  156. {
  157. include_once Yii::getAlias("@vendor/qrcode") . '/phpqrcode.php';
  158. //表示容错率,也就是有被覆盖的区域还能识别,分别是 L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%)
  159. $errorCorrectionLevel = 'H';
  160. $matrixPointSize = $size;//生成图片大小
  161. $behind = '/retail/qrCode/gathering/' . $merchantId . '_' . $shopId . '.png';
  162. $saveFile = dirUtil::getImgDir() . $behind;
  163. if (file_exists($saveFile)) {
  164. return $behind;
  165. }
  166. \QRcode::png($url, $saveFile, $errorCorrectionLevel, $matrixPointSize, 1);//最后的参数1表示空白间距
  167. return $behind;
  168. }
  169. }