使用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,'生成失败',''); } } } }
相关文章推荐
- PHP关联数组实现根据元素值删除元素的方法
- 《Windows服务器配置与管理 》FTP站点服务器
- php中函数前加&符号的作用分解
- 安卓数据存储之ContentProvider
- PHP调用JAVA方法
- 使用ExitProcess()结束本进程,TerminateProcess()结束进程
- PHP实现事件机制实例分析
- php 快速排序
- php使用MySQL保存session会话的方法
- PHP快速读取CSV大文件
- php面向对象魔术方法
- ThinkPHP中的或,且结合查询方法
- PHP与安卓和水果对接上传处理
- php加载外部文件类的3.5个方法
- php -- 判断文件是否存在
- [php] Thinkphp控制器继承
- PHP参考代码1
- php使用workerman自定义协议
- thinkphp对数据库操作有哪些内置函数
- PHPExcel导出数据时字段超过26列出错Invalid cell coordinate [1