userId = $userId; } /** * 上传图片 * * @param UploadedFile $file 上传的文件 * @param bool $private 是否私有 * @return FileImg 图片模型 * @throws \Exception 上传失败时抛出异常 */ public function uploadImg(UploadedFile $file, $private = false) { $dirLogic = new DirLogic(); $dir = $dirLogic->getDir($this->userId); // 重绘 $upfilepath = $file->getRealPath(); // create image manager with desired driver $manager = new ImageManager(new Driver()); // read image from file system $image = $manager->read($upfilepath); // resize image proportionally $image->scale($image->width(), $image->height()); $path = $dir . '/' . uniqid() . '.webp'; // save modified image in new format $webpimg = $image->toWebp(); $fileString = $webpimg->toString(); $size = $webpimg->size(); // 保存 $disk = StorageConfig::getStorage(); $res = Storage::disk($disk)->put($path, $fileString); if ($res === false) { throw new \Exception('服务器错误,请联系客服.'); } $f = new FileImg(); $f->storage_disk = $disk; $f->path = $path; $f->user_id = $this->userId; $f->o_name = $file->getClientOriginalName(); $f->type1 = 'webp'; $f->fsize = $size; $f->private = (int)$private; $f->re_id = 0; $f->re_type = ''; $f->save(); $f->refresh(); return $f; } }