###
Today because of work needs, completed a two-dimensional code generation image, and support click to download the
###
Controller files, related codes
1 //generate two-dimensional code2 $url= Action (' Apih5\\[email protected] ', [' provider ' = =$request-Channel]);3 //save QR Code to local and return QR code4 $qrcode=$this->app[' version ']->qrcode ($url);
5 $dir _path= '.. /public/static/versionchannel/';6 $fileName=$request->channel. '. png;7 $qrcode = Base64_decode ($qrcode);8 //Save to local, if the file does not exist, create a new9 $this->app[' Version ']->Saveversionqrcode ($qrcode, $dir _path, $fileName);Ten One //save QR Code to local and return QR code A $qrcode=$this->app[' Version ']->qrcode ($request->channel,$url);
Background implementation of the downloaded controller
1<?PHP2 3 namespace App\http\controllers\apih5;4 5 Useilluminate\http\request;6 7 Useapp\http\requests;8 UseApp\http\controllers\controller;9 Ten classVersioncontrollerextendsController One { A //version Two-dimensional code download - Public functionDownload$provider, Request$request) - { the //download QR code - $contenttype= ' Image/jpeg '; - $dir _path= '.. /public/static/versionchannel/'; - $fileName=$provider.‘. png; + $fileurl=$dir _path.$fileName; - + Header("Cache-control:private"); A Header ("Content-type: $contenttype"); // set the file types to download at Header ("Content-length:".) filesize ($fileurl)); // set the file size to download files - Header("Content-disposition:attachment; Filename= ". UrlDecode ($fileName)); // set the file name to download readfile($fileurl); - - } in}
File where method is located
1 /**2 * Generate two-dimensional code for Apih5/versioncontroller3 * @param [Type] $id [description]4 * @return [Type] [description]5 */6 Public functionQRCode ($url)7 {8 $qrcode=NewQrcodehelper;9 $qrcode=$qrcode->GETVERSIONCHANNELQR ($url);Ten One return $qrcode; A } - - /** the * Save QR Code to local folder - */ - Public functionSaveversionqrcode ($qrcodeData,$dir _path,$fileName) - { + //determines whether the directory exists, does not exist, and generates - if(!file_exists($dir _path) ) { + mkdir("$dir _path", 0777,true); A } at $fileurl=$dir _path.$fileName; - //Save to local, if the file does not exist, create a new - file_put_contents($fileurl,$qrcodeData,File_use_include_path); -}
Front desk trying to
Displays the location of the QR code picture
1 <td>23 $version->channel])}} ">4 $version->channel. " PNG ')}} "/>5 </a>6 </td>
How does PHP implement the image click Download and save locally? -----This example is a two-dimensional code generation image, and support click to download