ThinkPHP3.2.3 QR code for expansion-php Tutorial

Source: Internet
Author: User
The QR code of ThinkPHP3.2.3 extension is first installed in the path. Simple use without logo: publicfunctionqrcode () {Vendor (phpqrcode. phpqrcode); generate a QR code image $ objectnewQRcode (); $ urlwww. sh ThinkPHP3.2.3 extended QR code

Set the security path first.

Simple use without logo:

Public function qrcode () {Vendor ('phpqrcode. phpqrcode '); // Generate a QR code image $ object = new \ QRcode (); $ url = 'http: // www. shouce. ren/'; // URL or text content $ level = 3; $ size = 4; $ errorCorrectionLevel = intval ($ level ); // fault tolerance level $ matrixPointSize = intval ($ size); // Generate Image size $ object-> png ($ url, false, $ errorCorrectionLevel, $ matrixPointSize, 2 );}

Advanced use with logo:

Public function qrcode () {Vendor ('phpqrcode. phpqrcode '); // Generate a QR code image $ object = new \ QRcode (); $ qrcode_path = ''; $ file_tmp_name =''; $ errors = array (); if (! Empty ($ _ POST) {$ content = trim ($ _ POST ['content']); // QR code content $ contentSize = $ this-> getStringLength ($ content ); if ($ contentSize> 150) {$ errors [] = 'The number of words is too long and cannot exceed 150 characters! ';} If (isset ($ _ FILES ['upimage'] ['tmp _ name']) & $ _ FILES ['upload'] ['tmp _ name'] & is_uploaded_file ($ _ FILES ['upload'] ['tmp _ name']) {if ($ _ FILES ['upimage'] ['size']> 512000) {$ errors [] = "your uploaded file is too large to exceed 500 KB. ";}$ File_tmp_name = $ _ FILES ['upimag'] ['tmp _ name']; $ fileext = array (" image/pjpeg "," image/jpeg ", "image/gif", "image/x-png", "image/png"); if (! In_array ($ _ FILES ['upimage'] ['type'], $ fileext) {$ errors [] = "the file you uploaded is incorrectly formatted and only supports png, jpg and gif formats. ";}}$ Tpgs =$ _ POST ['tpg']; // image format $ qrcode_bas_path = 'upload/qrcode/'; if (! Is_dir ($ qrcode_bas_path) {mkdir ($ qrcode_bas_path, 0777, true);} $ uniqid_rand = date ("Ymdhis "). uniqid (). rand (1,1000); $ qrcode_path = $ qrcode_bas_path. $ uniqid_rand. "_ 1. ". $ tpgs; // original image path $ qrcode_path_new = $ qrcode_bas_path. $ uniqid_rand. "_ 2. ". $ tpgs; // The Image path of the QR code, if (Helper: getOS () = 'Linux ') {$ mv = move_uploaded_file ($ file_tmp_name, $ qrcode_path );} else {// solve the problem of garbled Chinese file names in windows $ save_path = Helper: safeEncoding ($ Qrcode_path, 'gb2312'); if (! $ Save_path) {$ errors [] = 'upload failed. please try again! ';} $ Mv = move_uploaded_file ($ file_tmp_name, $ qrcode_path);} if (empty ($ errors) {$ errorCorrectionLevel = $ _ POST ['errorcorrectionlevel']; // fault tolerance level $ matrixPointSize =$ _ POST ['matrixpointsize']; // generate the image size $ matrixMarginSize =$ _ POST ['matrixmarginsize']; // margin size // Generate a QR code image $ object: png ($ content, $ qrcode_path_new, $ errorCorrectionLevel, $ matrixPointSize, $ matrixMarginSize); $ QR = $ qrcode_path_new; // generated original QR code diagram $ logo = $ qrcode_path; // prepared logo image if (file_exists ($ logo )) {$ 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; // re-combine the image and resize imagecopyresampled ($ QR, $ logo, $ from_width, $ from_width, 0, 0, $ logo_qr_width, $ logo_qr_height, $ logo_width, $ logo_height); // output image // header ("Content-type: image/png "); imagepng ($ QR, $ qrcode_path); imagedestroy ($ QR);} else {$ qrcode_path = $ qrcode_path_new ;}} else {$ qrcode_path = ''; }}$ data = array ('data' => array ('errors '=> $ errors, 'qrcode _ path' => $ qrcode_path); $ this-> assign ('data', $ data); $ this-> display ();

Demo address: http://www.shouce.ren/tool/qrcode

Helper class Helper Address: http://www.thinkphp1.cn/topic/34875.html

  

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.