123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- namespace app\common\util;
- class QrcodeUtil
- {
-
- static public function create($yqcode,$type=0,$savepath='',$size=6){
- try{
-
- $errorCorrectionLevel = 3 ;
- $matrixPointSize = $size;
- $temp = './uploads/temp/'.md5($yqcode).mt_rand(1000,9999).'.png';
- if($type == 1){
- $dirfile = dirname($savepath);
- if(!file_exists($dirfile)){
- @mkdir($dirfile,0777);
- }
- $matrixPointSize = 4;
- }
-
- if (file_exists($temp)) {
- @unlink($temp);
- }
-
- include_once env('root_path').'/extend/phpqrcode/phpqrcode.php';
- \QRcode::png($yqcode, $temp, $errorCorrectionLevel, $matrixPointSize, 2);
- $logo = env('root_path').'/public/logo.png';
- if (!empty($logo)) {
- $QRBuffer = self::addLogoQrcode($temp, $logo);
- } else {
- $QRBuffer = imagecreatefromstring(self::print72dip300dip(file_get_contents($temp)));
- }
-
- if (file_exists($temp)) {
- @unlink($temp);
- }
- if($type == 0){
- return $QRBuffer;
- }else{
- imagejpeg($QRBuffer,$savepath,100);
- ImageDestroy($QRBuffer);
- return true;
- }
- }catch (\Exception $e){
- return false;
- }
- }
-
- public static function addLogoQrcode($QRTempPath, $logoPath)
- {
- $QRBuffer = imagecreatefromstring(self::print72dip300dip(file_get_contents($QRTempPath)));
- $logo = imagecreatefromstring(self::print72dip300dip(file_get_contents($logoPath)));
- $QR_width = imagesx($QRBuffer);
- $QR_height = imagesy($QRBuffer);
- $logo_width = imagesx($logo);
- $logo_height = imagesy($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(
- $QRBuffer,
- $logo,
- $from_width,
- $from_width,
- 0,
- 0,
- $logo_qr_width,
- $logo_qr_height,
- $logo_width,
- $logo_height
- );
- return $QRBuffer;
- }
-
- public static function print72dip300dip($QRBufferStr)
- {
-
- $len = pack("N", 9);
-
- $sign = pack("A*", "pHYs");
-
- $data = pack("NNC", 300 * 39.37, 300 * 39.37, 0x01);
-
- $checksum = pack("N", crc32($sign . $data));
- $phys = $len . $sign . $data . $checksum;
- $pos = strpos($QRBufferStr, "pHYs");
- if ($pos > 0) {
-
- $QRBufferStr = substr_replace($QRBufferStr, $phys, $pos - 4, 21);
- } else {
-
- $pos = 33;
-
- $QRBufferStr = substr_replace($QRBufferStr, $phys, $pos, 0);
- }
- return $QRBufferStr;
- }
- }
|