Yii integration Phpqrcode generate two-dimensional code with online demo address

Source: Internet
Author: User
Tags php foreach vars yii

1, first to the official website download package http://phpqrcode.sourceforge.net/

After downloading the class library provided by the official website, we only need to use phpqrcode.php to generate the QR code, of course, your PHP environment must turn on support GD2.

Phpqrcode.php provides a key PNG () method, where
The parameter $text represents the generated two-bit information text;
The parameter $outfile indicates whether the output QR code picture file, default no;
The parameter $level represents the fault tolerance, i.e., the area covered is also recognized, respectively L (qr_eclevel_l,7%), M (qr_eclevel_m,15%), Q (qr_eclevel_q,25%), H (qr_eclevel_h,30%) ;
The parameter $size indicates the generated picture size, the default is 3, and the parameter $margin indicates the spacing value of the border space around the two-dimensional code;
The parameter $saveandprint indicates whether to save the QR code and display it.

2, after downloading the extracted Phpqrcode folder into the Extensions folder, such as:

3, introduce class Phpqrcode

Yii:: $enableIncludePath = false; Yii::import (' Application.extensions.phpqrcode.phpqrcode ', 1);

Here is the complete method of generating the QR code

Public Function Actionqrcode () {$this->breadcrumbs=array_merge ($this->breadcrumbs,array (' Generate two-dimensional code ')); $qrcode _ Path= '; $file _tmp_name= '; $errors =array (); if (!empty ($_post)) {$content = Trim ($_post[' content ']);//Two-D code contents $ Contentsize= $this->getstringlength ($content), if ($contentSize >290) {$errors []= ' word is too long, no more than 150 characters! ‘;} 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 []=] The file you uploaded is too large to exceed 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 []=] The file you uploaded is not in the correct format and only supports PNG, JPG, GIF format. ";}} $tpgs =$_post[' tpgs '];//picture format $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{//solve the problem of garbled Chinese file names under Windows $save_path = helper::safeencoding ($qrcode _path, ' GB2312 '); {$errors [_path ') Upload failed, please try again! ‘;} $MV = Move_uploaded_file ($file _tmp_name, $qrcode _path);} if (empty ($errors)) {$errorCorrectionLevel = $_post[' errorcorrectionlevel '];//fault tolerance level $matrixpointsize = $_post[' Matrixpointsize '];//generate picture size $matrixmarginsize = $_post[' matrixmarginsize '];//margin size//generate QR code picture qrcode::p ng ($content, $ Qrcode_path_new, $errorCorrectionLevel, $matrixPointSize, $matrixMarginSize) $QR = $qrcode _path_new;//has generated the original two-dimensional code graph $ Logo = $qrcode _path;//ready logo image if (file_exists ($logo)) {$QR = Imagecreatefromstring (file_get_contents ($QR)); $logo = Imagecreatefromstring (file_get_contents ($logo)); $QR _width = Imagesx ($QR);//two-dimensionalCode image Width $qr_height = imagesy ($QR);//QR code image Height $logo_width = imagesx ($logo);//logo picture Width $logo_height = Imagesy ($logo);// Logo image Height $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;//regroup picture and resize imagecopyresampled ($QR, $logo, $from _width, $from _width, 0, 0, $log O_qr_width, $logo _qr_height, $logo _width, $logo _height);//Output picture//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);}

Upload interface of the foreground:

<?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-h IgH (30%) '); $arrayTpgs =array (' gif ' + ' gif format ', ' png ' = ' png ' format ', ' jpg format ');? ><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> online generation of QR code </div><div class= "Clearfix" &GT;&LT;/DIV&GT;&LT;/DIV&GT;&LT;? 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" > Size </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 "> Margin size </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 "> Fault tolerance Level </label><div class=" col-lg-3 "><?php echo CHtml::d ropdownlist (' Errorcorrectionlevel ') , $errorCorrectionLevel, $arrayCorrectionLevel, Array (' class ' = ' Form-control '));? ></div></div><div class= "Form-group" ><label class= "Col-lg-2 control-label" > Save format </ Label><div class= "col-lg-3" ><?php echo CHtml::d ropdownlist (' Tpgs ', $tpgs, $arrayTpgs, Array (' class ' = ' Form-control '); ></div></div><div class= "Form-group" ><label class= "col-lg-2 control-label" > QR code content </ 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 "> QR code logo image </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 (); > select File </a></div></div></div><div class= "List_back" ><input type= "Submit" value= "Generate two-dimensional code" 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> qr code </div><div class=" Panel-body ">< ? php if (empty ($_post)):? ><?php echo chtml::image ('/static/tool/qrcode/qrcode.gif ', ' QR code ');? ><?php endif;? ><?php if (!empty ($data [' Errors ')):? ><label class= "Col-lg-2 text-rIght "> Build failed </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 '], ' QR code ');? ><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> Right-click Save as QR code </a><?php endif;? ></div></div><?php $this->renderpartial ('/component/duoshuo_common ');? ></div>

Demo Address

Yii integration Phpqrcode generate two-dimensional code with online demo address

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.