From 314134104393453efc623dd6195f82035ad51259 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ula=C5=9F=20=C3=96zdemir?= Date: Sun, 15 Sep 2024 21:45:25 +0300 Subject: [PATCH] optimizeImage function modified to handle detect and handle multi-frame visuals --- app/Livewire/Questions/Create.php | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/app/Livewire/Questions/Create.php b/app/Livewire/Questions/Create.php index d454c05b9..9c527e3ee 100644 --- a/app/Livewire/Questions/Create.php +++ b/app/Livewire/Questions/Create.php @@ -294,12 +294,23 @@ public function optimizeImage(string $path): void $imagePath = Storage::disk('public')->path($path); $imagick = new Imagick($imagePath); - $imagick->resizeImage(1000, 1000, Imagick::FILTER_LANCZOS, 1, true); - - $imagick->stripImage(); - - $imagick->setImageCompressionQuality(80); - $imagick->writeImage($imagePath); + if ($imagick->getNumberImages() > 1) { + $imagick = $imagick->coalesceImages(); + + foreach ($imagick as $frame) { + $frame->resizeImage(1000, 1000, Imagick::FILTER_LANCZOS, 1, true); + $frame->stripImage(); + $frame->setImageCompressionQuality(80); + } + + $imagick = $imagick->deconstructImages(); + $imagick->writeImages($imagePath, true); + } else { + $imagick->resizeImage(1000, 1000, Imagick::FILTER_LANCZOS, 1, true); + $imagick->stripImage(); + $imagick->setImageCompressionQuality(80); + $imagick->writeImage($imagePath); + } $imagick->clear(); $imagick->destroy();