From 069a0e4ae141671ae32b193205e156c05e077293 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Tue, 8 Jun 2021 03:22:01 -0600 Subject: [PATCH] Add WebP support --- .../Admin/AdminSettingsController.php | 4 + app/Http/Controllers/ComposeController.php | 1 + .../ImageOptimizePipeline/ImageUpdate.php | 110 +++++++++--------- app/Util/Media/Image.php | 3 +- resources/views/admin/settings/home.blade.php | 8 +- 5 files changed, 68 insertions(+), 58 deletions(-) diff --git a/app/Http/Controllers/Admin/AdminSettingsController.php b/app/Http/Controllers/Admin/AdminSettingsController.php index f6fce8a52..cdd68ea7f 100644 --- a/app/Http/Controllers/Admin/AdminSettingsController.php +++ b/app/Http/Controllers/Admin/AdminSettingsController.php @@ -26,6 +26,7 @@ trait AdminSettingsController $png = in_array('image/png', $types); $gif = in_array('image/gif', $types); $mp4 = in_array('video/mp4', $types); + $webp = in_array('image/webp', $types); // $system = [ // 'permissions' => is_writable(base_path('storage')) && is_writable(base_path('bootstrap')), @@ -39,6 +40,7 @@ trait AdminSettingsController 'png', 'gif', 'mp4', + 'webp', 'rules', 'cloud_storage', 'cloud_disk', @@ -60,6 +62,7 @@ trait AdminSettingsController 'type_png' => 'nullable', 'type_gif' => 'nullable', 'type_mp4' => 'nullable', + 'type_webp' => 'nullable', ]); if($request->filled('rule_delete')) { @@ -83,6 +86,7 @@ trait AdminSettingsController 'type_png' => 'image/png', 'type_gif' => 'image/gif', 'type_mp4' => 'video/mp4', + 'type_webp' => 'image/webp', ]; foreach ($mimes as $key => $value) { diff --git a/app/Http/Controllers/ComposeController.php b/app/Http/Controllers/ComposeController.php index f25822697..b20afab99 100644 --- a/app/Http/Controllers/ComposeController.php +++ b/app/Http/Controllers/ComposeController.php @@ -136,6 +136,7 @@ class ComposeController extends Controller switch ($media->mime) { case 'image/jpeg': case 'image/png': + case 'image/webp': ImageOptimize::dispatch($media); break; diff --git a/app/Jobs/ImageOptimizePipeline/ImageUpdate.php b/app/Jobs/ImageOptimizePipeline/ImageUpdate.php index 367396df8..132e9faf2 100644 --- a/app/Jobs/ImageOptimizePipeline/ImageUpdate.php +++ b/app/Jobs/ImageOptimizePipeline/ImageUpdate.php @@ -16,68 +16,68 @@ use App\Jobs\MediaPipeline\MediaStoragePipeline; class ImageUpdate implements ShouldQueue { - use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; + use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; - protected $media; + protected $media; - protected $protectedMimes = [ - 'image/jpeg', - 'image/png', - ]; + protected $protectedMimes = [ + 'image/jpeg', + 'image/png', + 'image/webp' + ]; - /** - * Delete the job if its models no longer exist. - * - * @var bool - */ - public $deleteWhenMissingModels = true; - - /** - * Create a new job instance. - * - * @return void - */ - public function __construct(Media $media) - { - $this->media = $media; - } + /** + * Delete the job if its models no longer exist. + * + * @var bool + */ + public $deleteWhenMissingModels = true; - /** - * Execute the job. - * - * @return void - */ - public function handle() - { - $media = $this->media; - if(!$media) { - return; - } - $path = storage_path('app/'.$media->media_path); - $thumb = storage_path('app/'.$media->thumbnail_path); - - if (!is_file($path)) { - return; - } + /** + * Create a new job instance. + * + * @return void + */ + public function __construct(Media $media) + { + $this->media = $media; + } - if (in_array($media->mime, $this->protectedMimes) == true) { - ImageOptimizer::optimize($thumb); - if(!$media->skip_optimize) { - ImageOptimizer::optimize($path); - } - } + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + $media = $this->media; + if(!$media) { + return; + } + $path = storage_path('app/'.$media->media_path); + $thumb = storage_path('app/'.$media->thumbnail_path); - if (!is_file($path) || !is_file($thumb)) { - return; - } + if (!is_file($path)) { + return; + } - $photo_size = filesize($path); - $thumb_size = filesize($thumb); - $total = ($photo_size + $thumb_size); - $media->size = $total; - $media->save(); + if (in_array($media->mime, $this->protectedMimes) == true) { + ImageOptimizer::optimize($thumb); + if(!$media->skip_optimize) { + ImageOptimizer::optimize($path); + } + } - MediaStoragePipeline::dispatch($media); + if (!is_file($path) || !is_file($thumb)) { + return; + } - } + $photo_size = filesize($path); + $thumb_size = filesize($thumb); + $total = ($photo_size + $thumb_size); + $media->size = $total; + $media->save(); + + MediaStoragePipeline::dispatch($media); + } } diff --git a/app/Util/Media/Image.php b/app/Util/Media/Image.php index 2f4414de3..cbc1efe67 100644 --- a/app/Util/Media/Image.php +++ b/app/Util/Media/Image.php @@ -15,7 +15,8 @@ class Image public $orientation; public $acceptedMimes = [ 'image/png', - 'image/jpeg' + 'image/jpeg', + 'image/webp' ]; public function __construct() diff --git a/resources/views/admin/settings/home.blade.php b/resources/views/admin/settings/home.blade.php index b69ff0c0b..14bbf636c 100644 --- a/resources/views/admin/settings/home.blade.php +++ b/resources/views/admin/settings/home.blade.php @@ -238,8 +238,12 @@
- - + + +
+
+ +

Allowed media types.