当前位置: 首页 > Linux

PHP-生成二维码(qr-code)

时间:2023-04-06 11:25:52 Linux

1。为什么写这篇文章?最近在做一个项目,生成一个二维码供用户跳转。搜索了一下,发现网上用的是一个叫phpqrcode的扩展。在github上搜索了一下,发现这个项目的作者6年前就没有维护过。百度的文章也是如此。你复制我的,我复制你的,所以我必须在github上查看是否有更好的PHP生成。二维码的扩展,后来找到了一个名为qr-code的扩展,感觉不错,作者一直在维护,很好用。所以这里就介绍一下这个扩展的安装和使用,方便大家开发。二维码项目的github地址为:qr-code2。安装二维码。这里我们通过composer安装扩展。Composer也是比较流行的包管理工具。如果你不太了解作曲家,你可以先看看。文章:《php-composer的安装与使用方法》我的环境是linux,我们输??入以下命令来安装扩展:composerrequireendroid/qr-code当扩展安装完成后,我们就可以开始下面的操作了。3、要生成二维码,首先我们需要在项目中引入qr-code类文件。Composer现在基本上是通过psr-4"namespace":"path"自动加载的,它的位置位于扩展名composer.json文件的根目录下。好了,现在我们引入qr-code类文件,尝试输出一个简单的二维码。useEndroid\QrCode\QrCode;//$content一般是url地址,当然也可以是文本内容$content='http://www.baidu.com?rand='。兰特(1000,9999);$qrCode=newQrCode($content);//指定内容类型header('Content-Type:'.$qrCode->getContentType());//输出二维码echo$qrCode->writeString();好了,当指定了内容类型后,二维码会直接输出到页面上。如何将这种直接输出二维码应用到项目中?一般直接写在html中的标签中,例如:这样二维码就可以显示在页面的任意位置。当然,我们也可以将其存储在文件中,生成任意格式的图片,例如:$qrCode->writeFile(__DIR__.'/qrcode.png');这样我们就可以根据图片路径在页面上显示两个文件。二维码4.简单示例文件及常用参数介绍这里贴出一个简单的类处理文件,介绍一下二维码的一些常用参数。类文件:namespace'命名空间';useEndroid\QrCode\ErrorCorrectionLevel;useEndroid\QrCode\LabelAlignment;useEndroid\QrCode\QrCode;classQrcodeComponent{protected$_qr;受保护的$_encoding='UTF-8';保护$_size=300;受保护的$_logo=false;保护$_logo_url='';保护$_logo_size=80;受保护的$_title=false;保护$_title_content='';保护$_generate='显示';//display-直接显示writefile-写入文件constMARGIN=10;constWRITE_NAME='png';constFOREGROUND_COLOR=['r'=>0,'g'=>0,'b'=>0,'a'=>0];constBACKGROUND_COLOR=['r'=>255,'g'=>255,'b'=>255,'a'=>0];公共函数__construct($config){isset($config['generate'])&&$this->_generate=$config['generate'];isset($config['encoding'])&&$this->_encoding=$config['encoding'];isset($config['size'])&&$this->_size=$config['size'];isset($config['display'])&&$this->_size=$config['size'];isset($config['logo'])&&$this->_logo=$config['logo'];isset($config['logo_url'])&&$this->_logo_url=$config['logo_url'];isset($config['logo_size'])&&$this->_logo_size=$config['logo_size'];isset($config['title'])&&$this->_title=$config['title'];isset($config['title_content'])&&$this->_title_content=$config['title_content'];}/***生成二维码*@param$content需要写入的内容*@returnarray|页面输入*/publicfunctioncreate($content){$this->_qr=newQrCode($content);$this->_qr->setSize($this->_size);$this->_qr->setWriterByName(self::WRITE_NAME);$this->_qr->setMargin(self::MARGIN);$this->_qr->setEncoding($this->_encoding);$this->_qr->setErrorCorrectionL水平(错误修正级别::高);$this->_qr->setForegroundColor(self::FOREGROUND_COLOR);$this->_qr->setBackgroundColor(self::BACKGROUND_COLOR);if($this->_title){$this->_qr->setLabel($this->_title_content,16,'字体地址',LabelAlignment::CENTER);}if($this->_logo){$this->_qr->setLogoPath($this->_logo_url);$this->_qr->setLogoWidth($this->_logo_size);$this->_qr->setRoundBlockSize(true);}$this->_qr->setValidateResult(false);if($this->_generate=='display'){//前端调用例如:header('Content-Type:'.$这个->_qr->getContentType());返回$this->_qr->writeString();}elseif($this->_generate=='writefile'){返回$this->_qr->writeString();}埃尔se{return['success'=>false,'message'=>'找不到生成类型','data'=>''];}}/***生成文件*@param$file_name目录文件例如:/tmp*@returnarray*/publicfunctiongenerateImg($file_name){$file_path=$file_name.DS。唯一标识符()。'.'.自我::WRITE_NAME;如果(!file_exists($file_name)){mkdir($file_name,0777,true);}尝试{$this->_qr->writeFile($file_path);$data=['url'=>$file_path,'ext'=>self::WRITE_NAME,];return['success'=>true,'message'=>'writeqrimgsuccess','data'=>$data];}catch(Exception$e){return['success'=>false,'message'=>$e->getMessage(),'data'=>''];}}}使用方法:use'命名空间';$qr_url='http://www.baidu.com?id='.rand(1000,9999);$file_name='/tmp';//直接输出$qr_code=newQrcodeComponent();$qr_img=qr_code->create($qr_url);echo$qr_img;//生成文件$config=['generate'=>'writefile',];$qr_code=newQrcodeComponent($config);$qr_img=$qr_code->create($qr_url);$rs=$qr_code->generateImg($file_name);打印_r($rs);常用参数说明:setSize-二维码的大小pxsetWriterByName-写入文件的后缀名setMargin-二维码内容相对整张图片的外边距setEncoding-编码类型setErrorCorrectionLevel-容错级别,分为L,M,Q,H四级setForegroundColor-前景色setBackgroundColor-背景色setLabel-二维码标签setLogoPath-两个二维码标识路径setLogoWidth-二维码标识大小px