From bc4215a1b5a3302666cf84f8865d6107b7096b43 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 8 Nov 2023 19:53:38 +1100 Subject: [PATCH] Add Imagick extension to improve image compression --- app/Helpers/Document/WithTypeHelpers.php | 1 + app/Models/Document.php | 29 ++++++++++++++++++++++++ app/Services/Client/ClientService.php | 4 ++-- app/Utils/HtmlEngine.php | 3 ++- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/app/Helpers/Document/WithTypeHelpers.php b/app/Helpers/Document/WithTypeHelpers.php index 32fb4950a3..9f2776a16a 100644 --- a/app/Helpers/Document/WithTypeHelpers.php +++ b/app/Helpers/Document/WithTypeHelpers.php @@ -27,4 +27,5 @@ trait WithTypeHelpers return false; } + } diff --git a/app/Models/Document.php b/app/Models/Document.php index 476c0f605c..3c73e40d29 100644 --- a/app/Models/Document.php +++ b/app/Models/Document.php @@ -205,4 +205,33 @@ class Document extends BaseModel { return ctrans('texts.document'); } + + public function compress(): mixed + { + + $image = $this->getFile(); + $catch_image = $image; + + if(extension_loaded('imagick')) + return $catch_image; + + try { + $file = base64_encode($image); + + $img = new \Imagick(); + $img->readImageBlob($file); + $img->setImageCompression(true); + $img->setImageCompressionQuality(50); + + return $img->getImageBlob(); + + } + catch(\Exception $e){ + + nlog($e->getMessage()); + return $catch_image; + } + + } + } diff --git a/app/Services/Client/ClientService.php b/app/Services/Client/ClientService.php index 410b388841..21e846438f 100644 --- a/app/Services/Client/ClientService.php +++ b/app/Services/Client/ClientService.php @@ -64,9 +64,9 @@ class ClientService /** * Seeing too many race conditions under heavy load here. - * @deprecated + * * @param float $amount - * @return void + * @return ClientService */ public function updateBalance(float $amount) { diff --git a/app/Utils/HtmlEngine.php b/app/Utils/HtmlEngine.php index e896780c1b..7d0923330c 100644 --- a/app/Utils/HtmlEngine.php +++ b/app/Utils/HtmlEngine.php @@ -1038,6 +1038,7 @@ html { $container = $dom->createElement('div'); $container->setAttribute('style', 'display:grid; grid-auto-flow: row; grid-template-columns: repeat(2, 1fr); grid-template-rows: repeat(2, 1fr);justify-items: center;'); + /** @var \App\Models\Document $document */ foreach ($this->entity->documents()->where('is_public', true)->get() as $document) { if (!$document->isImage()) { continue; @@ -1045,7 +1046,7 @@ html { $image = $dom->createElement('img'); - $image->setAttribute('src', "data:image/png;base64,".base64_encode($document->getFile())); + $image->setAttribute('src', "data:image/png;base64,".base64_encode($document->compress())); $image->setAttribute('style', 'max-width: 50%; margin-top: 20px;'); $container->appendChild($image);