qrCodeUtil.php 7.4 KB

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