config['root'] . $this->config['dirname'] . DIRECTORY_SEPARATOR; if (!$this->createDir($basePath)) { throw new StorageException('文件夹创建失败,请核查是否有对应权限。'); } if (!$this->createDir($basePath . '/thumb')) { throw new StorageException('文件夹创建失败,请核查是否有对应权限。'); } $baseUrl = $this->config['domain'] . $this->config['uri'] . str_replace(DIRECTORY_SEPARATOR, '/', $this->config['dirname']) . DIRECTORY_SEPARATOR; $imageType = ['jpeg', 'jpg', 'png', 'gif']; foreach ($this->files as $key => $file) { //读取文件后缀 $extension = strtolower($file->getUploadExtension()); if (in_array($extension, $imageType)) { // 缩略 $image = Image::make($file); } $uniqueId = hash_file($this->algo, $file->getPathname()); $saveFilename = $uniqueId . '.' . $file->getUploadExtension(); $originSavePath = $basePath . $saveFilename; $thumbSavePath = in_array($extension, $imageType) ? ($basePath . 'thumb' . DIRECTORY_SEPARATOR . $saveFilename) : ($basePath . DIRECTORY_SEPARATOR . $saveFilename); $temp = [ 'key' => $key, 'origin_name' => $file->getUploadName(), 'save_name' => $saveFilename, 'save_path' => $thumbSavePath, 'url' => $baseUrl . 'thumb' . DIRECTORY_SEPARATOR . $saveFilename, 'unique_id' => $uniqueId, 'size' => $file->getSize(), 'mime_type' => $file->getUploadMineType(), 'extension' => $file->getUploadExtension(), ]; // 保存原图 $file->move($originSavePath); if (in_array($extension, $imageType)) { // 保存缩略图 $imgWidth = $image->width(); $imgHeight = $image->height(); $rate = round($imgWidth / 200, 2); $height = intval($imgHeight / $rate); $image = $image->resize(200, $height); $encoded = $image->encode('jpg'); $encoded->save($thumbSavePath); } // $file->move($savePath); array_push($result, $temp); } return $result; } /** * @desc: createDir 描述 */ protected function createDir(string $path): bool { if (is_dir($path)) { return true; } $parent = dirname($path); if (!is_dir($parent)) { if (!$this->createDir($parent)) { return false; } } return mkdir($path); } }