qrCodeUtil.php 11 KB

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