Skip to content

Commit

Permalink
Add .svg image file support
Browse files Browse the repository at this point in the history
* Don't attempt to resize SVG images.
* Add SVG mime type to image validation rules.
* Add SVG mime type to accepted image types.
* Add SVG mime type to the sniffer's safe list.
  • Loading branch information
tjone270 committed Jan 16, 2025
1 parent 786a434 commit 4dbfefd
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/Exports/ZipExports/Models/ZipExportImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function metadataOnly(): void

public static function validate(ZipValidationHelper $context, array $data): array
{
$acceptedImageTypes = ['image/png', 'image/jpeg', 'image/gif', 'image/webp'];
$acceptedImageTypes = ['image/png', 'image/jpeg', 'image/gif', 'image/webp', 'image/svg+xml'];
$rules = [
'id' => ['nullable', 'int', $context->uniqueIdRule('image')],
'name' => ['required', 'string', 'min:1'],
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ protected function logActivity(string $type, string|Loggable $detail = ''): void
*/
protected function getImageValidationRules(): array
{
return ['image_extension', 'mimes:jpeg,png,gif,webp', 'max:' . (config('app.upload_limit') * 1000)];
return ['image_extension', 'mimes:jpeg,png,gif,webp,svg', 'max:' . (config('app.upload_limit') * 1000)];
}

/**
Expand Down
13 changes: 13 additions & 0 deletions app/Uploads/ImageResizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public function resizeToThumbnailUrl(
bool $keepRatio = false,
bool $shouldCreate = false
): ?string {
// Do not attempt to resize SVGs, return the raw value always.
if ($this->isSvg($image)) {
return $this->storage->getPublicUrl($image->path);
}

// Do not resize GIF images where we're not cropping
if ($keepRatio && $this->isGif($image)) {
return $this->storage->getPublicUrl($image->path);
Expand Down Expand Up @@ -226,6 +231,14 @@ protected function isGif(Image $image): bool
return $this->getExtension($image) === 'gif';
}

/**
* Checks if the image is a svg. Returns true if it is, else false.
*/
protected function isSvg(Image $image): bool
{
return $this->getExtension($image) === 'svg';
}

/**
* Get the extension for the given image, normalised to lower-case.
*/
Expand Down
2 changes: 1 addition & 1 deletion app/Uploads/ImageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class ImageService
{
protected static array $supportedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
protected static array $supportedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg'];

public function __construct(
protected ImageStorage $storage,
Expand Down
1 change: 1 addition & 0 deletions app/Util/WebSafeMimeSniffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class WebSafeMimeSniffer
'image/webp',
'image/avif',
'image/heic',
'image/svg+xml',
'text/css',
'text/csv',
'text/javascript',
Expand Down

0 comments on commit 4dbfefd

Please sign in to comment.