» » Use PHP to generate two-dimensional code

 

Use PHP to generate two-dimensional code

Author: bamboo06 on 28-10-2014, 03:42, views: 3019

11
With the advancement of technology, two-dimensional code applications more widely, before the site has article describes using jQuery plugin to generate two-dimensional code, and today I share with you how to use PHP to generate two-dimensional code, and how to generate two-dimensional code in the middle with LOGO image.

Use Google API to generate two-dimensional code
Google offers a more comprehensive two-dimensional code generation interface, call the API interface is very simple, the following is the calling code:
$urlToEncode="http://www.goocode.net"; 
generateQRfromGoogle($urlToEncode); 
/** 
 * Google api [QRcode two-dimensional code generation can store up to 4296 alphanumeric type any text, you can view a specific two-dimensional code data format] 
  *param String $ chl information contained in the two-dimensional code, can be numbers, characters, binary information, character. 
  You can not mix data types, data must be UTF-8 URL-encoded 
  *param Int $ widhtHeight generate two-dimensional code size settings 
  *param String $ EC_level optional error correction level, QR code support four levels of error correction is used to recover lost, misread, fuzzy data. 
  * L- default: You can identify the loss of 7% of the data 
  * M- can identify the 15% loss of data 
  * Q- can identify the loss of 25% of the data 
  * H- can identify the loss of 30% of the data 
  *param Int $ margin generated two-dimensional code from the picture frame distance
 */ 
function generateQRfromGoogle($chl,$widhtHeight ='150',$EC_level='L',$margin='0') 
{ 
    $chl = urlencode($chl); 
    echo 'http://chart.apis.google.com/chart?chs='.$widhtHeight.'x'.$widhtHeight.' 
    &cht=qr&chld='.$EC_level.'|'.$margin.'&chl='.$chl.'" alt="QR code" widhtHeight="'.$widhtHeight.' 
    " widhtHeight="'.$widhtHeight.'"/>'; 
} 

Two-dimensional code generation using PHP library to generate two-dimensional code PHP QR Code
PHP QR Code is a two-dimensional code generated PHP class library, using it can easily generate a two-dimensional code, the official website offers downloads and multiple presentations demo, see the address: http: //phpqrcode.sourceforge.net/.
After downloading the official website provides libraries, just use phpqrcode.php can generate two-dimensional code, of course, open your PHP environment must support GD2. phpqrcode.php provides a critical png () method, where the parameter $ text representation of the information generated two text; $ outfile parameter indicates whether the output of two-dimensional code image file, the default No; parameter $ level, said fault rate, which is there are also identifies areas are covered, namely L (QR_ECLEVEL_L, 7%), M (QR_ECLEVEL_M, 15%), Q (QR_ECLEVEL_Q, 25%), H (QR_ECLEVEL_H, 30%); parameter $ size represents generated image size The default is 3; Parameter $ margin represents the border around a blank area of ​​the two-dimensional code spacing value; parameter $ saveandprint indicates whether to save the two-dimensional code and displayed.
public static function png($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); 
} 

Call PHP QR Code is very simple, the following code to generate a content "http://www.goocode.net" two-dimensional code.
include 'phpqrcode.php'; 
QRcode::png('http://www.goocode.net'); 

Then the actual application, we will add your own LOGO in the middle of a two-dimensional code has been enhanced publicity. How to generate a two-dimensional code that contains a logo it? In fact, the principle is very simple, first using PHP QR Code generating a two-dimensional code images, and then use php's image correlation function, will be prepared logo image is added to the original two-dimensional code just generated images in the middle, and then rebuild a new the two-dimensional code images.
include 'phpqrcode.php';  
$value = 'http://www.goocode.net'; //content
$errorCorrectionLevel = 'L';//Fault-tolerance level
$matrixPointSize = 6;//image size 
//Generate two-dimensional code images 
QRcode::png($value, 'qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2); 
$logo = 'logo.png';//Ready logo image
$QR = 'qrcode.png';//The original two-dimensional code has been generated in FIG
  
if ($logo !== FALSE) { 
    $QR = imagecreatefromstring(file_get_contents($QR)); 
    $logo = imagecreatefromstring(file_get_contents($logo)); 
    $QR_width = imagesx($QR);//width
    $QR_height = imagesy($QR);//height
    $logo_width = imagesx($logo);//logo width 
    $logo_height = imagesy($logo);//logo 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; 
    //Reassemble the picture and resize
    imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,  
    $logo_qr_height, $logo_width, $logo_height); 
} 
//export image
imagepng($QR, 'goocode.png'); 
echo '<img src="goocode.png">'; 

Since the two-dimensional code to allow a certain fault tolerance, the general two-dimensional code even if the cover part, but still be able to decode, we scanned the regular two-dimensional code can be decoded even when the scan time is less than half of the scan, this is because the Builder will be part of the information is repeated representation to improve its fault tolerance, which is why we add LOGO picture in the middle of a two-dimensional code does not affect the result of decoding reasons.

Category: PHP Scripts / Javascript

Dear visitor, you are browsing our website as Guest.
We strongly recommend you to register and login to view hidden contents.
<
  • 0 Comments
  • 0 Articles
19 October 2017 22:51

Darnisha

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
Okay so from what i have gathered didnt you post about this on http://www.allaboutessay.co.uk/essay-writing-template/ i think you did and forgot and are reposting it here again!

<
  • 0 Comments
  • 0 Articles
11 November 2017 23:19

ali

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
In this article understand the most important thing, the item will give you a keyword rich link a great useful website page: He is Falling In Love

<
  • 0 Comments
  • 0 Articles
16 November 2017 09:32

ali

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
wow this saintly however ,I love your enter plus nice pics might be part personss negative love being defrent mind total poeple , Essential Oils Inflammation

<
  • 0 Comments
  • 0 Articles
17 November 2017 07:29

ali

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
It is rather very good, nevertheless glance at the data with this handle. Health Benefits of Kale

<
  • 0 Comments
  • 0 Articles
1 December 2017 16:35

Yunisha

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
Thank you for sharing, I so many know. This article is very useful! <a href="https://goo.gl/E9n4gp"> Walatra Jelly Gamat</a>

<
  • 0 Comments
  • 0 Articles
19 December 2017 20:39

carl

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
PHP is something that I really need to learn. There is a lot of work with the drone with camera that I need to do and that requires me to learn PHP and I will do that as soon as I can. Thanks for telling me that here.

<
  • 0 Comments
  • 0 Articles
29 December 2017 02:45

david

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
Marvelous web journal. I appreciated perusing your articles. This is really an incredible read for me. I have bookmarked it and I am anticipating perusing new articles. Keep doing awesome! public relations

<
  • 0 Comments
  • 0 Articles
8 January 2018 17:36

ALISE

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
Just pure brilliance from you here. I have never expected something less than this from you and you have not disappointed me at all. I suppose you will keep the quality work going on.
forskolin studies

<
  • 0 Comments
  • 0 Articles
9 January 2018 23:40

sarah

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
Amazing article! I need individuals to know exactly how great this data is in your article. It's fascinating, convincing substance. Your perspectives are much like my own particular concerning this subject. south florida pr agency

<
  • 0 Comments
  • 0 Articles
10 January 2018 22:10

ALISE

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
Awesome article with great idea!Thank you for such a profitable article. I truly acknowledge for this extraordinary data. pr outreach

<
  • 0 Comments
  • 0 Articles
18 January 2018 16:03

ALISE

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
I can feel that you paid much attention for those articles, as all of them make sense and are very useful. tech pr firms

Information
Comment on the news site is possible only within (days) days from the date of publication.