1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

Add Imagick extension to improve image compression

This commit is contained in:
David Bomba 2023-11-08 19:53:38 +11:00
parent f66d9e3447
commit bc4215a1b5
4 changed files with 34 additions and 3 deletions

View File

@ -27,4 +27,5 @@ trait WithTypeHelpers
return false; return false;
} }
} }

View File

@ -205,4 +205,33 @@ class Document extends BaseModel
{ {
return ctrans('texts.document'); 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;
}
}
} }

View File

@ -64,9 +64,9 @@ class ClientService
/** /**
* Seeing too many race conditions under heavy load here. * Seeing too many race conditions under heavy load here.
* @deprecated *
* @param float $amount * @param float $amount
* @return void * @return ClientService
*/ */
public function updateBalance(float $amount) public function updateBalance(float $amount)
{ {

View File

@ -1038,6 +1038,7 @@ html {
$container = $dom->createElement('div'); $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;'); $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) { foreach ($this->entity->documents()->where('is_public', true)->get() as $document) {
if (!$document->isImage()) { if (!$document->isImage()) {
continue; continue;
@ -1045,7 +1046,7 @@ html {
$image = $dom->createElement('img'); $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;'); $image->setAttribute('style', 'max-width: 50%; margin-top: 20px;');
$container->appendChild($image); $container->appendChild($image);