使用PHP二维码生成类库PHP QR Code生成二维码
来源:互联网 发布:架子鼓软件下载 编辑:程序博客网 时间:2024/05/22 10:53
使用PHP二维码生成类库PHP QR Code生成二维码思路:
1.首先生成二维码;
2.生成带logo的二维码;
3.把带logo的二维码传到阿里云(阿里云是我们的图片储存的地方)
4.把二维码的图片地址入库invite_code
5.在一开始的查看库中是否与图片生成;如果有图片路径则不需要再次生成
解释说明PHP二维码生成类库PHP QR Code:
PHP QR Code是一个PHP二维码生成类库,利用它可以轻松生成二维码,
官网提供了下载和多个演示demo,查看地址:http://phpqrcode.sourceforge.net/。
下载官网提供的类库后,只需要使用phpqrcode.php就可以生成二维码了,当然您的PHP环境必须开启支持GD2。
phpqrcode.php提供了一个关键的png()方法,
参数
参数
参数
public static function png($text, $outfile=false, $level=QR_ECLEVEL_L, $size=3, $margin=4, $saveandprint=false) { $enc = QRencode::factory($level, $size, $margin); return $enc->encodePNG($text, $outfile, $saveandprint=false); }
那如何生成含有logo的二维码呢?
其实原理很简单,先使用PHP QR Code生成一张二维码图片,然后再利用php的image相关函数,将事先准备好的logo图片加入到刚生成的原始二维码图片中间,然后重新生成一张新的二维码图片。
//生成二维码 public function user_mycode(Request $req) { include(dirname($_SERVER["DOCUMENT_ROOT"]) . '\app\Helpers\phpqrcode.php'); new QRcode(); $user_info = Auth::user(); $uid = $user_info['user_id']; //判断用户是否登录 if (empty($uid) || !isset($uid)) { return response()->json(['status' => 201, 'error' => '请先登录!']); } //查询出当前用户的邀请码 $lotteryList = DB::table('ecs_users') ->select('my_invite_code', 'invite_qrcode') ->where('user_id', $uid) ->first(); $invite_qrcode = $lotteryList->invite_qrcode; if (count($invite_qrcode) > 0 && !empty($invite_qrcode)) { return response()->json(['status' => 200, 'error' => 'ok', 'data' => $invite_qrcode]); } $code = $lotteryList->my_invite_code; $value = 'https://m.mallmg.com/?#/register?invite_code=' . $code; //二维码内容 $errorCorrectionLevel = 'M'; //容错级别 $matrixPointSize = 10; //生成图片大小 // 生成二维码图片 QRcode::png($value, 'qrcode_' . $uid . '.png', $errorCorrectionLevel, $matrixPointSize, 2); //生成中间带logo的二维码 $logo = $_SERVER["DOCUMENT_ROOT"] . '\dirk.jpg'; // logo图片是你自己放到文件夹里的 $QR = 'qrcode_' . $uid . '.png'; //已经生成的原始二维码图 if ($logo !== FALSE) { $QR = imagecreatefromstring(file_get_contents($QR));//获取已经生成的原始二维码 $logo = imagecreatefromstring(file_get_contents($logo));//获取二维码中间的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); } $root_path = $_SERVER["DOCUMENT_ROOT"]; $target = 'qrWithLogo_' . $uid . '.png'; //输出图片 imagepng($QR, $target); //二维码上传到阿里云 $now = time() . mt_rand(1111, 9999);//当前时间 $fileName = "qrcode{$uid}_{$now}" . ".png";//上传文件之后的名称 $path = "images/qrcode/" . $fileName;//阿里上传之后的路径 try { //上传阿里云 $ossClient = new OssClient(config("alioss.accesskey_id"), config("alioss.accesskey_secret"), config("alioss.endpoint")); $ossClient->uploadFile(config("alioss.bucket"), $path, $root_path . '/' . $target); unlink($_SERVER["DOCUMENT_ROOT"] . "/" . 'qrcode_' . $uid . '.png'); unlink($_SERVER["DOCUMENT_ROOT"] . "/" . $target); $res = DB::table('ecs_users') ->where('user_id', $uid) ->update(['invite_qrcode' => $path]); if (!$res) { return response()->json(['status' => 202, 'error' => '修改失败']); } } catch (OssException $e) { return response()->json(['status' => 203, 'error' => '二维码上传失败', "message" => $e->getMessage()], 400); } return response()->json(['status' => 200, 'error' => 'ok', 'data' => $path]); }
补充:
由于二维码允许有一定的容错性,一般的二维码即使在遮住部分但仍然能够解码,经常我们扫描二维码的时候扫描到甚至不到一半时就能解码扫描结果,这是因为生成器会将部分信息重复表示来提高其容错度,这就是为什么我们在二维码中间加个LOGO图片并不影响解码结果的原因。
- 使用PHP二维码生成类库PHP QR Code生成二维码
- 使用PHP二维码生成类库PHP QR Code生成二维码
- 使用PHP二维码生成类库PHP QR Code生成二维码
- 使用PHP二维码生成类库PHP QR Code生成二维码
- PHP 【利用二维码生成库 PHP QR Code 生成二维码】
- PHP QR Code生成二维码
- PHP QR Code 生成二维码
- PHP QR Code生成二维码
- PHP QR Code生成二维码
- 使用PHP QR Code生成二维码
- 使用PHP QR Code生成二维码
- 使用PHP QR Code生成二维码
- 使用PHP QR Code生成二维码
- 使用PHP QR Code生成二维码
- 使用PHP QR Code生成二维码
- 使用PHP QR Code生成二维码
- 使用PHP QR Code生成二维码
- PHP利用PHP QR Code二维码类生成二维码
- iOS 11用户调查报告显示:电池续航时间仅为iOS 10一半
- 李彦宏:《意见》是鼓舞企业家迎接全球创新“中国时刻”的号角
- 结构体排序问题
- ARP介绍
- 集合框架List\Set
- 使用PHP二维码生成类库PHP QR Code生成二维码
- caffe安装 Ubuntu 14.04 + CUDA8.0 + cuDNN v5 + caffe的简要版
- Spring项目转化Spring Web项目插件
- 云栖大会之前 阿里云解释企业云安全架构逻辑
- 英特尔推出全新自主学习芯片加速人工智能发展
- iPhone X会让苹果再次“复兴”么?
- Qt简介
- 诠析亚马逊6大新品:阵线腾挪、场景进攻和建造中枢
- PIL-图像处理