Yii+phpqrcode generation of two-dimension code instances

Source: Internet
Author: User
Tags rand vars yii

We've talked about the use of Phpqrcode to generate two-dimensional code, and we're going to list the following

PHP uses php QR code to generate a two-dimensional code (with logo)

PHP generates a two-dimensional code (uses PHP QR Code Two-dimensional code to generate class library)

Using Phpqrcode to generate two-dimensional code instance code

Super simple PHP to generate two-dimension code instances


Let's talk about the example of a two-dimensional code generated by the Yii consolidation Phpqrcode.


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

Download the class library provided by the official website, only need to use phpqrcode.php can generate two-dimensional code, of course, your PHP environment must open support GD2.

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

2, after downloading the unpacked Phpqrcode folder into the Extensions folder, the following figure:

3, introducing class Phpqrcode

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

is the complete method for generating two-dimensional 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-dimensional code contents    
          $contentSize = $this->getstringlength ($content);             if ($contentSize >290) {                  $errors []= ' word is too long, not 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 []=] You upload the file is too large, the maximum can not 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 the Chinese file name garbled under Windows                   $save _path = helper::
Safeencoding ($qrcode _path, ' GB2312 ');                 if (! $save _path ) {                      $errors []= ' 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                 //generation of two-dimensional code picture                 qrcode::p ng ($ Content, $qrcode _path_new,  $errorCorrectionLevel,  $matrixPointSize,  $matrixMarginSize);                  $QR  =   $qrcode The original two-dimensional code map of _path_new;//has been generated                   $logo  =  $qrcode _path;//ready logo photos                  if  (File_exists ($logo))  {                      $QR
 = imagecreatefromstring (file_get_contents ($QR));                   
   $logo  = imagecreatefromstring (file_get_contents ($logo));                       $QR _width = imagesx ($QR);//Two-dimensional code mapSlice width                       $QR _height = imagesy ($QR);//two-dimensional code picture height                       $logo _width = imagesx ( $logo);//logo Picture width                       $logo _height = imagesy ($logo);//logo Picture 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;                      //Picture and resize                      imagecopyresampled ($QR,  $logo,  $from _width,  $from _width,  0, 0,  $logo _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= ';             }         &NBSP}          $data =array (' Data ' =>array (' Errors ' => $errors
, ' Qrcode_path ' => $qrcode _path));
         $this->render  (  ' QRCode ', $data);     }


Foreground Upload interface:

<?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 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> generate two-dimensional code    online           </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" > 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;? >                  &nbsP;  </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 '));? >         &NBsp;   </div>         </div>          <div class= "Form-group" >       
      <label class= "Col-lg-2 control-label" > Two-dimensional 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"> Two-dimensional code logo picture </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 files </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> two-dimensional code         </div>          <div class= "Panel-body" >          <?php if (Empty ($_post)):?>         <? Php echo chtml::image ('/static/tool/qrcode/qrcode.gif ', ' two-dimensional code ');? >         <?php endif;? >         <?php if (!empty ($data [' Errors ']):?>              <label class= "col-lg-2  Text-right "> Build failure </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 '], ' two dimensional 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 key Save as two-dimensional code </a>          <?php endif;? >
        </div>     </div> <?php  $this->renderpartial ('/component/duoshuo_common ');? > </div>


As shown in figure:

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.