(Advanced article) PHP generation with logo Two-dimensional code method summary

Source: Internet
Author: User
Tags php class
First, the class library used

1, Phpqrcode (PHP library)

2. Qrcode.js (JavaScript Library)

Second, the use of Phpqrcode

Only PHP class library, that is, the generation of two-dimensional code in the background operation. Because in the background to generate a logo with the QR code, then you must first upload the logo image to the server (upload here I used Webuploader)

Phpqrcode How to use:

First download phpqrcode:http://phpqrcode.sourceforge.net/

Then remember to introduce the phpqrcode.php file before using it

$value = $url;//QR code content $errorCorrectionLevel = ' L ';//Set fault tolerance level $matrixPointSize = $size;//Generate picture size QRCode::p ng ($value, ' public _files '. Directory_separator. ' Code '. Directory_separator. ' Qrcode.png ', $errorCorrectionLevel, $matrixPointSize, 2);//Generate two-dimensional code picture No Logo$logo = $tar _path;//upload to the server logo image $QR = ' public _files '. Directory_separator. ' Code '. Directory_separator. '  Qrcode.png ';//The original two-dimensional code graph that has been generated 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 Picture width $logo _he  ight = Imagesy ($logo);//logo picture 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 _heig HT);} $name = time (); Imagepng ($QR, ' Public_fiLes '. Directory_separator. ' Code '. Directory_separator. $name. PNG ');//output two-dimensional code image with logo

Third, the use of qrcode.js

Using Qrcode.js to generate two-dimensional code directly on the front-end, first download jquery.qrcode.js

QRCode is also very simple to use:

var length = size*80;//Set the two-dimensional code size length = parseint (length); $ ("#code_img"). QRCode ({//code_img is an IMG tag ID  render: " Canvas ",  //Set rendering mode, with table and canvas, using canvas rendering performance is relatively good  text:url,  //scan the QR code after the display of content, you can directly fill in a URL, Scan the QR code and automatically jump to the link  width:length,       //Two-dimensional code width  height:length,  background: "#ffffff",    //two-dimensional code of the rear color  Foreground: "#000000",    //The foreground color of the two-D code  SRC: $ (' #image '). attr (' src ')       //two-D image in the middle of the Code});

The introduction of Jquery.qrcode.js and then write their own JS code, after the implementation of the two-dimensional code can be displayed processing

Mainly two-dimensional code in the middle of the logo reference format, generally take the local image there are both formats: One is a local URL, the other is to convert the picture to Base64 format

At first, I tried the format of the local URL to refer to the picture, found that only reference the image with the JS file in the Unified directory, so the local URL format is not supported, so I adopted the latter way.

Use

<input accept= "image/*" type= "file" id= "File_input" >

To upload and select a local image, and then take 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 a picture!") ");    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 reads the small file directly into the page in a specially formatted URL address. This particular format is base64.

Comparison of three, two class libraries

Two class libraries, one in the background operation, one directly in the front-end operation.

Phpqrcode generates two-dimensional code in the background operation, the resulting picture is saved on the server. Generally generated two-dimensional code is directly saved to the local and then directly use, rarely go to the server two times, so the use of Phpqrcode will make the server image accumulation, occupy unnecessary space, delete the words will also cost extra. So using Phpqrcode is not suitable for this QR code generation tool. And uploading pictures also creates extra overhead.

Qrcode.js directly in the front-end operation, upload images directly saved in the browser, directly in the front-end generation of QR code, do not need any background interference, so as to reduce unnecessary overhead, and will not cause the server to accumulate images and occupy unnecessary space.

Above is (Advanced article) PHP generation with logo QR Code method Summary of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.