diff --git a/app/Enums/GroupConfigKey.php b/app/Enums/GroupConfigKey.php index a7608662..6cbc8cfd 100644 --- a/app/Enums/GroupConfigKey.php +++ b/app/Enums/GroupConfigKey.php @@ -54,4 +54,10 @@ final class GroupConfigKey /** @var string 图片缓存时间 */ const ImageCacheTtl = 'image_cache_ttl'; + + /** @var string 图片保存格式 */ + const ImageSaveFormat = 'image_save_format'; + + /** @var string 图片保存质量 */ + const ImageSaveQuality = 'image_save_quality'; } diff --git a/app/Http/Requests/Admin/GroupRequest.php b/app/Http/Requests/Admin/GroupRequest.php index 4c698e38..d2ef4d20 100644 --- a/app/Http/Requests/Admin/GroupRequest.php +++ b/app/Http/Requests/Admin/GroupRequest.php @@ -34,6 +34,8 @@ class GroupRequest extends FormRequest 'configs.limit_per_day' => 'required|integer', 'configs.limit_per_week' => 'required|integer', 'configs.limit_per_month' => 'required|integer', + 'configs.image_save_quality' => 'required|min:1|max:100', + 'configs.image_save_format' => '', 'configs.path_naming_rule' => 'max:400', 'configs.file_naming_rule' => 'max:400', 'configs.accepted_file_suffixes' => 'required|array|in:jpeg,jpg,png,gif,tif,bmp,ico,psd,webp', @@ -116,6 +118,8 @@ class GroupRequest extends FormRequest 'configs.limit_per_month' => '每月上传限制', 'configs.path_naming_rule' => '路径命名规则', 'configs.file_naming_rule' => '文件命名规则', + 'configs.image_save_quality' => '图片保存质量', + 'configs.image_save_format' => '图片保存格式', 'configs.accepted_file_suffixes' => '允许上传的文件后缀', 'configs.is_enable_scan' => '是否启用图片审核', diff --git a/app/Services/ImageService.php b/app/Services/ImageService.php index 74644a08..78ba5962 100644 --- a/app/Services/ImageService.php +++ b/app/Services/ImageService.php @@ -151,16 +151,32 @@ class ImageService // 上传频率限制 $this->rateLimiter($configs, $request); - // 是否启用水印,覆盖原图片 - if ( - $configs->get(GroupConfigKey::IsEnableWatermark) && - collect($configs->get(GroupConfigKey::WatermarkConfigs))->get('mode', Mode::Overlay) == Mode::Overlay && - ! in_array($extension, ['ico', 'gif']) - ) { - $watermarkImage = $this->stickWatermark($file, collect($configs->get(GroupConfigKey::WatermarkConfigs))); - $watermarkImage->save(); - $file = new UploadedFile($watermarkImage->basePath(), $file->getClientOriginalName(), $file->getMimeType()); - $watermarkImage->destroy(); + // 图片处理,跳过 ico 于 gif + if (! in_array($extension, ['ico', 'gif'])) { + // 图片保存质量与格式 + $quality = $configs->get(GroupConfigKey::ImageSaveQuality, 100); + $format = $configs->get(GroupConfigKey::ImageSaveFormat); + if ($quality < 100 || $format) { + // 获取拓展名,判断是否需要转换 + $format = $format ?: $extension; + $filename = Str::replaceLast($extension, $format, $file->getClientOriginalName()); + $handleImage = InterventionImage::make($file)->save($format, $quality); + $file = new UploadedFile($handleImage->basePath(), $filename, $handleImage->mime()); + // 重新设置拓展名 + $extension = $format; + $handleImage->destroy(); + } + + // 是否启用水印,覆盖原图片 + if ( + $configs->get(GroupConfigKey::IsEnableWatermark) && + collect($configs->get(GroupConfigKey::WatermarkConfigs))->get('mode', Mode::Overlay) == Mode::Overlay + ) { + $watermarkImage = $this->stickWatermark($file, collect($configs->get(GroupConfigKey::WatermarkConfigs))); + $watermarkImage->save(); + $file = new UploadedFile($watermarkImage->basePath(), $file->getClientOriginalName(), $file->getMimeType()); + $watermarkImage->destroy(); + } } $filename = $this->replacePathname( diff --git a/config/convention.php b/config/convention.php index 933bd763..ef6934e6 100644 --- a/config/convention.php +++ b/config/convention.php @@ -107,6 +107,8 @@ return [ GroupConfigKey::LimitPerWeek => 600, GroupConfigKey::LimitPerMonth => 999, GroupConfigKey::AcceptedFileSuffixes => ['jpeg', 'jpg', 'png', 'gif', 'tif', 'bmp', 'ico', 'psd', 'webp'], + GroupConfigKey::ImageSaveFormat => '', + GroupConfigKey::ImageSaveQuality => 100, GroupConfigKey::PathNamingRule => '{Y}/{m}/{d}', GroupConfigKey::FileNamingRule => '{uniqid}', GroupConfigKey::ImageCacheTtl => 2626560, diff --git a/resources/views/admin/group/add.blade.php b/resources/views/admin/group/add.blade.php index 9b7293fe..81a4403d 100644 --- a/resources/views/admin/group/add.blade.php +++ b/resources/views/admin/group/add.blade.php @@ -79,6 +79,21 @@ +
+ + +
+ +
+ + + + @foreach($default->get('accepted_file_suffixes') as $extension) + + @endforeach + +
+
diff --git a/resources/views/admin/group/edit.blade.php b/resources/views/admin/group/edit.blade.php index 43216cac..7a61905c 100644 --- a/resources/views/admin/group/edit.blade.php +++ b/resources/views/admin/group/edit.blade.php @@ -75,6 +75,21 @@
+
+ + +
+ +
+ + + + @foreach($default->get('accepted_file_suffixes') as $extension) + + @endforeach + +
+