<?php
namespace app\common\util;

class QrcodeUtil
{

    /**
     * @param $yqcode 二维码内容
     * @param int $type 0=二维码输出内容 1=保存二维码到指定位置
     * @param string $savepath
     * @return bool|false|resource
     */
    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;
        }
    }

    //添加LOGO
    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图片宽度
        $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(
            $QRBuffer,
            $logo,
            $from_width,
            $from_width,
            0,
            0,
            $logo_qr_width,
            $logo_qr_height,
            $logo_width,
            $logo_height
        );
        return $QRBuffer;
    }

    //浏览器默认输出为72DPI,打印要求为300DPI
    public static function print72dip300dip($QRBufferStr)
    {
        //数据块长度为9
        $len = pack("N", 9);
        //数据块类型标志为pHYs
        $sign = pack("A*", "pHYs");
        //X方向和Y方向的分辨率均为300DPI(1像素/英寸=39.37像素/米),单位为米(0为未知,1为米)
        $data = pack("NNC", 300 * 39.37, 300 * 39.37, 0x01);
        //CRC检验码由数据块符号和数据域计算得到
        $checksum = pack("N", crc32($sign . $data));
        $phys = $len . $sign . $data . $checksum;
        $pos = strpos($QRBufferStr, "pHYs");
        if ($pos > 0) {
            //修改pHYs数据块
            $QRBufferStr = substr_replace($QRBufferStr, $phys, $pos - 4, 21);
        } else {
            //IHDR结束位置(PNG头固定长度为8,IHDR固定长度为25)
            $pos = 33;
            //将pHYs数据块插入到IHDR之后
            $QRBufferStr = substr_replace($QRBufferStr, $phys, $pos, 0);
        }
        return $QRBufferStr;
    }

}