Skip to content

A free open source IT asset/license management system

Fixed #8147 - allow webp image format for public file uploads

snipe4ac15daee7a3
diff --git a/app/Http/Requests/ImageUploadRequest.php b/app/Http/Requests/ImageUploadRequest.php
index 2b43f9194d..f7b1a05256 100644
--- a/app/Http/Requests/ImageUploadRequest.php
+++ b/app/Http/Requests/ImageUploadRequest.php
@@ -27,7 +27,7 @@ class ImageUploadRequest extends Request
public function rules()
{
return [
- 'image' => 'mimes:png,gif,jpg,jpeg,svg,bmp,svg+xml',
+ 'image' => 'mimes:png,gif,jpg,jpeg,svg,bmp,svg+xml,webp',
'avatar' => 'mimes:png,gif,jpg,jpeg,svg,bmp,svg+xml',
];
}
@@ -91,8 +91,8 @@ class ImageUploadRequest extends Request
\Log::info('File name will be: '.$file_name);
\Log::debug('File extension is: '. $ext);
- if ($image->getClientOriginalExtension()!=='svg') {
- \Log::debug('Not an SVG - resize');
+ if (($image->getClientOriginalExtension()!=='webp') && ($image->getClientOriginalExtension()!=='svg')) {
+ \Log::debug('Not an SVG or webp - resize');
\Log::debug('Trying to upload to: '.$path.'/'.$file_name);
$upload = Image::make($image->getRealPath())->resize(null, $w, function ($constraint) {
$constraint->aspectRatio();
@@ -102,20 +102,27 @@ class ImageUploadRequest extends Request
// This requires a string instead of an object, so we use ($string)
Storage::disk('public')->put($path.'/'.$file_name, (string)$upload->encode());
-
- // If the file is an SVG, we need to clean it and NOT encode it
} else {
- \Log::debug('This is an SVG');
- $sanitizer = new Sanitizer();
- $dirtySVG = file_get_contents($image->getRealPath());
- $cleanSVG = $sanitizer->sanitize($dirtySVG);
-
- try {
- \Log::debug('Trying to upload to: '.$path.'/'.$file_name);
- Storage::disk('public')->put($path.'/'.$file_name, $cleanSVG);
- } catch (\Exception $e) {
- \Log::debug('Upload no workie :( ');
- \Log::debug($e);
+ // If the file is a webp, we need to just move it since webp support
+ // needs to be compiled into gd for resizing to be available
+ if ($image->getClientOriginalExtension()=='webp') {
+ \Log::debug('This is a webp, just move it');
+ Storage::disk('public')->put($path.'/'.$file_name, file_get_contents($image));
+ // If the file is an SVG, we need to clean it and NOT encode it
+ } else {
+
+ \Log::debug('This is an SVG');
+ $sanitizer = new Sanitizer();
+ $dirtySVG = file_get_contents($image->getRealPath());
+ $cleanSVG = $sanitizer->sanitize($dirtySVG);
+
+ try {
+ \Log::debug('Trying to upload to: '.$path.'/'.$file_name);
+ Storage::disk('public')->put($path.'/'.$file_name, $cleanSVG);
+ } catch (\Exception $e) {
+ \Log::debug('Upload no workie :( ');
+ \Log::debug($e);
+ }
}
}
diff --git a/resources/lang/en/general.php b/resources/lang/en/general.php
index cf936392e4..ed624b9ccf 100644
--- a/resources/lang/en/general.php
+++ b/resources/lang/en/general.php
@@ -112,7 +112,7 @@
'image' => 'Image',
'image_delete' => 'Delete Image',
'image_upload' => 'Upload Image',
- 'image_filetypes_help' => 'Accepted filetypes are jpg, png, gif, and svg. Max upload size allowed is :size.',
+ 'image_filetypes_help' => 'Accepted filetypes are jpg, webp, png, gif, and svg. Max upload size allowed is :size.',
'import' => 'Import',
'importing' => 'Importing',
'importing_help' => 'You can import assets, accessories, licenses, components, consumables, and users via CSV file. <br><br>The CSV should be comma-delimited and formatted with headers that match the ones in the <a href="https://snipe-it.readme.io/docs/importing" target="_new">sample CSVs in the documentation</a>.',
diff --git a/resources/views/partials/forms/edit/image-upload.blade.php b/resources/views/partials/forms/edit/image-upload.blade.php
index 1676ce669e..78fe48ff8f 100644
--- a/resources/views/partials/forms/edit/image-upload.blade.php
+++ b/resources/views/partials/forms/edit/image-upload.blade.php
@@ -6,7 +6,7 @@
<label class="btn btn-default" aria-hidden="true">
{{ trans('button.select_file') }}
- <input type="file" name="{{ (isset($fieldname) ? $fieldname : 'image') }}" class="js-uploadFile" id="uploadFile" data-maxsize="{{ \App\Helpers\Helper::file_upload_max_size() }}" accept="image/gif,image/jpeg,image/png,image/svg" style="display:none; max-width: 90%" aria-label="image" aria-hidden="true">
+ <input type="file" name="{{ (isset($fieldname) ? $fieldname : 'image') }}" class="js-uploadFile" id="uploadFile" data-maxsize="{{ \App\Helpers\Helper::file_upload_max_size() }}" accept="image/gif,image/jpeg,image/webp,image/png,image/svg" style="display:none; max-width: 90%" aria-label="image" aria-hidden="true">
</label>
<span class='label label-default' id="uploadFile-info"></span>