Phpqrcode is a class library for generating two-dimensional codes in php. We can use it to easily generate two-dimensional codes. In this article, we will look at how to integrate phpqrcode to generate two-dimensional Codes Using yii. Phpqrcode is a class library for generating two-dimensional codes in php. We can use it to easily generate two-dimensional codes. In this article, we will look at how to integrate phpqrcode to generate two-dimensional Codes Using yii.
Script ec (2); script
We have mentioned some articles about using phpqrcode to generate a QR code. Below we will first list
Php ExploitationPhp qr CodeGenerate QR code (with logo)
PHPGenerate a QR code (UsePhp qr CodeQR code generation Class Library)
ExploitationPhpqrcodeGenerate QR code
Ultra-simplePHPGenerate a QR code instance
Let's talk about how yii integrates phpqrcode to generate a QR code instance.
1, first go to the official website to download package http://phpqrcode.sourceforge.net/
After downloading the class library provided on the official website, you only need to use phpqrcode. php to generate a QR code. Of course, you must enable GD2 in your PHP environment.
Phpqrcode. php provides a key png () method.
The $ text parameter indicates generating two pieces of information text;
The parameter $ outfile indicates whether to output the QR code image file. The default value is no;
The $ level parameter indicates the coverage rate, that is, the covered areas can also be identified, 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 the size of the generated image. The default value is 3. The parameter $ margin indicates the gap between blank areas in the border around the QR code;
The parameter $ saveandprint indicates whether to save the QR code and display it.
2. Put the decompressed phpqrcode folder under the extensions folder, for example:
3. Introduce phpqrcode
Yii: $ enableIncludePath = false;
Yii: import ('application. extensions. phpqrcode. phpqrcode', 1 );
The following describes how to generate a complete QR code.
Public function actionQrcode () {$ this-> breadcrumbs = array_merge ($ this-> breadcrumbs, array ('generate QR code '); $ qrcode_path = ''; $ file_tmp_name = ''; $ errors = array (); if (! Empty ($ _ POST) {$ content = trim ($ _ POST ['content']); // QR code content $ contentSize = $ this-> getStringLength ($ content ); if ($ contentSize> 290) {$ errors [] = 'the number of words is too long and cannot exceed 150 characters! ';} Yii ::$ enableIncludePath = false; Yii: import ('application. extensions. phpqrcode. phpqrcode ', 1); if (isset ($ _ FILES ['upimage'] ['tmp _ name']) & $ _ FILES ['upload'] ['tmp _ name'] & is_uploaded_file ($ _ FILES ['upload'] ['tmp _ name']) {if ($ _ FILES ['upimage'] ['SIZE']> 512000) {$ errors [] = "your uploaded file is too large to exceed 500 kb. ";}$ File_tmp_name = $ _ FILES ['upimag'] ['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 incorrectly formatted and only supports png, jpg and gif formats. ";}}$ Tpgs =$ _ POST ['tpg']; // image 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 in windows $ save_path = Helper: safeEncoding ($ qrcode_path, 'gb 2312 '); 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 the image size $ matrixMarginSize =$ _ POST ['matrixmarginsize']; // Margin Size // generate a QR code image QRcode: png ($ content, $ qrcode_path_new, $ errorCorrectionLevel, $ matrixPointSize, $ matrixMarginSize); $ QR = $ qrcode_path_new; // generated original QR code diagram $ logo = $ qrcode_path; // prepared logo image if (file_exists ($ logo )) {$ QR = imagecreatefromstring (file_get_contents ($ QR); $ logo = imagecreatefromstring (file_get_contents ($ logo); $ QR_width = imagesx ($ QR ); // QR code Image Width $ QR_height = imagesy ($ QR); // QR code Image Height $ logo_width = imagesx ($ logo ); // logo image 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; // re-combine the image and resize imagecopyresampled ($ QR, $ logo, $ from_width, $ from_width, 0, 0, $ logo_qr_width, $ logo_qr_height, $ logo_width, $ logo_height); // output image // 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 );}
Front-end upload interface:
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 ['tpg'];} $ 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');?>
Generate QR code online
BeginWidget ('cactiveform', array ('id' => 'qrcode-form', 'htmloptions '=> array ('id' => 'view _ table ', 'class' => 'add-form padding-10', 'enablesype '=> 'multipart/form-data'), 'enableajaxvalidation' => false);?> Size
>
Margin Size
>
Fault tolerance level
'Form-control');?>
Save format
'Form-control');?>
QR code content
'Form-control', 'maxlength' => 150);?>
QR code logo image
Select File
EndWidget ();?>
QR code
Generation failed
"Target =" _ blank "> right-click to save as a QR code
RenderPartial ('/component/duoshuo_common');?>