Php allows you to generate and forcibly download images with QR codes,

Source: Internet
Author: User

Php allows you to generate and forcibly download images with QR codes,

This article mainly shares with you how to generate a QR code image with a QR code and forcibly download the implementation code, hoping to help you implement your own QR code.

/* Download the QR code */public function Test () {// receives the passed parameter $ data ['keys '] = ['b01id' => trim (I ('get. b01id '), 'b02id' => trim (I ('get. b02id '), 'b03id' => trim (I ('get. b03id'), 'brid' => trim (I ('get. brid'), 'unicon' => trim (I ('get. unicon ')]; if (empty ($ data ['keys'] ['b01id']) $ this-> error ('Merchant does not exist ', U ('seller/Index'), 3); $ res = D ('sellerbrcode')-> download ($ data); // The method is shown below if (! Is_array ($ res) $ this-> error ($ res, U ('seller/Index'), 3); if (! Empty ($ res ['error']) $ this-> error ($ res ['error'], U ('seller/seebrcode? '. $ Res ['url']), 3); // create a scan card $ bgimg = imagecreatefromstring (file_get_contents ($ res ['bgimg ']); // get the background image as the canvas import ('component/phpqrcode', APP_PATH ,'. php '); // introduces the QR code generation class // if there is a QR code 123, a QR code 123 if (''! = $ Res ['codea ']) {/* parameter description: * parameter 1: value to be maintained by the QR code * parameter 2: false indicates direct output, others are the generated file name * parameter 3: Error Correction level: L, M, Q, H * parameter 4: The point size. The larger the value, the larger the image **/\ QRcode :: png ($ res ['codea '], $ res ['codemg'], 'h', 18);/* use the image scaling method bool imagecopyresized ($ dst_im, $ src_im, $ dst_x, $ dst_y, $ src_x, $ src_y, $ dst_w, $ dst_h, $ src_w, $ src_h) dst_im target image Link resources. Src_im source image link resource. The destination of the dst_x X coordinate. Dst_y y coordinate destination. The source point of the src_x coordinate. Src_y y coordinate source point. Dst_w destination width. Dst_h target height. Src_w Source width. Src_h source height. **/$ Codeimg = imagecreatefromstring (file_get_contents ($ res ['codeimg ']); // get the QR code image $ bg_info = getimagesize ($ res ['codeimg']); // obtain the size of the original QR code image imagecopyresized ($ bgimg, $ codeimg, 1520,550, 700,666, $ bg_info [0], $ bg_info [1]-36 );} // if the QR code of UnionPay cloud flash payment exists, the image of the QR code of UnionPay cloud flash payment is generated if (''! = $ Res ['codeb']) {\ QRcode: png ($ res ['codeb'], $ res ['paycodeimg '], 'h', 18 ); $ codeimg = imagecreatefromstring (file_get_contents ($ res ['paycodeimg']); // get the QR code image $ bg_info = getimagesize ($ res ['paycodeimmg ']); // obtain the size of the original QR code image imagecopyresized ($ bgimg, $ codeimg, 236,536, 750,670, $ bg_info [0], $ bg_info [1]-80 );} // if the merchant promotion QR code exists, the merchant promotion QR code image if (''! = $ Res ['codec']) {\ QRcode: png ($ res ['codec'], $ res ['bridimg '], 'h', 18 ); $ codeimg = imagecreatefromstring (file_get_contents ($ res ['bridimg ']); // get the QR code image $ bg_info = getimagesize ($ res ['bridimg']); // obtain the size of the original QR code image imagecopyresized ($ bgimg, $ codeimg, 1375,1288, 230,230, $ bg_info [0], $ bg_info [1]);} // if there is a merchant name if (''! = $ Res ['bname']) {// to center the text, you need to calculate the left offset preg_match_all ("/[0-9] {1 }/", $ res ['bname'], $ len1); // obtain the number of characters in the string preg_match_all ("/[a-zA-Z] {1 }/", $ res ['bname'], $ len2); // obtain the number of letters in the string preg_match_all ("/([\ x {4e00}-\ x {9fa5}]) {1}/u ", $ res ['bname'], $ len3); // obtain the number of Chinese characters in the string $ left = 610-(count ($ len1 [0]) * 16)-(count ($ len2 [0]) * 15)-(count ($ len3 [0]) * 28); $ res ['filename']. = '_'. $ res ['bname']; // append the QR code name $ black = Imagecolorallocate ($ bgimg, 0x20, 0x57, 0xa0); // The font color imagefttext ($ bgimg, 42, 0, $ left, 480, $ black ,'. /Public/font/msyh. ttf', $ res ['bname']);} // if there is a QR code number, write the QR code number if (''! = $ Res ['brid']) {$ black = imagecolorallocate ($ bgimg, 0x3e, 0x75, 0xc7); // font color imagefttext ($ bgimg, 30, 0, 1720,1190, $ black ,'. /Public/font/msyh. ttf', $ res ['brid']);} // Add UnionPay cloud pay-as-you-go logo $ codeimg = imagecreatefromstring (file_get_contents ($ res ['join']); // get the QR code image $ bg_info = getimagesize ($ res ['Union ']); // get the size of the original logo image imagecopyresized ($ bgimg, $ codeimg, 550,760, 0, 0, 120,120 * $ bg_info [1]/$ bg_info [0], $ bg_info [0], $ bg_info [1]); // Add scan Code 123 logo $ codeimg = imagecreatefromstring (file_get_contents ($ res ['logo ']); // get the QR code image $ bg_info = getimagesize ($ res ['logo ']); // get the size of the original logo image imagecopyresized ($ bgimg, $ codeimg, 1805,770, 0, 0, 120,120 * $ bg_info [1]/$ bg_info [0], $ bg_info [0], $ bg_info [1]); $ res ['filename']. = '_'. date ('Y-m-d', time ()). '.png '; // append the two-dimensional code name header ('pragma: public'); // required header ('expires: 0 '); // No cache header ('cache-Control: must-revalidate, post-check = 0, pre-check = 0'); header ('cache-Control: private ', false); header ('content-Type: application/force-download'); header ('content-Disposition: attachment; filename = "'. $ res ['filename']. '"'); header ('content-Transfer-Encoding: binary '); header ('Connection: close'); imagepng ($ bgimg );} /* splice data before generating the image */public function download ($ data) {$ tool = D ('Tool'); // get the public class $ mysql = M (); $ admin = I ('session. admin ');/* concatenate URLs to facilitate the following errors: */$ show ['url'] = 'b01id = '. $ data ['keys '] ['b01id']; // if the user ID is passed, then determine whether the user belongs to the merchant if ($ data ['keys '] ['b03id']) $ show ['url']. = '& b03id = '. $ data ['keys '] ['b03id']; // if the two-dimensional code idif is passed ($ data ['keys'] ['b02id']) $ show ['url']. = '& b02id = '. $ data ['keys '] ['b02id']; // if the UnionPay cloud flash payment is passed if ($ data ['keys'] ['unicon ']) $ show ['url']. = '& unicon = '. $ data ['keys '] ['un Icon ']; // if the promotion QR code is passed, if ($ data ['keys'] ['brid']) $ show ['url']. = '& brid = '. $ data ['keys '] ['brid']; // call the upper method to obtain data $ data = $ this-> seebrcode ($ data, $ tool, $ mysql, $ admin, false); if (! Is_array ($ data) {return ['error' => $ data, 'url' => $ show ['url'];} /* get the data to be displayed */$ show ['bgimg '] = '. /Public/img/qrcode/qrcode.png '; // The background image address $ show ['logo'] = '. /Public/img/qrcode/logo.png '; // scan the logo address of Code 123 $ show ['unival'] = '. /Public/img/qrcode/unionPay.png '; // UnionPay logo address $ show ['codec'] = '. /Public/img/qrcode/code.png '; // save address generated by the common QR code $ show ['paycodeimg'] = '. /Public/img/qrcode/paycode.png '; // save address generated by the UnionPay QR code $ show [' Bridimg '] = '. /Public/img/qrcode/brid.png '; // address for saving the QR code image for merchant promotion $ show ['filename'] = 'qr Code 123 QR code for receiving '; // QR code name // scan Code 123 QR code content $ show ['codea '] = (! $ Data ['keys'] ['b02id'])? '': Isset ($ data ['qrcode'] [$ data ['keys '] ['b02id'])? $ Data ['url1']. $ data ['qrcode'] [$ data ['keys '] ['b02id'] ['brcode']: ''; // scan the QR Code 123 $ show ['brid'] = (! $ Data ['keys'] ['b02id'])? '': Isset ($ data ['qrcode'] [$ data ['keys '] ['b02id'])? 'No. '. $ data ['keys'] ['b02id']: ''; // UnionPay QR code $ show ['codeb'] = (! $ Data ['keys '] ['unicon'])? '': Isset ($ data ['qrcode'] [$ data ['keys '] ['unicon'])? $ Data ['qrcode'] [$ data ['keys '] ['unicon'] ['brcode']: ''; // QR code for merchant promotion $ show ['codec'] = (! $ Data ['keys'] ['brid'])? '': Isset ($ data ['Download'] [$ data ['keys '] ['brid'])? $ Data ['url2 ']. $ data ['Download'] [$ data ['keys '] ['brid'] ['brcode']: ''; // merchant name $ show ['bname'] = empty ($ data ['seller '] ['bshortname'])? (Empty ($ data ['seller '] ['bname'])? (Empty ($ data ['seller '] ['bcode'])? $ Data ['seller] ['b01id']: $ data ['seller '] ['bcode']): $ data ['seller'] ['bname']): $ data ['seller '] ['bshortname'];/* if an old image exists, delete the old image */if (is_file ($ res ['codec']) unlink ($ res ['codec']); // Delete the old QR Code 123 QR code image if (is_file ($ res ['paycodec']) unlink ($ res ['paycodeimg']); // Delete the old UnionPay cloud flash QR code image if (is_file ($ res ['paycodeimg']) unlink ($ res ['paycodeimg ']); // Delete the QR code image return $ show ;}

Summary

The above section describes how to generate a php image with a QR code and forcibly download the implementation code. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.