1024 * 1024 * 5, // 默认最大上传5M 'fileExt' => 'jpeg,jpg,png', // 默认上传文件后缀 'fileMime' => 'image/jpeg,image/png,image/gif' // 默认上传文件mime ]; protected $dir = true; protected $originalName = false; protected $pathPrefix = ''; protected $fileNamePrefix = 'cms_'; protected $domain = ''; public function __construct($conf = []) { $this->domain = Env::get("QINIU.cdn"); if (!empty($conf['bucket'])) { $this->bucket = $conf['bucket']; } if (!empty($conf['accessKey'])) { $this->accessKey = $conf['accessKey']; } if (!empty($conf['secretKey'])) { $this->secretKey = $conf['secretKey']; } if (!empty($conf['pathPrefix'])) { $this->pathPrefix = trim($conf['pathPrefix'], '/'); } } /** * 生成随机字符串 */ private function random($length, $type="string", $convert="0") { $conf = [ 'number' => '0123456789', 'string' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 'all' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789=' ]; $string = $conf[$type]; if(!$string) { $string = $conf['string']; } $strlen = strlen($string) - 1; $char = ''; for($i=0; $i<$length; $i++) { $char .= $string[mt_rand(0, $strlen)]; } if($convert > 0) { $res = strtoupper($char); } elseif ($convert == 0) { $res = $char; } elseif ($convert < 0) { $res = strtolower($char); } return $res; } /** * 组装文件名 */ private function buildFileName() { return $this->fileNamePrefix . time().substr(time(),-5).substr(microtime(),2,3) . $this->random(8); } /** * 上传验证规则 */ public function validate($rule) { $this->rule = $rule; } /** * 上传文件到七牛云 * @param $name * @param bool $multi 多文件模式 * @param int $num 多文件模式文件数量 * @return array * @throws \Exception */ public function uploadFile($name,$multi=false,$num = 1) { // 构建鉴权对象 $auth = new Auth($this->accessKey, $this->secretKey); // 生成上传 Token $token = $auth->uploadToken($this->bucket); // 初始化 UploadManager 对象并进行文件的上传。 $uploadMgr = new UploadManager(); $data = []; if (false === $multi) { $file = request()->file($name); $data = $this->doFileToQn($file, $uploadMgr, $token); } else { for ($i = 0; $i < $num; $i++) { $file = request()->file($name)[$i]; $data[] = $this->doFileToQn($file, $uploadMgr, $token); } } return $data; } /** * 七牛云上传本地文件,并且本地相对路由和七牛云路径完全一致 * @param string $fullName 文件名称带全路径。/www/wwwroot/test.yulinzhihou.com/public/ueditor/php/upload/file/20210424/1619232876970230.png * @param string $relativePath 相对路径,注意:前面不能以斜杠开头 如:ueditor/php/upload/file/20210424/1619232876970230.png * @return array * @throws \Exception */ public function uploadLocalFile($fullName,$relativePath) { // 构建鉴权对象 $auth = new Auth($this->accessKey, $this->secretKey); // 生成上传 Token $token = $auth->uploadToken($this->bucket); // 初始化 UploadManager 对象并进行文件的上传。 $uploadMgr = new UploadManager(); $file = ltrim($relativePath,'\/'); list($ret, $err) = $uploadMgr->putFile($token,$file,$fullName); if ($err !== null) { throw new \Exception($err); } else { return ['hash'=>$ret['hash'], 'filename'=>$ret['key'], 'remote_url'=>$this->domain . $ret['key']]; } } /** * @param $file * @param UploadManager $uploadMgr * @param string $token * @return array * @throws \Exception */ private function doFileToQn($file, UploadManager $uploadMgr, string $token): array { $aspectRatio = []; if (!empty($this->rule['aspectRatio'])) { $aspectRatio = $this->rule['aspectRatio']; unset($this->rule['aspectRatio']); } $fileName = $file->getOriginalName(); // 文件原名 if (!$this->originalName) { $fileName = $this->buildFileName() . '.' . $file->extension(); if (!$this->dir && !empty($this->pathPrefix)) { $fileName = $this->pathPrefix . '/' . $fileName; } } if ($this->dir) { $fileName = date('Y') . '/' . date('m') . '/' . date('d') . '/' . $fileName; if (!empty($this->pathPrefix)) { $fileName = $this->pathPrefix . '/' . $fileName; } } $filePath = $file->getPathname(); // 临时路径 if (!empty($aspectRatio)) { // 验证图片宽高 list($width, $height, $type, $attr) = getimagesize($file); if ($width != $aspectRatio['width'] || $height != $aspectRatio['height']) { throw new \Exception('图片宽高不符合'); } } $fileType = $file->getOriginalMime(); list($ret, $err) = $uploadMgr->putFile($token, $fileName, $filePath, null, $fileType, false); if ($err !== null) { throw new \Exception($err); } else { return ['hash' => $ret['hash'], 'filename' => $ret['key'], 'remote_url' => $this->domain . $ret['key']]; } } }