feat: 添加svg矢量图的支持 (#833)

This commit is contained in:
zhendery 2024-12-28 20:47:52 +08:00 committed by GitHub
parent 66d7a0dce1
commit fe5e4d9a67
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 13 additions and 11 deletions

View File

@ -165,7 +165,7 @@ class Controller extends BaseController
if ( if (
$image->group?->configs->get(GroupConfigKey::IsEnableWatermark) && $image->group?->configs->get(GroupConfigKey::IsEnableWatermark) &&
$configs->get('mode', Mode::Overlay) == Mode::Dynamic && $configs->get('mode', Mode::Overlay) == Mode::Dynamic &&
! in_array($image->extension, ['ico', 'gif']) ! in_array($image->extension, ['ico', 'gif', 'svg'])
) { ) {
$contents = $service->stickWatermark($contents, $configs)->encode()->getEncoded(); $contents = $service->stickWatermark($contents, $configs)->encode()->getEncoded();
} }
@ -184,8 +184,8 @@ class Controller extends BaseController
$mimetype = $image->mimetype; $mimetype = $image->mimetype;
// ico 图片直接输出,不经过 InterventionImage 处理 // ico svg 图片直接输出,不经过 InterventionImage 处理
if ($image->extension === 'ico') { if (in_array($image->extension, ['ico', 'svg'])) {
goto out; goto out;
} }

View File

@ -38,7 +38,7 @@ class GroupRequest extends FormRequest
'configs.image_save_format' => '', 'configs.image_save_format' => '',
'configs.path_naming_rule' => 'max:400', 'configs.path_naming_rule' => 'max:400',
'configs.file_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', 'configs.accepted_file_suffixes' => 'required|array|in:jpeg,jpg,png,gif,tif,bmp,ico,psd,webp,svg',
'configs.is_enable_scan' => 'boolean', 'configs.is_enable_scan' => 'boolean',
'configs.scanned_action' => [ 'configs.scanned_action' => [

View File

@ -151,8 +151,8 @@ class ImageService
// 上传频率限制 // 上传频率限制
$this->rateLimiter($configs, $request); $this->rateLimiter($configs, $request);
// 图片处理,跳过 ico gif // 图片处理,跳过 ico gif svg
if (! in_array($extension, ['ico', 'gif'])) { if (! in_array($extension, ['ico', 'gif', 'svg'])) {
// 图片保存质量与格式 // 图片保存质量与格式
$quality = $configs->get(GroupConfigKey::ImageSaveQuality, 75); $quality = $configs->get(GroupConfigKey::ImageSaveQuality, 75);
$format = $configs->get(GroupConfigKey::ImageSaveFormat); $format = $configs->get(GroupConfigKey::ImageSaveFormat);
@ -245,8 +245,8 @@ class ImageService
throw new UploadException('图片记录保存失败'); throw new UploadException('图片记录保存失败');
} }
// 图片检测,跳过 tif、ico 以及 psd 格式 // 图片检测,跳过 tif、ico、psd、svg 格式
if ($configs->get(GroupConfigKey::IsEnableScan) && ! in_array($extension, ['psd', 'ico', 'tif'])) { if ($configs->get(GroupConfigKey::IsEnableScan) && ! in_array($extension, ['psd', 'ico', 'tif', 'svg'])) {
$scanConfigs = $configs->get(GroupConfigKey::ScanConfigs); $scanConfigs = $configs->get(GroupConfigKey::ScanConfigs);
if ($this->scan( if ($this->scan(
driver: $scanConfigs['driver'], driver: $scanConfigs['driver'],
@ -266,8 +266,10 @@ class ImageService
} }
} }
// 生成缩略图 // 生成缩略图svg等格式本身体积足够小且网页原生支持(比生成的png缩略图还小),不用生成缩略图
$this->makeThumbnail($image, $file); if(!in_array($extension, ['svg'])) {
$this->makeThumbnail($image, $file);
}
return $image; return $image;
} }

View File

@ -106,7 +106,7 @@ return [
GroupConfigKey::LimitPerDay => 300, GroupConfigKey::LimitPerDay => 300,
GroupConfigKey::LimitPerWeek => 600, GroupConfigKey::LimitPerWeek => 600,
GroupConfigKey::LimitPerMonth => 999, GroupConfigKey::LimitPerMonth => 999,
GroupConfigKey::AcceptedFileSuffixes => ['jpeg', 'jpg', 'png', 'gif', 'tif', 'bmp', 'ico', 'psd', 'webp'], GroupConfigKey::AcceptedFileSuffixes => ['jpeg', 'jpg', 'png', 'gif', 'tif', 'bmp', 'ico', 'psd', 'webp', 'svg'],
GroupConfigKey::ImageSaveFormat => '', GroupConfigKey::ImageSaveFormat => '',
GroupConfigKey::ImageSaveQuality => 75, GroupConfigKey::ImageSaveQuality => 75,
GroupConfigKey::PathNamingRule => '{Y}/{m}/{d}', GroupConfigKey::PathNamingRule => '{Y}/{m}/{d}',