支持 exists 文件名 ase tro size col chm
###
今天因为工作需要,完成了一个二维码的生成图片,并支持点击下载的
###
控制器文件,相关代码
- 1 // 生成二维码
- 2 $url = action('Apih5\\ [email protected], ['provider' = >$request - >channel]);
- 3 // 保存二维码到本地,并返回二维码
- 4 $qrcode = $this - >app['version'] - >qrcode($url);
- 5 $dir_path = '.. / public / static / versionChannel / ';
- 6 $fileName = $request - >channel.'.png';
- 7 $qrcode = base64_decode($qrcode);
- 8 // 保存到本地,如果不存在文件,则创建新的
- 9 $this - >app['version'] - >saveVersionQrcode($qrcode, $dir_path, $fileName);
- 10 11 // 保存二维码到本地,并返回二维码
- 12 $qrcode = $this - >app['version'] - >qrcode($request - >channel, $url);
后台实现下载的控制器
- <?php
- namespace App\Http\Controllers\Apih5;
- use Illuminate\Http\Request;
- use App\Http\Requests;
- use App\Http\Controllers\Controller;
- class VersionController extends Controller
- {
- //版本二维码下载
- public function download($provider, Request $request)
- {
- // 下载二维码
- $contenttype = 'image/jpeg';
- $dir_path = '../public/static/versionChannel/';
- $fileName = $provider.'.png';
- $fileurl = $dir_path.$fileName;
- header("Cache-control: private");
- header("Content-type: $contenttype"); //设置要下载的文件类型
- header("Content-Length:" . filesize($fileurl)); //设置要下载文件的文件大小
- header("Content-Disposition: attachment; filename=" . urldecode($fileName)); //设置要下载文件的文件名
- readfile($fileurl);
- }
- }
方法所在文件
- /**
- * 生成二维码 for apih5/VersionController
- * @param [type] $id [description]
- * @return [type] [description]
- */
- public
- function qrcode($url) {
- $qrcode = new QrcodeHelper;
- $qrcode = $qrcode - >getVersionChannelQr($url);
- return $qrcode;
- }
- /**
- * 保存二维码到本地文件夹
- */
- public
- function saveVersionQrcode($qrcodeData, $dir_path, $fileName) {
- // 判断目录是否存在,不存在则生成
- if (!file_exists($dir_path)) {
- mkdir("$dir_path", 0777, true);
- }
- $fileurl = $dir_path.$fileName;
- // 保存到本地,如果不存在文件,则创建新的
- file_put_contents($fileurl, $qrcodeData, FILE_USE_INCLUDE_PATH);
- }
前台试图
显示二维码图片的位置
- <td>
- <a href="{{ action('Apih5\\[email protected],['provider' => $version->channel]) }}">
- <img width="100px" src="{{url('/static/versionChannel/'.$version->channel.'.png')}}"
- />
- </a>
- </td>
php 如何实现图片点击下载,并保存本地?----- 本例子为二维码的生成图片,并支持点击下载
来源: http://www.bubuko.com/infodetail-2067966.html