Php generate a QR code with logo

Source: Internet
Author: User
This article compares two common methods to generate a QR code with a logo. it is very practical. if you need it, you can select it based on your needs. I. class library used

1. phpqrcode (php Library)

2. qrcode. js (javascript Library)

II. Use of phpqrcode

Only use php class libraries, that is, generating QR codes in the background. To generate a QR code with a logo in the background, you must first upload the logo image to the server (webuploader is used here)

Phpqrcode usage:

Download phpqrcode: http://phpqrcode.sourceforge.net/

Remember to introduce the phpqrcode. php file before using it.

$ Value = $ url; // The content of the QR code $ errorCorrectionLevel = 'l'; // Set the fault tolerance level $ matrixPointSize = $ size; // generate the image size QRcode :: png ($ value, 'public _ files '. DIRECTORY_SEPARATOR. 'code '. DIRECTORY_SEPARATOR .'qrcode.png ', $ errorCorrectionLevel, $ matrixPointSize, 2); // Generate a QR code image without a logo $ logo = $ tar_path; // the logo image uploaded to the server $ QR = 'public _ files '. DIRECTORY_SEPARATOR. 'code '. DIRECTORY_SEPARATOR .'qrcode.png '; // if ($ logo! = FALSE) {$ 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; imagecopyresampled ($ QR, $ logo, $ from_width, $ from_width, 0, 0, $ logo_qr_width, $ logo_qr_height, $ logo_width, $ logo_height);} $ name = time (); imagepng ($ QR, 'public _ files '. DIRECTORY_SEPARATOR. 'code '. DIRECTORY_SEPARATOR .w.name.'.png '); // outputs a QR code image with a logo.

III. Use of qrcode. js

Use qrcode. js to generate a QR code directly at the front end. first download jquery. qrcode. js

Qrcode is also easy to use:

Var length = size * 80; // set the size of the QR code. length = parseInt (length); $ ("# code_img "). qrcode ({// code_img is the id render of an img label: "canvas", // sets the rendering mode. there are tables and canvas. the rendering performance using canvas is relatively good. text: url, // the content displayed after scanning the QR code, you can directly fill in a url, scan the QR code and automatically jump to the link width: length, // The width of the QR code height: length, background: "# ffffff", // The foreground color of the QR code foreground: "#000000", // The foreground color of the QR code src: $ ('# image '). attr ('src') // picture in the middle of the QR code });

After jquery. qrcode. js is introduced, write the js code. after the code is executed, the QR code can be displayed and processed.

It is mainly the logo reference format in the middle of the QR code. Generally, there are two formats for obtaining a local Image: one is a local URL, and the other is converting the image to a base64 format.

At first, I tried the format of the local URL to reference the image. I found that the image can only be referenced in the same directory as the js file, so the local URL format is not supported, so I used the latter method.

Use

To upload and select a local image, and then use its base64 format

Var input = document. getElementById ("file_input"); if (typeof FileReader === 'undefined') {input. setAttribute ('disabled ', 'disabled');} else {input. addEventListener ('change', readFile, false);} function readFile () {var file = this. files [0]; if (! /Image \/\ w +/. test (file. type) {alert ("the file must be an image! "); Return false;} var reader = new FileReader (); reader. readAsDataURL (file); reader. onload = function (e) {$ ('# image '). attr ('src', this. result); // image is the id of the img tag }}

Reads a file as a string of Data URLs and directly reads a small file into the page at a URL address in a special format. The special format is base64.

III. comparison between two class libraries

Two class libraries, one in the background and the other in the front-end.

Phpqrcode generates a QR code in the background, and the generated image is saved on the server. Generally, the generated QR code is saved locally and used directly. it is rarely obtained from the server twice. Therefore, using phpqrcode will pile up images on the server, occupying unnecessary space, the deletion will also incur additional costs. Therefore, phpqrcode is not applicable to such QR code generation tools. In addition, uploading images can produce excessive overhead.

Qrcode. js directly operates on the front end, uploads images and stores them in the browser, and generates QR codes directly on the front end without any background interference. this reduces unnecessary overhead, it will not cause Image accumulation on the server and occupy unnecessary space.

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.