使用PHP QR Code生成二维码事例应用

$uid = (int) $_POST['uid'];
        $key = $_POST['key'];
        $account = $_POST['account'];
        $type = $_POST['type'];
//                $uid = 23;
//	 	$key = 1;
//	 	$account = 'http://blog.csdn.net/mengke1124';
//	 	$type = 2;
        if (empty($uid) || empty($key) || empty($account) || empty($type)) {
            $this->ajaxMessage(1,'参数错误');
        }
        if (!in_array($type,array(1,2))) {
            $this->ajaxMessage(1,'类型参数错误');
        }
        //$this->isLogin($uid, $key);
        if ($type == 1) {
            //如果已经存在二维码则直接使用
            $code = new User();
            if ($code->userCode($uid)) {
                $path = $code->userCode($uid);
                echo CHtml::image($path,'个人用户二维码',array('width' => '155px','height' => '150px'));
                $this->ajaxMessage(0,'生成成功',$path);
                exit;
            }

            include 'phpqrcode.php';
            $value = $account; //二维码内容   
            $errorCorrectionLevel = 'L'; //容错级别   
            $matrixPointSize = 20; //生成图片大小   
            //生成二维码图片   
            QRcode::png($value,'qrcode.png',$errorCorrectionLevel,$matrixPointSize,2);
            $QR = 'qrcode.png'; //已经生成的原始二维码图 
            $user = new User();
            $userlogo = $user->userAvatar($uid); //根据用户id获取用户头像logo
            // $logo = './uploads/logo.jpg';//准备好的logo图片
            $logo = '.' . $userlogo; //准备好的用户头像logo图片

            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图片宽度   
                $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);
            }

            $code_folder = "uploads/" . "code/" . date("Y") . "/" . date("m") . "/" . date("d") . "/"; //上传二维码路径
            if (!file_exists($code_folder)) {//检查二维码目录是否存在
                mkdir($code_folder,0777,true);  //mkdir("temp/sub, 0777, true);递归创建目录
            }
            $code = date('Ymd',time()) . rand(11111,99999) . '_' . rand(11111,99999);

            //保存二维码到指定目录   
            if (imagepng($QR,'./' . $code_folder . '/' . $code . '.png')) {
                $model = new UserDetail();
                $model->uid = $uid;
                $model->code = '/' . $code_folder . $code . '.png';
                if ($model->save(false)) {
                    echo CHtml::image('/' . $code_folder . $code . '.png','个人用户二维码',array('width' => '155px','height' => '150px'));
                    $this->ajaxMessage(0,'生成成功','/' . $code_folder . $code . '.png');
                } else {
                    $this->ajaxMessage(2,'生成失败','');
                }
            }
        } elseif ($type == 2) {
            //如果已经存在二维码则直接使用
            $code = new User();
            if ($code->userCode($uid)) {
                $path = $code->userCode($uid);
                echo CHtml::image($path,'群二维码',array('width' => '155px','height' => '150px'));
                $this->ajaxMessage(0,'生成成功',$path);
                exit;
            }

            include 'phpqrcode.php';
            $value = $account; //二维码内容   
            $errorCorrectionLevel = 'L'; //容错级别   
            $matrixPointSize = 20; //生成图片大小   
            //生成二维码图片   
            QRcode::png($value,'qrcode.png',$errorCorrectionLevel,$matrixPointSize,2);
            $QR = 'qrcode.png'; //已经生成的原始二维码图 
            $user = new User();
            $userlogo = $user->userAvatar($uid); //根据用户id获取用户头像logo
            // $logo = './uploads/logo.jpg';//准备好的logo图片
            $logo = '.' . $userlogo; //准备好的用户头像logo图片

            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图片宽度   
                $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);
            }

            $code_folder = "uploads/" . "code/" . date("Y") . "/" . date("m") . "/" . date("d") . "/"; //上传二维码路径
            if (!file_exists($code_folder)) {//检查二维码目录是否存在
                mkdir($code_folder,0777,true);  //mkdir("temp/sub, 0777, true);递归创建目录
            }
            $code = date('Ymd',time()) . rand(11111,99999) . '_' . rand(11111,99999);

            //保存二维码到指定目录   
            if (imagepng($QR,'./' . $code_folder . '/' . $code . '.png')) {
                $model = new UserDetail();
                $model->uid = $uid;
                $model->code = '/' . $code_folder . $code . '.png';
                if ($model->save(false)) {
                    echo CHtml::image('/' . $code_folder . $code . '.png','群二维码',array('width' => '155px','height' => '150px'));
                    $this->ajaxMessage(0,'生成成功','/' . $code_folder . $code . '.png');
                } else {
                    $this->ajaxMessage(2,'生成失败','');
                }
            }
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值