您的位置:首页 > 编程语言 > PHP开发

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

2015-06-26 17:14 639 查看
$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,'生成失败','');
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: