> composer require qiniu/php-sdk
*****
`
~~~
<?php
// +----------------------------------------------------------------------
// | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.luckyhhy.cn
// +----------------------------------------------------------------------
// | Author: LuckyHhy <jackhhy520@qq.com>
// +----------------------------------------------------------------------
// | Date: 2020/3/3-14:18
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------
namespace Jrk;
use Qiniu\Auth;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;
use Qiniu\Config;
use think\facade\Cache;
/**
* TODO 七牛云上传
* Class Qiniu
*/
class QiniuUp{
protected static $accessKey;
protected static $secretKey;
protected static $auth = null;
//TODO 空间域名 Domain
protected static $uploadUrl;
//TODO 存储空间名称 公开空间
protected static $storageName;
/**
* @return Auth|null
* @throws \Exception
* @author: LuckyHhy <jackhhy520@qq.com>
* @date: 2020/3/3
* @name: autoInfo
* @describe: 初始化
*/
protected static function autoInfo()
{
if (($storageName = Cache::get('storageName')) && ($uploadUrl = Cache::get('uploadUrl')) && ($accessKey = Cache::get('accessKey')) && ($secretKey = Cache::get('secretKey'))) {
self::$accessKey = $accessKey;
self::$secretKey = $secretKey;
self::$uploadUrl = $uploadUrl;
self::$storageName = $storageName;
} else {
$config = \think\facade\Config::get('app.config');
$qi_config = $config['qiniu'];
self::$accessKey = trim($qi_config['accessKey']);
self::$secretKey = trim($qi_config['secretKey']);
self::$uploadUrl = trim($qi_config['uploadUrl']) . '/';
self::$storageName = trim($qi_config['storage_name']);
Cache::set('accessKey', self::$accessKey);
Cache::set('secretKey', self::$secretKey);
Cache::set('uploadUrl', self::$uploadUrl);
Cache::set('storageName', self::$storageName);
}
if (!self::$accessKey || !self::$secretKey || !self::$uploadUrl || !self::$storageName) {
exception('请设置 secretKey 和 accessKey 和 空间域名 和 存储空间名称');
}
if (self::$auth == null) self::$auth = new Auth(self::$accessKey, self::$secretKey);
return self::$auth;
}
/**
* @return array
* @throws \Exception
* @author: LuckyHhy <jackhhy520@qq.com>
* @date: 2020/3/3
* @name: getToKenAndDomainI
* @describe:获取上传图片视频token和domain
*/
public static function getToKenAndDomainI()
{
$token = self::autoInfo()->uploadToken(self::$storageName);
$domain = self::$uploadUrl;
$fileName = md5(rand(1000, 9999) . date('YmdHis') . rand(0, 9999));
return compact('token', 'domain', 'fileName');
}
/**
* @param string $filename
* @return array|string
* @throws \Exception
* @author: LuckyHhy <jackhhy520@qq.com>
* @date: 2020/3/3
* @name: uploadImage
* @describe: 图片上传 名称
*/
public static function uploadImage($filename = 'image')
{
$request = app('request');
$file = $request->file($filename);
$filePath = $file->getRealPath();
$ext = $file->getOriginalExtension();
$key = substr(md5($file->getRealPath()), 0, 5) . date('YmdHis') . rand(0, 9999) . '.' . $ext;
$token = self::autoInfo()->uploadToken(self::$storageName);
try {
$uploadMgr = new UploadManager();
return $uploadMgr->putFile($token, $key, $filePath);
} catch (\Exception $e) {
return $e->getMessage();
}
}
/**
* @param $key
* @param $content
* @return array|string
* @throws \Exception
* @author: LuckyHhy <jackhhy520@qq.com>
* @date: 2020/3/3
* @name: uploadImageStream
* @describe:图片上传 内容
*/
public static function uploadImageStream($key, $content)
{
$token = self::autoInfo()->uploadToken(self::$storageName, $key);
try {
$uploadMgr = new UploadManager();
return $uploadMgr->put($token, $key, $content);
} catch (\Exception $e) {
return $e->getMessage();
}
}
/**
* @param $key
* @param string $type
* @param string $imageUrl
* @param int $time
* @return array
* @author: LuckyHhy <jackhhy520@qq.com>
* @date: 2020/3/3
* @name: imageUrl
* @describe:图片下载链接
*/
public static function imageUrl($key, $type = '', $imageUrl = '', $time = 0)
{
return ['code' => 200, 'name' => $key, 'dir' => self::$uploadUrl . $key, 'thumb_path' => self::$uploadUrl . $key, 'time' => time()];
// $imageValue = !strlen(trim($type)) ? self::$uploadUrl.$key : self::$uploadUrl.$key.self::getType($type);
// if($time > time() && !strlen(trim($imageUrl))) return ['code'=>100,'dir'=>$imageUrl,'thumb_path'=>$imageUrl,'time'=>$time];
// $imageUrl = self::autoInfo()->privateDownloadUrl($imageValue);
// return ['code'=>200,'name'=>$key,'dir'=>$imageUrl,'thumb_path'=>$imageUrl,'time'=>bcadd(time(),3600,0)];
}
/**
* @param $imageType
* @return string
* @author: LuckyHhy <jackhhy520@qq.com>
* @date: 2020/3/3
* @name: getType
* @describe:获取图片时转换图片大小
*/
public static function getType($imageType)
{
$type = '';
switch ($imageType) {
case "8x6":
$type = '?imageView2/1/w/800/h/600';
break;
}
return $type;
}
/**
* @param $key
* @return mixed
* @throws \Exception
* @author: LuckyHhy <jackhhy520@qq.com>
* @date: 2020/3/3
* @name: delete
* @describe:删除资源
*/
public static function delete($key)
{
$bucketManager = new BucketManager(self::autoInfo(), new Config());
return $bucketManager->delete(self::$storageName, $key);
}
}
~~~
`
- 空白目录
- thinkphp5
- tools-常用类库
- redis类库
- Excel类库
- File文件操作类库
- Http请求类库
- Maile邮件发送
- Hooks行为钩子
- 七牛云
- 随机数和字符串生成
- 字符串处理
- 时间类处理
- tree型转换
- 工具类库
- 文件打包下载
- 常用功能
- 文件上传
- php生成word文档
- elasticsearch 基本搜索
- 使用jwt开发API接口
- 安装模及搭建
- ApiCheck.php
- ApiCheckLogin.php
- common.php
- Login.php
- Comment.php
- 汉字转拼音
- 安装使用
- Pinyin类
- elasticsearch操作
- 常用方法
- 数据源生成layui-select
- 获取自定义配置项
- 百度编辑器
- 格式化文件大小
- 多语言设置
- hook监听
- 域名绑定到模块
- thinkphp6
- 文件上传
- tp5totp6
- 创建路径
- 获取类所有方法
- password_hash加密验证
- 生成 qrcode
- 邮件发送
- 获取QQ信息
- GoogleAuthenticator
- redis限流
- redis 加锁
- 百度翻译
- QueryList爬取数据
- 获取时间类
- 命令
- Git常用命令
- easyswoole
- pix_qrcode
- 验证 cpf,cnpj
- php常用方法
- 日志
- 卡通头像
- 两位小数
- 图片转base64
- auth加密解密
- phpoffice/phpspreadsheet导入导出
- fastadmin
- 树结构
- 单选框
- 复选框
- 二级搜索
- select选择框
- selectpage选中回调事件
- 标签添加
- 修改where条件
- 表格列表中添加input框
- selectpage事件
- fieldlist
- js操作
- test_js
- 多表格
- template模板