支持使用角色组控制上传图片的质量于格式 (Closed #415)

This commit is contained in:
Wisp X 2022-08-17 11:35:05 +08:00
parent 289ecffbb1
commit c46207c278
6 changed files with 68 additions and 10 deletions

View File

@ -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';
}

View File

@ -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' => '是否启用图片审核',

View File

@ -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(

View File

@ -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,

View File

@ -79,6 +79,21 @@
<x-input type="text" name="configs[file_naming_rule]" id="file_naming_rule" autocomplete="file_naming_rule" placeholder="请输入文件命名规则" value="{{ $default->get('file_naming_rule') }}" />
</div>
<div class="col-span-6 sm:col-span-3">
<label for="image_save_quality" class="block text-sm font-medium text-gray-700">图片保存质量</label>
<x-input type="number" name="configs[image_save_quality]" id="image_save_quality" autocomplete="path_naming_rule" placeholder="请输入图片保存质量" value="{{ $default->get('image_save_quality', 100) }}" />
</div>
<div class="col-span-6 sm:col-span-3">
<label for="image_save_format" class="block text-sm font-medium text-gray-700">图片转换格式</label>
<x-select id="configs[image_save_format]" name="configs[image_save_format]" autocomplete="image_save_format">
<option value="">不转换格式</option>
@foreach($default->get('accepted_file_suffixes') as $extension)
<option value="{{ strtolower($extension) }}">{{ strtoupper($extension) }}</option>
@endforeach
</x-select>
</div>
<div class="col-span-6">
<x-fieldset title="是否默认" faq="设置默认后,新用户注册以后将会属于该默认角色组,且默认组只能有一个。">
<x-switch id="is_default" name="is_default" value="1"></x-switch>

View File

@ -75,6 +75,21 @@
<x-input type="text" name="configs[file_naming_rule]" id="file_naming_rule" autocomplete="file_naming_rule" placeholder="请输入文件命名规则" value="{{ $group->configs->get('file_naming_rule') }}" />
</div>
<div class="col-span-6 sm:col-span-3">
<label for="image_save_quality" class="block text-sm font-medium text-gray-700">图片保存质量</label>
<x-input type="number" name="configs[image_save_quality]" id="image_save_quality" autocomplete="path_naming_rule" placeholder="请输入图片保存质量" value="{{ $group->configs->get('image_save_quality', 100) }}" />
</div>
<div class="col-span-6 sm:col-span-3">
<label for="image_save_format" class="block text-sm font-medium text-gray-700">图片转换格式</label>
<x-select id="configs[image_save_format]" name="configs[image_save_format]" autocomplete="image_save_format">
<option value="">不转换格式</option>
@foreach($default->get('accepted_file_suffixes') as $extension)
<option value="{{ strtolower($extension) }}" @selected($group->configs->get('image_save_format') === $extension)>{{ strtoupper($extension) }}</option>
@endforeach
</x-select>
</div>
<div class="col-span-6">
<x-fieldset title="是否默认" faq="设置默认后,新用户注册以后将会属于该默认角色组,且默认组只能有一个。">
<x-switch id="is_default" name="is_default" value="1" :checked="(bool)$group->is_default"></x-switch>