This article mainly share with you the PHP generation with two-dimensional code image and force download implementation code, hope to help everyone realize a QR code.
/* Download QR code */public function test () {//Receive passed parameters $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 not Present ', U (' Seller/index '), 3); $res = D (' Sellerbrcode ') Download ($data); The method 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);//Make sweep code $bgimg = imagecreatefromstring (file_get_contents ($res [' bgimg ']));//Get the background map to do the canvas import (' Component/phpqrcode ', App_path, '. php '); Introduce a QR code generation class//If there is a scan code 1232-D code, then generate a scan code 1232-D code if ("'! = $res [' Codea ']) {/* parameter description: * Parameter 1: QR code needs to keep the value * parameter 2:false represents the direct output, the other is the generated file name * Parameter 3: Error correction level: L, M, Q, H * Parameter 4: The size of the point, the larger the number of images * */\qrcode::p ng ($res [' Codea '], $res [' codeimg '], ' H ', 18);/* Use picture 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 linked resources. Src_im resources for source image linking。 Dst_x The x-coordinate of the destination. Dst_y y-coordinate destination. Src_x the source point of the x-coordinate. Src_y the y-coordinate source point. Dst_w the destination width. Dst_h target height. Src_w the source width. Src_h source height. * */$codeimg = imagecreatefromstring (file_get_contents ($res [' codeimg '));//get QR code picture $bg_info = getimagesize ($res [' Codeimg ']);//Get the original QR code image size imagecopyresized ($bgimg, $codeimg, 1520,550,0,70,700,666, $BG _info[0], $BG _info[1]-36);} If there is a UnionPay cloud flash two-dimensional code, then generate UnionPay cloud flash QR code picture if (' '! = $res [' codeb ']) {\qrcode::p ng ($res [' Codeb '], $res [' paycodeimg '], ' H ', 18); $ codeimg = imagecreatefromstring (file_get_contents ($res [' paycodeimg ']);//get QR code picture $bg_info = getimagesize ($res [' Paycodeimg ']);//Get the original QR code image size imagecopyresized ($bgimg, $codeimg, 236,536,0,80,750,670, $BG _info[0], $BG _info[1]-80);} If there is a merchant promotion QR code, then generate the merchant promotion QR code picture if (' '! = $res [' codec ']) {\qrcode::p ng ($res [' codec '], $res [' bridimg '], ' H ',.); $codeimg = Imagecreatefromstring (file_get_contents ($res [' bridimg '));//get QR code picture $bg_info = getimagesize ($res [' bridimg ']);// Get the size of the original two-dimensional code picture imagecopyresized ($bgimg, $codeimg, 1375,1288,0,0,230,230, $BG _info[0], $BG _info[1]);} If there is a merchant name if ('! = ' $res [' BNAme ']) {//for the text to center, you need to calculate the left offset preg_match_all ("/[0-9]{1}/", $res [' bname '], $len 1);//Get the number of words in the string Preg_match_all ("/[a-za-z ]{1}/", $res [' bname '], $len 2);//Get the number of letters in the string Preg_match_all ("/([\x{4e00}-\x{9fa5}]) {1}/u ", $res [' bname '], $len 3);// Get the number of Chinese in the string $left = 610-(COUNT ($len 1[0]) * (COUNT ($len 2[0])-(COUNT ($len 3[0]) *); $res [' filename ']. = ' _ ' . $res [' bname '];//append two-D code name $black = Imagecolorallocate ($bgimg, 0x20, 0x57, 0xa0);//Font Color imagefttext ($bgimg, 0, $left, 48 0, $black, './public/font/msyh.ttf ', $res [' bname ']);} If there is a QR code number, write a QR code number if (' '! = $res [' Brid ']) {$black = Imagecolorallocate ($bgimg, 0x3e, 0x75, 0xc7);//Font Color Imagefttext ($ Bgimg, 0, 1720,1190, $black, './public/font/msyh.ttf ', $res [' Brid ']);} Add UnionPay cloud Flash logo$codeimg = imagecreatefromstring (file_get_contents ($res [' Union '));//get QR code picture $bg_info = getimagesize ($ res[' Union ');//Get the original logo image size imagecopyresized ($bgimg, $codeimg, 550,760,0,0,120,120* $bg _info[1]/$bg _info[0], $BG _ Info[0], $BG _info[1]);//Add a sweep code 123logo$codeimg = ImagecreatefromstrING (file_get_contents ($res [' logo ')]);//get QR code image $bg_info = getimagesize ($res [' logo ']);// Get the original logo image size 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 two-D code name header (' Pragma:public '); Requiredheader (' expires:0 '); No Cacheheader (' 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);}
/* stitching data before generating image */public function Download ($data) {$tool = D (' tool ');//Get public class $mysql = M (); $admin = I (' session.admin ');/* Stitching URL, to facilitate the following error, jump using */$show [' url '] = ' b01id= '. $data [' Keys '] [' b01id '];//if the user ID is passed, determine if the user belongs to the merchant if ($data [' Keys '] [' B03id ']) $show [' url '].= ' &b03id= '. $data [' Keys '] [' b03id '];//if a QR code is passed idif ($data [' Keys '] [' b02id ']) $show [' url ']. = ' &b02id= '. $data [' Keys '] [' b02id '];//if the UnionPay cloud Flash payment if ($data [' Keys '] [' Unicon ']) $show [' url '].= ' &unicon= '. $ data[' Keys ' [' Unicon '];//if the promo QR code is passed if ($data [' Keys '] [' Brid ']) $show [' url '].= ' &brid= '. $data [' Keys '] [' Brid '];/ /Call the upper method to get the data $data = $this->seebrcode ($data, $tool, $mysql, $admin, False), if (!is_array ($data)) {return [' ERROR ' = $data, ' url ' = = $show [' url ']; }/* get the data to show */$show [' bgimg '] = './public/img/qrcode/qrcode.png ';//Background Image address $show[' logo '] = './public/img/qrcode/ Logo.png ';//code 123 logo address $show[' union ' = './public/img/qrcode/unionpay.png ';//UnionPay logo address $show[' codeimg '] = './public /img/qrcode/code.png ';//common QR code generated save address $show[' paycodeimg'] = './public/img/qrcode/paycode.png ';//UnionPay Two-dimensional code generated save address $show[' bridimg '] = './public/img/qrcode/brid.png ';// Merchant promotion QR Code image save address $show[' filename ']= ' sweep code 123 payment QR code ';//QR code name//scan code 1232-dimensional code content $show[' Codea '] = (! $data [' Keys '] [' b02id '])? ": Isset ($data [' QRCode '] [$data [' Keys '] [' b02id ']])? $data [' Url1 ']. $data [' QRCode '] [$data [' Keys '] [' b02id ']][' Brcode ']: ';//sweep code 1232-dimensional code number $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 ']: ';//merchant promotion QR code $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 there is an old picture, delete the old picture */if (Is_file ($res [' codeimg '])) unlink ( $res [' codeimg ']); Delete the old sweep code 1232-D code picture if (Is_file ($res [' paycodeimg '])) unlink ($res [' paycodeimg ']); Delete old UnionPay cloud flash QR code image if (Is_file ($res [' paycodeimg '])) unlink ($res [' paycodeimg ']); Delete the old merchant promotion QR code picture return $show;}