Composer 安装 SDK
composer require qiniu/php-sdk
安装图像扩展
composer require topthink/think-image
控制器方法页面:
// 引入鉴权类
use Qiniu\Auth;
// 引入上传类
use Qiniu\Storage\UploadManager;
public function upload(Request $request)
{
$file = $request->file('article_logo');
$savename = \think\facade\Filesystem::disk('public')->putFile('topic', $file);
$image = \think\Image::open("$file");
// 按照原图的比例生成一个最大为400*400的缩略图并保存为thumb.png
$image->text('全体两万薪', realpath('STENCIL.TTF'), 30, '#FC011A')
->water('./logo.png')->thumb(400, 400)->save("storage/$savename");
//获取绝对路径 在项目的public目录下
$path='D:\Software\phpstudy_pro\WWW\mon7-2\exercise\week2zc\public\storage/'.$savename;
//七牛云上传
// 获取图片后缀
$ext = $file->getOriginalExtension();
// 上传到七牛后保存的新图片名
$newImageName = substr(md5($file->getOriginalName()),0,6)
. rand(00000,99999) . '.'.$ext;
$auth = new Auth('七牛云ak','七牛云sk');
// 要上传的空间位置
$token = $auth->uploadToken('储备空间名');
// 初始化 UploadManager 对象并进行文件的上传。
$uploadMgr = new UploadManager();
list($ret , $err) = $uploadMgr->putFile($token , $newImageName , $path);
$postData['article_logo'] = $newImageName;
}
前端显示
<img src='http://域名/{$val.file}'>
|