Skip to content

Commit 4dbfefd

Browse files
committed
Add .svg image file support
* 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.
1 parent 786a434 commit 4dbfefd

File tree

5 files changed

+17
-3
lines changed

5 files changed

+17
-3
lines changed

app/Exports/ZipExports/Models/ZipExportImage.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function metadataOnly(): void
3232

3333
public static function validate(ZipValidationHelper $context, array $data): array
3434
{
35-
$acceptedImageTypes = ['image/png', 'image/jpeg', 'image/gif', 'image/webp'];
35+
$acceptedImageTypes = ['image/png', 'image/jpeg', 'image/gif', 'image/webp', 'image/svg+xml'];
3636
$rules = [
3737
'id' => ['nullable', 'int', $context->uniqueIdRule('image')],
3838
'name' => ['required', 'string', 'min:1'],

app/Http/Controller.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ protected function logActivity(string $type, string|Loggable $detail = ''): void
163163
*/
164164
protected function getImageValidationRules(): array
165165
{
166-
return ['image_extension', 'mimes:jpeg,png,gif,webp', 'max:' . (config('app.upload_limit') * 1000)];
166+
return ['image_extension', 'mimes:jpeg,png,gif,webp,svg', 'max:' . (config('app.upload_limit') * 1000)];
167167
}
168168

169169
/**

app/Uploads/ImageResizer.php

+13
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ public function resizeToThumbnailUrl(
6666
bool $keepRatio = false,
6767
bool $shouldCreate = false
6868
): ?string {
69+
// Do not attempt to resize SVGs, return the raw value always.
70+
if ($this->isSvg($image)) {
71+
return $this->storage->getPublicUrl($image->path);
72+
}
73+
6974
// Do not resize GIF images where we're not cropping
7075
if ($keepRatio && $this->isGif($image)) {
7176
return $this->storage->getPublicUrl($image->path);
@@ -226,6 +231,14 @@ protected function isGif(Image $image): bool
226231
return $this->getExtension($image) === 'gif';
227232
}
228233

234+
/**
235+
* Checks if the image is a svg. Returns true if it is, else false.
236+
*/
237+
protected function isSvg(Image $image): bool
238+
{
239+
return $this->getExtension($image) === 'svg';
240+
}
241+
229242
/**
230243
* Get the extension for the given image, normalised to lower-case.
231244
*/

app/Uploads/ImageService.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
class ImageService
1515
{
16-
protected static array $supportedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
16+
protected static array $supportedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg'];
1717

1818
public function __construct(
1919
protected ImageStorage $storage,

app/Util/WebSafeMimeSniffer.php

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class WebSafeMimeSniffer
3333
'image/webp',
3434
'image/avif',
3535
'image/heic',
36+
'image/svg+xml',
3637
'text/css',
3738
'text/csv',
3839
'text/javascript',

0 commit comments

Comments
 (0)