yii 整合phpqrcode生成二维码附在线演示地址,yiiphpqrcode
1,先到官网下载包 http://phpqrcode.sourceforge.net/
下载官网提供的类库后,只需要使用phpqrcode.php就可以生成二维码了,当然您的PHP环境必须开启支持GD2。
phpqrcode.php提供了一个关键的png()方法,其中
参数$text表示生成二位的的信息文本;
参数$outfile表示是否输出二维码图片 文件,默认否;
参数$level表示容错率,也就是有被覆盖的区域还能识别,分别是 L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%);
参数$size表示生成图片大小,默认是3;参数$margin表示二维码周围边框空白区域间距值;
参数$saveandprint表示是否保存二维码并显示。
2,下载后把解压后的phpqrcode文件夹放到extensions文件夹下,如下图:
3,引入类 phpqrcode
Yii::$enableIncludePath = false; Yii::import ('application.extensions.phpqrcode.phpqrcode', 1 );
下面是完整的生成二维码的方法
public function actionQrcode(){ $this->breadcrumbs=array_merge($this->breadcrumbs,array( '生成二维码' )); $qrcode_path=''; $file_tmp_name=''; $errors=array(); if(!empty($_POST)){ $content = trim($_POST['content']); //二维码内容 $contentSize=$this->getStringLength($content); if($contentSize>290){ $errors[]='字数过长,不能多于150个字符!'; } Yii::$enableIncludePath = false; Yii::import ('application.extensions.phpqrcode.phpqrcode', 1 ); if(isset($_FILES['upimage']['tmp_name']) && $_FILES['upimage']['tmp_name'] && is_uploaded_file($_FILES['upimage']['tmp_name'])){ if($_FILES['upimage']['size']>512000){ $errors[]="你上传的文件过大,最大不能超过500K。"; } $file_tmp_name=$_FILES['upimage']['tmp_name']; $fileext = array("image/pjpeg","image/jpeg","image/gif","image/x-png","image/png"); if(!in_array($_FILES['upimage']['type'],$fileext)){ $errors[]="你上传的文件格式不正确,仅支持 png, jpg, gif格式。"; } } $tpgs=$_POST['tpgs'];//图片格式 $bas_path=dirname ( Yii::app ()->BasePath ); $qrcode_bas_path=$bas_path.'/upload/qrcode/'; if(!is_dir($qrcode_bas_path)){ mkdir($qrcode_bas_path, 0777, true); } $uniqid_rand=date("Ymdhis").uniqid(). rand(1,1000); $qrcode_path=$qrcode_bas_path.$uniqid_rand. "_1.".$tpgs; $qrcode_path_new=$qrcode_bas_path.$uniqid_rand."_2.".$tpgs; if(Helper::getOS()=='Linux'){ $mv = move_uploaded_file($file_tmp_name, $qrcode_path); }else{ //解决windows下中文文件名乱码的问题 $save_path = Helper::safeEncoding($qrcode_path,'GB2312'); if(!$save_path){ $errors[]='上传失败,请重试!'; } $mv = move_uploaded_file($file_tmp_name, $qrcode_path); } if(empty($errors)){ $errorCorrectionLevel = $_POST['errorCorrectionLevel'];//容错级别 $matrixPointSize = $_POST['matrixPointSize'];//生成图片大小 $matrixMarginSize = $_POST['matrixMarginSize'];//边距大小 //生成二维码图片 QRcode::png($content,$qrcode_path_new, $errorCorrectionLevel, $matrixPointSize, $matrixMarginSize); $QR = $qrcode_path_new;//已经生成的原始二维码图 $logo = $qrcode_path;//准备好的logo图片 if (file_exists($logo)) { $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); //输出图片 // header("Content-type: image/png"); imagepng($QR,$qrcode_path); imagedestroy($QR); }else{ $qrcode_path=$qrcode_path_new; } $qrcode_path=str_replace($bas_path,'', $qrcode_path); }else{ $qrcode_path=''; } } $data=array('data'=>array('errors'=>$errors,'qrcode_path'=>$qrcode_path)); $this->render ( 'qrcode',$data); }
前台的上传界面:
<?php $vars = get_defined_vars (); $data = $vars ['data']; $content=Yii::app ()->request->hostInfo; $matrixPointSize=6; $matrixMarginSize=2; $errorCorrectionLevel='M'; $tpgs='gif'; if(!empty($_POST)){ $content=$_POST['content']; $matrixPointSize=$_POST['matrixPointSize']; $matrixMarginSize=$_POST['matrixMarginSize']; $errorCorrectionLevel=$_POST['errorCorrectionLevel']; $tpgs=$_POST['tpgs']; } $arrayCorrectionLevel=array('L'=>'L - Low (7%)','M'=>'M - Medium (15%)','Q'=>'Q - Quartile (25%)','H'=>'H - High (30%)'); $arrayTpgs=array('gif'=>'gif格式','png'=>'png格式','jpg格式'); ?> <div class="col-md-12"> <div class="form-horizontal panel panel-default margin-t-10 b-img"> <div class="panel-heading"> <div class="pull-left"> <span class="g-bg glyphicon glyphicon-wrench margin-r-2" aria-hidden="true"></span>在线生成二维码 </div> <div class="clearfix"></div> </div> <?php $form = $this->beginWidget ( 'CActiveForm', array ( 'id' => 'qrcode-form', 'htmlOptions' => array ( 'id' => 'view_table', 'class' => 'add-form padding-10', 'enctype' => 'multipart/form-data' ), 'enableAjaxValidation' => false ) ); ?> <div class="form-group"> <label class="col-lg-2 control-label">尺寸大小</label> <div class="col-lg-3"> <select class="form-control" id="matrixPointSize" name="matrixPointSize"> <?php for ($i=1;$i<21;$i++):?> <option value="<?php echo $i;?>" <?php echo $i==$matrixPointSize?'selected':'';?>><?php echo $i;?></option> <?php endfor;?> </select> </div> </div> <div class="form-group"> <label class="col-lg-2 control-label">边距大小</label> <div class="col-lg-3"> <select class="form-control" id="matrixMarginSize" name="matrixMarginSize"> <?php for ($i=0;$i<21;$i++):?> <option value="<?php echo $i;?>" <?php echo $i==$matrixMarginSize?'selected':'';?>><?php echo $i;?></option> <?php endfor;?> </select> </div> </div> <div class="form-group"> <label class="col-lg-2 control-label">容错级别</label> <div class="col-lg-3"> <?php echo CHtml::dropDownList('errorCorrectionLevel',$errorCorrectionLevel, $arrayCorrectionLevel,array('class'=>'form-control'));?> </div> </div> <div class="form-group"> <label class="col-lg-2 control-label">保存格式</label> <div class="col-lg-3"> <?php echo CHtml::dropDownList('tpgs',$tpgs, $arrayTpgs,array('class'=>'form-control'));?> </div> </div> <div class="form-group"> <label class="col-lg-2 control-label">二维码内容</label> <div class="col-lg-5"> <?php echo CHtml::textField('content',$content,array('class'=>'form-control','maxlength'=>150));?> </div> </div> <div class="form-group"> <label class="col-lg-2 control-label">二维码logo图片</label> <div class="col-lg-5"> <div class="col-md-6"> <input id="upimage" type="file" name="upimage" class="hidden"> <input id="tmp_file" class="form-control" type="text" value="gif,png,jpg"> </div> <div class="col-md-6"><a class="btn btn-default" onclick="$('input[id=upimage]').click();">选择文件</a></div> </div> </div> <div class="list_back"> <input type="submit" value="生成二维码" class="btn btn-success"> </div> </div> <?php $this->endWidget(); ?> <div class="panel panel-default margin-t-10 b-img"> <div class="panel-heading"> <span class="g-bg glyphicon glyphicon-wrench margin-r-2" aria-hidden="true"></span>二维码 </div> <div class="panel-body"> <?php if(empty($_POST)):?> <?php echo CHtml::image('/static/tool/qrcode/qrcode.gif','二维码');?> <?php endif;?> <?php if(!empty($data['errors'])):?> <label class="col-lg-2 text-right">生成失败</label> <div class="col-lg-5"> <?php foreach ($data['errors'] as $e):?> <?php echo $e;?><br> <?php endforeach;?> </div> <?php endif;?> <?php if(!empty($data['qrcode_path'])):?> <?php echo CHtml::image($data['qrcode_path'],'二维码');?> <a class="btn btn-success color-f" href="<?php echo $data['qrcode_path'];?>" target="_blank"><span aria-hidden="true" class="glyphicon glyphicon-download-alt margin-r-2"></span>右键另存为二维码</a> <?php endif;?> </div> </div> <?php $this->renderPartial('/component/duoshuo_common');?> </div>
如图:
演示地址

如何使用PHP实现动态生成二维码功能二维码(QRCode)被广泛应用于各个领域,它可以存储大量信息且易于扫描。在网页应用中,我们经常需要动态生成二维码,以便为用户提供便捷的操作方式。本文将介绍如何使用PHP实现动态生成二维码的功能。一、安装和配置PHPQRCode库为了方便生成二维码,我们可以使用PHPQRCode库。首先,我们需要

先决条件:在您的iPhone上启用二维码扫描默认情况下,所有运行iOS11的iPhone都启用了扫描QR码的功能。因此,您需要确保您的iPhone已更新到最新的可用版本,至少iOS11才能能够原生扫描QR码。在继续执行以下任何方法之前,您必须确保在iPhone上启用了该功能。您可以通过打开“设置”应用并点击“相机”部分在iPhone上启用QR码扫描。在下一个屏幕上,启用“扫描QR码”切换。这应该会打开该功能,以便您可以使用以下任何方法扫描并从QR码中提取

如何使用PHP生成批量的二维码?随着互联网技术的不断发展,二维码已经成为了一种非常普遍的信息传递工具。二维码可以存储大量的信息,并且可以快速扫描识别,因此在各行各业中得到了广泛的应用。在很多情况下,我们需要批量生成大量的二维码,比如用于商品标签、活动门票等。PHP是一种广泛应用于web开发的脚本语言,具有灵活、简单易用的特点。下面,我们将介绍如何使用PHP生

如何使用PHP生成带有时间限制的二维码?随着移动支付和电子门票的普及,二维码成为了一种常见的技术。在很多场景中,我们可能需要生成一种带有时间限制的二维码,即使在一定时间后,该二维码也将失效。本文将介绍如何使用PHP生成带有时间限制的二维码,并提供代码示例供参考。安装PHPQRCode库要使用PHP生成二维码,我们需要先安装PHPQRCode库。这个库

如何通过PHP编写一个简单的二维码生成器二维码在现代社会中已经变得非常常见,它能够快速传递信息,提升用户体验。在本文中,我将向大家介绍如何使用PHP编写一个简单的二维码生成器。一、安装必要的工具和库在开始之前,我们需要确保已经安装以下工具和库:PHP:确保已经安装了PHP的最新版本,可以通过运行php-v命令来查看当前PHP的版本。Composer:C

使用Slim框架中间件实现二维码生成和扫描的功能简介:在现代社会,二维码已经成为广泛应用的一种信息传递方式。许多应用程序和网站都提供了二维码的生成和扫描功能。本文将介绍如何使用Slim框架的中间件来实现二维码的生成和扫描功能。安装Slim框架:首先,我们需要安装Slim框架。在终端中执行以下命令:composerrequireslim/slim生成二维码

二维码是现代社会中广泛使用的一种信息编码方式,Vue是一款前端框架,如何使用Vue实现二维码生成呢?一、了解二维码生成的原理二维码的生成原理是将一段文本或一段URL地址转换成一张图片,在这张图片中编码了文本或URL地址的信息。二维码生成可以使用第三方库,本文介绍如何使用Qrcode.js库来生成二维码。Qrcode.js是一款轻量级、无依赖的二维码生成库。二

如何使用PHP开发公众号的二维码生成功能当今社交媒体的盛行使得公众号成为企业与用户互动的重要渠道之一。为了吸引更多用户关注公众号,企业常常会使用二维码来方便用户扫码关注。本文将介绍如何使用PHP开发公众号的二维码生成功能,并提供具体的代码示例。获取二维码生成地址在使用PHP开发公众号的二维码生成功能之前,我们首先需要获取二维码生成地址。可以通过微信公众平台提


Heiße KI -Werkzeuge

Undresser.AI Undress
KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover
Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool
Ausziehbilder kostenlos

Clothoff.io
KI-Kleiderentferner

AI Hentai Generator
Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

Heiße Werkzeuge

SublimeText3 Linux neue Version
SublimeText3 Linux neueste Version

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

Herunterladen der Mac-Version des Atom-Editors
Der beliebteste Open-Source-Editor

WebStorm-Mac-Version
Nützliche JavaScript-Entwicklungstools

ZendStudio 13.5.1 Mac
Leistungsstarke integrierte PHP-Entwicklungsumgebung
