您好,欢迎访问一九零五行业门户网

yii+phpqrcode生成二维码实例

phpqrcode是一个php生成二维码的类库,我们可以利用他轻松生成二维码,本文我们来看看用yii如何整合phpqrcode生成二维码的。
以前我们讲过一些关于用phpqrcode生成二维码的文章,下面我们先列出来
php利用php qr code生成二维码(带logo)
php生成二维码(使用php qr code二维码生成类库)
利用phpqrcode生成二维码实例代码
超简单php生成二维码实例
下面我们来讲讲yii整合phpqrcode生成二维码的实例。
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); }
前台的上传界面:
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格式');?> 在线生成二维码
beginwidget ( 'cactiveform', array ( 'id' => 'qrcode-form', 'htmloptions' => array ( 'id' => 'view_table', 'class' => 'add-form padding-10', 'enctype' => 'multipart/form-data' ), 'enableajaxvalidation' => false) );?> 尺寸大小 边距大小
保存格式 'form-control'));?>
二维码内容 'form-control','maxlength'=>150));?>
二维码logo图片
选择文件
endwidget(); ?> 二维码
生成失败
右键另存为二维码
renderpartial('/component/duoshuo_common');?>

如图:
其它类似信息

推荐信息