php生成二维码

2021-06-12 21:04

阅读:620

标签:bsp   二维码   ror   oms   obj   str   led   new   int   

//https://sourceforge.net/projects/phpqrcode/files/   下载地址

include ‘phpqrcode/phpqrcode.php‘;

class MyQrcode{

    /*
     * 普通生成二维码
     * @param string $url
     * @param int $size
     * @param string $output_path
     * @param string $filename
     * @return array
     */
    public function createNormal($url=‘‘,$size=4,$output_path=‘‘,$filename=‘‘)
    {
        if(empty($url))
        {
            return array(‘result‘=>"fail",‘msg‘=>"url参数为空");
        }

        if(empty($path))
        {
            $output_path=‘./‘;
        }

        if(empty($filename))
        {
            $filename=md5(time()).‘.png‘;
        }

        //容错级别
        $errorCorrenctionLevel = ‘l‘;

        Qrcode::png($url,$output_path.$filename,$errorCorrenctionLevel,$size,2);

        return array(‘result‘=>‘success‘,‘msg‘=>"生成成功",‘imgurl‘=>$output_path.$filename);
    }

    public function createHasCenterImage($url=‘‘,$centerImg=‘‘,$size=4,$output_path=‘‘,$output_filename=‘‘,$tempfile=‘temp.png‘)
    {
        if(empty($url))
        {
            return array(‘result‘=>"fail",‘msg‘=>"url参数为空");
        }

        if(empty($path))
        {
            $output_path=‘./‘;
        }

        if(empty($output_filename))
        {
            $output_filename=md5(time()).‘.png‘;
        }
        if(empty($centerImg))
        {
            return array(‘result‘=>"fail",‘msg‘=>"中心图片参数为空");
        }

        if(!file_exists($centerImg))
        {
            return array(‘result‘=>"fail",‘msg‘=>"中心图片不存在");
        }



        //容错级别
        $errorCorrenctionLevel = ‘l‘;

        Qrcode::png($url,$tempfile,$errorCorrenctionLevel,$size,2);

        if(!file_exists($tempfile))
        {
            return array(‘result‘=>"fail",‘msg‘=>"生成临时二维码失败");
        }


        $logo = $centerImg;
        $qrcode = $tempfile;
        $logo = imagecreatefromstring(file_get_contents($logo));
        $qrcode = imagecreatefromstring(file_get_contents($qrcode));
        $QR_width = imagesx($qrcode);//二维码图片宽度
        $QR_height = imagesy($qrcode);//二维码图片高度
        $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($qrcode, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,$logo_qr_height, $logo_width, $logo_height);
        //保存生成的二维码
        $r= imagepng($qrcode,$output_path.$output_filename);


        //删除临时的二维码图片
        @unlink($tempfile);


        if($r==true)
        {
            return array(‘result‘=>"success",‘msg‘=>"生成二维码成功",‘imgurl‘=>$output_path.$output_filename);
        }
        else
        {
            return array(‘result‘=>"fail",‘msg‘=>"生成二维码失败");
        }
    }
}


$obj=new MyQrcode();
$obj->createNormal(‘http://www.baidu.com‘,4,‘./‘.‘tt.png‘);

 

php生成二维码

标签:bsp   二维码   ror   oms   obj   str   led   new   int   

原文地址:http://www.cnblogs.com/norm/p/7286317.html


评论


亲,登录后才可以留言!