extension() != 'gif') { // 压缩图片 $originalName = static::randomFileName($extension ?: $upload->getClientOriginalExtension()); $storagePath = storage_path('app/public/file/') . $originalName; $img = Image::make($upload->getRealPath()); // 未传宽度 大于800压缩至800 否则按照原宽度 $width = $img->width() >= 800 ? 800 : $img->width(); // 高度自适应 $img->resize($width, null, function ($constraint) { $constraint->aspectRatio(); }); $img->save($storagePath); $upload = new UploadedFile($storagePath, $originalName); } // 上传并删除压缩图片 try { $filePath = static::put($bucketPath, $upload->getRealPath(), $upload->getClientOriginalExtension()); } catch (OssException $e) { throw $e; } finally { if (isset($storagePath)) { unlink($storagePath); } } return $filePath; } //上传图片 public static function putForUploadedFile($bucketPath, UploadedFile $upload) { return static::put($bucketPath, $upload->getRealPath(), $upload->getClientOriginalExtension()); } public static function put($bucketPath, $filepath, $ext) { static::init(); try { $filePath = static::$prefix . $bucketPath . '/' . static::randomFileName($ext); static::$ossClient->uploadFile(static::$bucket, $filePath, $filepath); } catch (OssException $e) { throw $e; } return $filePath; } public static function buildFilePath() { return date('Y') . '/' . date('m') . '/' . date('d'); } public static function randomFileName($ext, $len = 40) { return Str::random($len) . '.' . $ext; } //删除修改前,上传修改后图片 public static function delAndUploadImage($file, $filePath, UploadedFile $upload) { static::delFile($file); return static::putForUploadedFile($filePath, $upload); } //删除修改前,上传修改后视频 public static function delAndUploadVideo($file, $filePath, UploadedFile $upload) { static::delFile($file); return static::putForUploadedFile($filePath, $upload); } public static function delFile($file) { static::init(); if (static::exist($file)) { return static::$ossClient->deleteObject(static::$bucket, $file); } return false; } public static function exist($file) { static::init(); return static::$ossClient->doesObjectExist(static::$bucket, $file); } public static function getFullImgUrl($filePath) { return config('filesystems.disks.aliyun.url') . $filePath; } }