<?php


namespace app\lib\utils;

class QrcodeUtil
{
    /**二维码生成路径
     * @param $data
     * @return string
     */
    public static function mk_dir($data){
        $dir = date("d");
        $path =ROOT_PATH.'/public/attachs/'. date("Y").'/'.date("m").'/';
        if(is_dir($path .$dir)){
            return $dir;
        }else{
            mkdir($path .$dir,0777,true);
            return $dir;
        }
    }
    /**生成普通二维码
     * @param $url
     * @param int $size
     * @return string
     */
    public static function createQrcode($url,$data=array(),$qrcodeName,$size = 16,$logo = ''){
        $dir = self::mk_dir($data);
        $qrcodeName=$qrcodeName?$qrcodeName:time();//如果名称不存在默认时间戳
        $file = date("Y").'/'.date("m").'/'.$dir.'/'.$qrcodeName.'.png';
        $fileName  =ROOT_PATH.'/public/attachs/'.$file;
        if(!(is_file($fileName)) || !file_exists($fileName)){   //判断二维码是否存在
            vendor('phpqrcode.phpqrcode');
            // ob_end_clean();//会清除缓冲区的内容，并将缓冲区关闭，但不会输出内容。
            $level = 'H';
            $data = $url;
            \QRcode::png($data, $fileName, $level, $size,2,true);
        }
        if($logo){
            $logo = ROOT_PATH.$logo;//需要显示在二维码中的Logo图像
            $QR = $fileName;//'F:\WWW\luma_new\attachs\weixin\upload\qrcode\type_ele_activity_id\2020-12-09\1.png';//$fileName;
            if ($logo !== FALSE) {
                $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_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 ( $QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height );
            }
            imagepng ( $QR, $fileName );//带Logo二维码的文件名
        }
        return $file;
    }


    /**
     * 创建海报图片
     * @param array $product
     * @return bool|string
     */
    function createPoster($product = array()){
        header("Content-type: image/jpg");
        $filePath = 'public/uploads/poster/'.time().'.jpg';
        $dir = iconv("UTF-8", "GBK", "public/uploads/poster");
        if(!file_exists($dir)) mkdir($dir,0775,true);
        $config = array(
            'text'=>array(
                array(
                    'text'=>substr(iconv_substr($product['store_name'],0,mb_strlen($product['store_name']),'utf-8'),0,24).'...',
                    'left'=>50,
                    'top'=>500,
                    'fontPath'=>ROOT_PATH.'public/static/font/simsunb.ttf',     //字体文件
                    'fontSize'=>16,             //字号
                    'fontColor'=>'40,40,40',       //字体颜色
                    'angle'=>0,
                ),
                array(
                    'text'=>'￥'.$product['price'],
                    'left'=>50,
                    'top'=>580,
                    'fontPath'=>ROOT_PATH.'public/static/font/simsunb.ttf',     //字体文件
                    'fontSize'=>16,             //字号
                    'fontColor'=>'40,40,40',       //字体颜色
                    'angle'=>0,
                )
            ),
            'image'=>array(
                array(
                    'url'=>$product['image'],
                    'left'=>50,
                    'top'=>70,
                    'right'=>0,
                    'stream'=>0,
                    'bottom'=>0,
                    'width'=>350,
                    'height'=>350,
                    'opacity'=>100,
                ),
                array(
                    'url'=>$product['code_path'],
                    'left'=>250,
                    'top'=>480,
                    'right'=>0,
                    'stream'=>0,
                    'bottom'=>0,
                    'width'=>160,
                    'height'=>180,
                    'opacity'=>100,
                ),
            ),
            'background'=>ROOT_PATH.UPLOAD_PATH.'/poster/background.jpg'          //背景图
        );
        $imageDefault = array(
            'left'=>0,
            'top'=>0,
            'right'=>0,
            'bottom'=>0,
            'width'=>100,
            'height'=>100,
            'opacity'=>100
        );
        $textDefault = array(
            'text'=>'2222222222',
            'left'=>0,
            'top'=>0,
            'fontSize'=>32,       //字号
            'fontColor'=>'255,255,255', //字体颜色
            'angle'=>0,
        );
        $background = $config['background'];//海报最底层得背景
        //背景方法
        $backgroundInfo = getimagesize($background);
        $backgroundFun = 'imagecreatefrom'.image_type_to_extension($backgroundInfo[2], false);
        $background = $backgroundFun($background);
        // $backgroundWidth = imagesx($background);  //背景宽度
        // $backgroundHeight = imagesy($background);  //背景高度
        $backgroundWidth = 460;  //背景宽度
        $backgroundHeight = 700;  //背景高度
        $imageRes = imageCreatetruecolor($backgroundWidth,$backgroundHeight);
        $color = imagecolorallocate($imageRes, 0, 0, 0);
        imagefill($imageRes, 0, 0, $color);
        // imageColorTransparent($imageRes, $color);  //颜色透明
        imagecopyresampled($imageRes,$background,0,0,0,0,imagesx($background),imagesy($background),imagesx($background),imagesy($background));   //处理了图片
        if(!empty($config['image'])){
            foreach ($config['image'] as $key => $val) {
                $val = array_merge($imageDefault,$val);
                $info = getimagesize($val['url']);
                $function = 'imagecreatefrom'.image_type_to_extension($info[2], false);
                if($val['stream']){   //如果传的是字符串图像流
                    $info = getimagesizefromstring($val['url']);
                    $function = 'imagecreatefromstring';
                }
                $res = $function($val['url']);
                $resWidth = $info[0];
                $resHeight = $info[1];
                //建立画板 ，缩放图片至指定尺寸
                $canvas=imagecreatetruecolor($val['width'], $val['height']);
                imagefill($canvas, 0, 0, $color);
                //关键函数，参数（目标资源，源，目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h）
                imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'],$resWidth,$resHeight);
                $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']) - $val['width']:$val['left'];
                $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']) - $val['height']:$val['top'];
                //放置图像
                imagecopymerge($imageRes,$canvas, $val['left'],$val['top'],$val['right'],$val['bottom'],$val['width'],$val['height'],$val['opacity']);//左，上，右，下，宽度，高度，透明度
            }   }
        //处理文字
        if(!empty($config['text'])){
            foreach ($config['text'] as $key => $val) {
                $val = array_merge($textDefault,$val);
                list($R,$G,$B) = explode(',', $val['fontColor']);
                $fontColor = imagecolorallocate($imageRes, $R, $G, $B);
                $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']):$val['left'];
                $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']):$val['top'];
                imagettftext($imageRes,$val['fontSize'],$val['angle'],$val['left'],$val['top'],$fontColor,$val['fontPath'],$val['text']);
            }
        }
        //生成图片
        $res = imagejpeg ($imageRes,$filePath,90); //保存到本地
        imagedestroy($imageRes);
        if(!$res) return false;
        return $filePath;
    }

}