| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- namespace common\components;
- use Yii;
- use common\components\stringUtil;
- class qrCodeUtil{
- /**
- * 生成二维码
- * $unique 举例 userId_189 merchant_20等
- * $size 大小 5 195px 8 312px
- */
- public static function generate($url, $unique= '',$logo = '',$size=5)
- {
- $app = Yii::getAlias("@app");
- $saveDir = $app.'/../../resource/images/qrcode/';
- include_once Yii::getAlias("@vendor/qrcode").'/phpqrcode.php';
- $rand = stringUtil::buildOrderNo();
- $random = empty($unique) ? $rand : $unique.'_'.$rand;
- //表示容错率,也就是有被覆盖的区域还能识别,分别是 L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%)
- $errorCorrectionLevel = 'H';
- $matrixPointSize = $size;//生成图片大小
- $QR = $saveDir.$random.'.png';
- \QRcode::png($url, $QR, $errorCorrectionLevel, $matrixPointSize, 1);//最后的参数1表示空白间距
- if (empty($logo) || file_exists($logo) == false) {
- return '/qrcode/'.$random.'.png';//没有logo输出的二维码
- }
- $QR = imagecreatefromstring(file_get_contents($QR));
- $logo = imagecreatefromstring(file_get_contents($logo));
- $QR_width = imagesx($QR);//二维码图片宽度
- $QR_height = imagesy($QR);//二维码图片高度
- $logo_width = imagesx($logo);//logo图片宽度
- $logo_height = imagesy($logo);//logo图片高度
- $logo_qr_width = $QR_width / 5;
- $scale = $logo_width/$logo_qr_width;
- $logo_qr_height = $logo_height/$scale;
- $from_width = ($QR_width - $logo_qr_width) / 2;
- imagecopyresampled($QR,$logo,$from_width,$from_width,0,0,$logo_qr_width,$logo_qr_height,$logo_width,$logo_height);//重新组合图片并调整大小
- imagepng($QR, $saveDir.$random.'_combine.png');//输出图片
- return '/qrcode/'.$random.'_combine.png';//组合logo后输出的二维码
- }
- /**
- * 生成固定名称的图片二维码
- * $unique 举例 userId_189 merchant_20等
- * $size 大小 5 195px 8 312px
- */
- public static function generateFixQrCode($url, $unique,$logo = '',$size=5)
- {
- $app = Yii::getAlias("@app");
- $saveDir = $app.'/../resource/images/qrcode/';
- include_once Yii::getAlias("@vendor/qrcode").'/phpqrcode.php';
- $random = $unique;
- //表示容错率,也就是有被覆盖的区域还能识别,分别是 L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%)
- $errorCorrectionLevel = 'H';
- $matrixPointSize = $size;//生成图片大小
- $QR = $saveDir.$random.'.png';
- if(file_exists($QR)){
- return '/qrcode/'.$random.'.png';
- }
- \QRcode::png($url, $QR, $errorCorrectionLevel, $matrixPointSize, 1);//最后的参数1表示空白间距
- if (!empty($logo) && file_exists($logo)) {
- $QR = imagecreatefromstring(file_get_contents($QR));
- $logo = imagecreatefromstring(file_get_contents($logo));
- $QR_width = imagesx($QR);//二维码图片宽度
- $QR_height = imagesy($QR);//二维码图片高度
- $logo_width = imagesx($logo);//logo图片宽度
- $logo_height = imagesy($logo);//logo图片高度
- $logo_qr_width = $QR_width / 5;
- $scale = $logo_width/$logo_qr_width;
- $logo_qr_height = $logo_height/$scale;
- $from_width = ($QR_width - $logo_qr_width) / 2;
- imagecopyresampled($QR,$logo,$from_width,$from_width,0,0,$logo_qr_width,$logo_qr_height,$logo_width,$logo_height);//重新组合图片并调整大小
- imagepng($QR, $saveDir.$random.'.png');//输出图片
- }
- return '/qrcode/'.$random.'.png';
- }
-
- /**
- * 生成会员二维码
- * $unique 举例 userId_189 merchant_20等
- * $size 大小 5 195px 8 312px
- */
- public static function card($url, $unique= '',$logo = '',$size=5)
- {
- $app = Yii::getAlias("@app");
- $saveDir = $app.'/../../resource/images/card/';
- include_once Yii::getAlias("@vendor/qrcode").'/phpqrcode.php';
- $rand = stringUtil::buildOrderNo();
- $random = empty($unique) ? $rand : $unique.'_'.$rand;
- //表示容错率,也就是有被覆盖的区域还能识别,分别是 L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%)
- $errorCorrectionLevel = 'H';
- $matrixPointSize = $size;//生成图片大小
- $QR = $saveDir.$random.'.png';
- \QRcode::png($url, $QR, $errorCorrectionLevel, $matrixPointSize, 1);//最后的参数1表示空白间距
- if (empty($logo) || file_exists($logo) == false) {
- return '/card/'.$random.'.png';//没有logo输出的二维码
- }
- $QR = imagecreatefromstring(file_get_contents($QR));
- $logo = imagecreatefromstring(file_get_contents($logo));
- $QR_width = imagesx($QR);//二维码图片宽度
- $QR_height = imagesy($QR);//二维码图片高度
- $logo_width = imagesx($logo);//logo图片宽度
- $logo_height = imagesy($logo);//logo图片高度
- $logo_qr_width = $QR_width / 5;
- $scale = $logo_width/$logo_qr_width;
- $logo_qr_height = $logo_height/$scale;
- $from_width = ($QR_width - $logo_qr_width) / 2;
- imagecopyresampled($QR,$logo,$from_width,$from_width,0,0,$logo_qr_width,$logo_qr_height,$logo_width,$logo_height);//重新组合图片并调整大小
- imagepng($QR, $saveDir.$random.'_combine.png');//输出图片
- return '/card/'.$random.'_combine.png';//组合logo后输出的二维码
- }
-
- public static function generatePayQrCode($url, $unique= '',$logo = '',$size=5)
- {
- $app = Yii::getAlias("@app");
- $saveDir = $app.'/../resource/images/qrcode/';
- include_once Yii::getAlias("@vendor/qrcode").'/phpqrcode.php';
- $rand = stringUtil::buildOrderNo();
- $random = empty($unique) ? $rand : $unique.'_'.$rand;
- //表示容错率,也就是有被覆盖的区域还能识别,分别是 L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%)
- $errorCorrectionLevel = 'H';
- $matrixPointSize = $size;//生成图片大小
- $QR = $saveDir.$random.'.png';
- \QRcode::png($url, $QR, $errorCorrectionLevel, $matrixPointSize, 1);//最后的参数1表示空白间距
- if (empty($logo) || file_exists($logo) == false) {
- return '/qrcode/'.$random.'.png';//没有logo输出的二维码
- }
- $QR = imagecreatefromstring(file_get_contents($QR));
- $logo = imagecreatefromstring(file_get_contents($logo));
- $QR_width = imagesx($QR);//二维码图片宽度
- $QR_height = imagesy($QR);//二维码图片高度
- $logo_width = imagesx($logo);//logo图片宽度
- $logo_height = imagesy($logo);//logo图片高度
- $logo_qr_width = $QR_width / 5;
- $scale = $logo_width/$logo_qr_width;
- $logo_qr_height = $logo_height/$scale;
- $from_width = ($QR_width - $logo_qr_width) / 2;
- imagecopyresampled($QR,$logo,$from_width,$from_width,0,0,$logo_qr_width,$logo_qr_height,$logo_width,$logo_height);//重新组合图片并调整大小
- imagepng($QR, $saveDir.$random.'_combine.png');//输出图片
- //return '/qrcode/'.$random.'_combine.png';//组合logo后输出的二维码
- $weixinImg = $app.'/web/images/merchant/wxPay.png';
- $combineQR = $saveDir.$random.'_combine.png';
-
- $weixinImgSource = imagecreatefromstring(file_get_contents($weixinImg));
- $combineQRSource = imagecreatefromstring(file_get_contents($combineQR));
-
- $weixinWidth = imagesx($weixinImgSource);
- $weixinHeight = imagesy($weixinImgSource);
- $combineQRWidth = imagesx($combineQRSource);
- $combineQRHeight = imagesy($combineQRSource);
- $combineQRQRWidth = $weixinWidth / 5;
- $scale = $combineQRWidth / $combineQRQRHeight;
- $combineQRQRWidth = '';
-
- }
-
- }
|