HP QR code generates two-dimensional code

Source: Internet
Author: User

Recently need to do a scan login function, through the HP QR code to achieve, HP QR code is an open source PHP generated QR code class library

Address: http://phpqrcode.sourceforge.net/

The phpqrcode.php PNG () method allows you to generate a QR code image, and the PNG () method parameter description:

 Public Static functionpng$text,$outfile=false,$level= qr_eclevel_l,$size= 3,$margin= 4,$saveandprint=false)         {            $enc= Qrencode::factory ($level,$size,$margin); return $enc->encodepng ($text,$outfile,$saveandprint=false); }

The first parameter $text,

The second parameter $outfile default to No, do not generate files, only the two-dimensional code picture returned, otherwise you need to store the image of the generation of two-dimensional code path

The third parameter, $level, defaults to L, which can pass values of L (qr_eclevel_l,7%), M (qr_eclevel_m,15%), Q (qr_eclevel_q,25%), H (qr_eclevel_h,30%), respectively. This parameter controls the fault tolerance of the two-dimensional code, and different parameters indicate the percentage of the area that the QR code can be overwritten.

With the fault tolerance of the QR code, we can place the avatar in any area of the generated QR code image.

The fourth parameter, $size, controls the size of the resulting picture, which defaults to 4.

The fifth parameter, $margin, controls the size of the white space to generate the QR code.

The sixth parameter $saveandprint, save the two-dimensional code picture and display, $outfile must pass the picture path.

Official document instance (generate picture):

    Echo"; //set it to writable location, a place for temp generated PNG files    $PNG _temp_dir=dirname(__file__). Directory_separator. ' Temp '.Directory_separator; //HTML PNG location prefix    $PNG _web_dir= ' temp/'; include"Qrlib.php"; //Ofcourse We need rights to create temp dir    if(!file_exists($PNG _temp_dir))        mkdir($PNG _temp_dir); $filename=$PNG _temp_dir.‘ Test.png '; //processing Form input//remember to sanitize user input in Real-life solution!!!    $errorCorrectionLevel= ' L '; if(isset($_request[' Level ']) &&In_array($_request[' Level '],Array(' L ', ' M ', ' Q ', ' H '))))        $errorCorrectionLevel=$_request[' Level ']; $matrixPointSize= 4; if(isset($_request[' Size ']))        $matrixPointSize=min(Max((int)$_request[' Size '], 1), 10); if(isset($_request[' Data '])) {             //it ' s very important!        if(Trim($_request[' Data ']) = = "')             die(' Data cannot be empty! <a href= '? ') >back</a> '); //User Data        $filename=$PNG _temp_dir.‘ Test '.MD5($_request[' Data ']. ' | '.$errorCorrectionLevel. ' | '.$matrixPointSize).‘. png; QRCode::p ng ($_request[' Data '],$filename,$errorCorrectionLevel,$matrixPointSize, 2); } Else {                //Default Data        Echo' can provide data in GET parameter: <a href= '? data=like_that ">like that</a>; QRCode::p ng (' PHP QR Code:) ',$filename,$errorCorrectionLevel,$matrixPointSize, 2); }

Project instance (do not generate pictures directly in the browser output):

test.php

include"Qrlib.php"; if(Empty($PNG _temp_dir)) {    //set it to writable location, a place for temp generated PNG files    $PNG _temp_dir=dirname(__dir__). Directory_separator. /web/upload '.Directory_separator;}if(Empty($PNG _web_dir)) {    //HTML PNG location prefix    $PNG _web_dir= ' web/upload/';}//Ofcourse We need rights to create temp dirif(!file_exists($PNG _temp_dir))    mkdir($PNG _temp_dir);$filename=$PNG _temp_dir. Time().‘. png;//processing form Input//remember to sanitize user input in Real-life solution!!!if(In_array($errorCorrectionLevel,Array(' L ', ' M ', ' Q ', ' H ')))) {    $errorCorrectionLevel=$errorCorrectionLevel;} Else {    $errorCorrectionLevel= "L";}if($matrixPointSize)    $matrixPointSize=min(Max((int)$matrixPointSize, 1), 10);if(!Empty($data)) {    //it ' s very important!    if(Trim($data) = = "')         die(' Data cannot be empty! <a href= '? ') >back</a> '); //User Data    $filename=$PNG _temp_dir. Time().MD5($data. ' | '.$errorCorrectionLevel. ' | '.$matrixPointSize).‘. png; QRCode::p ng ($data,false,$errorCorrectionLevel,$matrixPointSize, 2);} Else {    //default data//echo ' can provide data in GET parameter: <a href= "? Data=like_that" >like that</a><    Hr/> '; QRCode::p ng (' PHP QR Code:) ',false,$errorCorrectionLevel,$matrixPointSize, 2);}

Create a new page for direct output QR code

    /** * Generate QR code directly on the browser*/     Public functionactionpng () {$data= \yii::$app->request->get (' Data ', '); $data=Base64_decode($data); $PNG _temp_dir= ' '; $PNG _web_dir= ' '; $errorCorrectionLevel= "M"; $matrixPointSize= 3; include‘.. /components/phpqrcode/test.php '; }

Reference:

<src= "<?=/tv-wall/png?data=". Base64_encode ($data)?> "></  iframe>

Note: If directly on the page output, will output the image of the binary code, through the browser resolution to display the picture, the page can not output anything else, or the browser will not be able to parse the output binary code directly, so here on a separate page display QR code image, and then through the IFRAME reference

HP QR code generates two-dimensional code

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.