Yii integrates phpqrcode to generate a QR code with the online demo address. yiiphpqrcode_PHP tutorial

Source: Internet
Author: User
Tags php foreach
Yii integrates phpqrcode to generate a QR code with the online demo address, yiiphpqrcode. Yii integrates phpqrcode to generate a QR code with the online demo address, yiiphpqrcode1. after downloading phpqrcode.sourceforge.net on the official website, you only need to integrate phpqrcode to generate a QR code with the online demo address

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, '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 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');?>

Demo address

Http://www.bkjia.com/PHPjc/1070041.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1070041.htmlTechArticleyii integration phpqrcode generated QR code with online demo address, yiiphpqrcode 1, first to the official website to download the package http://phpqrcode.sourceforge.net/download the library provided by the official website, only need to use p...

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.