| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- <?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 = '';
- }
- /**
- * 生成收款二维码
- * $unique 举例 userId_189 merchant_20等
- * $size 大小 5 195px 8 312px
- */
- public static function generateGatheringQrCode($url, $sjId, $shopId, $logo = '', $size = 5)
- {
- include_once Yii::getAlias("@vendor/qrcode") . '/phpqrcode.php';
- //表示容错率,也就是有被覆盖的区域还能识别,分别是 L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%)
- $errorCorrectionLevel = 'H';
- $matrixPointSize = $size;//生成图片大小
- $prefix = dirUtil::getImgDir();
- $middle = 'retail/qrCode/gathering/';
- if (file_exists($prefix . $middle) == false) {
- mkdir($prefix . $middle, 0777, true);
- }
- $behind = $middle . $sjId . '_' . $shopId . '.png';
- $saveFile = $prefix . $behind;
- if (file_exists($saveFile) == false) {
- \QRcode::png($url, $saveFile, $errorCorrectionLevel, $matrixPointSize, 1);//最后的参数1表示空白间距
- oss::uploadImage($behind, $saveFile);
- }
- return $behind;
- }
- public static function generateShortTimeGatheringQrCode($url, $sjId, $shopId, $payWay, $logo = '', $size = 5)
- {
- include_once Yii::getAlias("@vendor/qrcode") . '/phpqrcode.php';
- //表示容错率,也就是有被覆盖的区域还能识别,分别是 L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%)
- $errorCorrectionLevel = 'H';
- $matrixPointSize = $size;//生成图片大小
- $prefix = dirUtil::getImgDir();
- $middle = 'retail/qrCode/gathering2/' . $sjId . '/' . $shopId . '/';
- if (file_exists($prefix . $middle) == false) {
- mkdir($prefix . $middle, 0777, true);
- }
- $behind = $middle . $payWay . '_' . md5($url) . '.png';
- $saveFile = $prefix . $behind;
- if (file_exists($saveFile) == false) {
- \QRcode::png($url, $saveFile, $errorCorrectionLevel, $matrixPointSize, 1);//最后的参数1表示空白间距
- oss::uploadImage($behind, $saveFile);
- }
- return $behind;
- }
- //h5商城 ssh 2020.4.30
- public static function generateH5MallQrCode($url, $logo = '', $size = 5)
- {
- include_once Yii::getAlias("@vendor/qrcode") . '/phpqrcode.php';
- //表示容错率,也就是有被覆盖的区域还能识别,分别是 L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%)
- $errorCorrectionLevel = 'H';
- $matrixPointSize = $size;//生成图片大小
- $prefix = dirUtil::getImgDir();
- $middle = 'retail/qrCode/h5Mall/';
- if (file_exists($prefix . $middle) == false) {
- mkdir($prefix . $middle, 0777, true);
- }
- $behind = $middle . md5($url) . '.png';
- $file = $prefix . $behind;
- $imgUrl = imgUtil::getPrefix() . $behind;
- if (file_exists($file) == false) {
- \QRcode::png($url, $file, $errorCorrectionLevel, $matrixPointSize, 1);//最后的参数1表示空白间距
- }
- return ['shortUrl' => $behind, 'url' => $imgUrl];
- }
- }
|