PHP generation with logo Two-dimensional code method summary _php Skill

Source: Internet
Author: User
Tags base64 readfile

One, the use of the class library

1, Phpqrcode (PHP library)

2, Qrcode.js (JavaScript library)

Second, the use of Phpqrcode

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

Phpqrcode Use Method:

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

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

$value = $url///Two-dimensional code content $errorCorrectionLevel = ' L '///Set fault tolerance level $matrixPointSize = $size//Generate picture size QRCode::p ng ($value, ' publ Ic_files '. Directory_separator. ' Code '. Directory_separator. ' Qrcode.png ', $errorCorrectionLevel, $matrixPointSize, 2)//Generate two-dimensional code picture without logo $logo = $tar _path;//uploaded to the server logo picture $QR = ' public _files '. Directory_separator. ' Code '. Directory_separator. '
  Qrcode.png '//already generated original two-dimensional code figure if ($logo!== FALSE) {$QR = Imagecreatefromstring (file_get_contents ($QR));
  $logo = imagecreatefromstring (file_get_contents ($logo)); $QR _width = Imagesx ($QR);//Two-dimensional code picture width $QR _height = Imagesy ($QR);//two-dimensional code picture height $logo _width = imagesx ($logo);//logo Picture width $logo
  _height = 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 _hei
Ght);
$name = time (); ImAgepng ($QR, ' public_files '. Directory_separator. ' Code '. Directory_separator. $name.

 PNG ');//output two-dimensional code picture with logo

Third, the use of qrcode.js

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

The use of QRCode is also simple:

var length = size*80;//Sets the two-dimensional code size
length = parseint (length);
$ ("#code_img"). QRCode ({//code_img is an IMG tag ID
  render: "Canvas",  //Set rendering mode, there are table and canvas, Using canvas mode rendering performance is relatively good
  text:url,//  scan two-dimensional code after the contents of the display, you can directly fill out a URL, scanning two-dimensional code automatically jumps to the link
  width:length,       //two-D code width
  height:length,
  background: "#ffffff",    ///two-D
  background foreground: "#000000",///    two-D code foreground color
  SRC: $ (' #image '). attr (' src ')       ///Two D code in the middle of the picture
});

After the introduction of Jquery.qrcode.js to write this JS code, after the implementation of two-dimensional code can be displayed processing

Mainly two-dimensional code in the middle of the logo reference format, generally take local pictures have two formats: one is the local URL, the other is the picture into base64 format

At first I tried to use the local URL format to refer to the picture, found that can only refer to the same JS file under the Unified directory of pictures, so the local URL format does not support, so I adopted the latter way.

Use

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

To upload and select the local picture, 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 ("File must be a picture!") ");
    return false;
  }
  var reader = new FileReader ();
  Reader.readasdataurl (file);
  Reader.onload = function (e) {
    $ (' #image '). attr (' src ', this.result);//image ID for img tag
  }
}

Reads the file as a string of data URL strings and reads the small file directly into the page in a specially formatted URL address. This special format is base64.

Comparison between the three and two class libraries

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

Phpqrcode generates two-dimensional code in the background operation, the resulting picture is saved on the server. The general generation of two-dimensional code is directly saved to the local and then directly used, rarely go to the server two times to obtain, so the use of Phpqrcode words will make the image of the server accumulation, occupy the unnecessary space, delete words will also spend extra overhead. Therefore, using Phpqrcode does not apply to this two-dimensional code generation tool. And uploading pictures can create extra overhead.

Qrcode.js directly in front of the operation, upload pictures directly saved in the browser, directly in front of the generation of two-dimensional code, do not require any interference in the background, so as to reduce the unnecessary overhead, will not be in the server caused by the accumulation of pictures 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.