Page 1 of 1

Imagick and Lise

Posted: Wed Mar 02, 2022 10:01 am
by pierrepercee
Hello,

I use imagick to generate a thumbnail based on a pdf document. Thanks to Velden it work Like a charm.
But to simple perhaps...
The beginning of my UDT

Code: Select all

$obj = $params['item_object'];
$fd = $obj->fielddefs['envoipdf'];
$fd =  $fd->GetImagePath(true) . '/' . $fd->value;
$ali = $obj->alias;
$im = new Imagick();
$im->setResolution(72,72);
$im->readImage($fd)
For a single page pdf it works well. The thumbnail is well created.
The returned path is

Code: Select all

https://www.maropclac.com/uploads/gabkio/placa/visuel-exercice-003.pdf
But for multiples pages pdf document, if you want to use only the first page to generate your image you have to use :

Code: Select all

$im->readImage($fd."[0]")
Bad fortune, it do not work....It returns :

Code: Select all

Fatal error: Uncaught ImagickException: Imagick::readImage(): HTTP request failed! HTTP/1.1 404 Not Found in /home/tukbix92dqp/testserv/lib/classes/class.usertagoperations.inc.php(305) : eval()'d code:7 Stack trace: #0 /home/tukbix92dqp/testserv/lib/classes/class.usertagoperations.inc.php(305) : eval()'d code(7): Imagick->readImage('https://www.maro...') #1 /home/tukbix92dqp/testserv/lib/classes/class.usertagoperations.inc.php(285): cms_user_tag_slthumb(Array, Object(Smarty_CMS)) #2 /home/tukbix92dqp/testserv/lib/classes/class.Events.php(126): UserTagOperations->CallUserTag('slthumb', Array) #3 /home/tukbix92dqp/testserv/modules/LISE/lib/class.LISEItemOperations.php(232): Events::SendEvent('LISEjourmaro', 'PostItemSave', Array) #4 /home/tukbix92dqp/testserv/modules/LISE/LISE.module.php(631): LISEItemOperations::Save(Object(LISEjourmaro), Object(LISEItem)) #5 /home/tukbix92dqp/testserv/modules/LISE/framework/action.admin_edititem.php(218): LISE->SaveItem(Object(LISEItem)) #6 /home/tukbix92dqp/testserv/modules/LISE/l in /home/tukbix92dqp/testserv/lib/classes/class.usertagoperations.inc.php(305) : eval()'d code on line 7
I have tried differents solutions to retrieve the path but... I have posted on stackoverflow because I thought Imagick was the guilty, no more answer at all...

A little help... :)

Re: Imagick and Lise

Posted: Wed Mar 02, 2022 2:45 pm
by velden
I'd start to try:

Code: Select all

$fd =  $fd->GetImagePath() . '/' . $fd->value;
(GetImagePath(true) -> returns a url but I expect a normal file path works better.

Re: Imagick and Lise

Posted: Thu Mar 03, 2022 8:59 am
by pierrepercee
Hello Velden,

Thanks, you are right ! No more error message now. But the thumbnail is not created at all (same thing for one page pdf).
I know that there's a ImagickException to handle error but I dont know how to use it in my UDT.

Here's my complete UDT

Code: Select all

$obj = $params['item_object'];
$fd = $obj->fielddefs['envoipdf'];
$fd =  $fd->GetImagePath() . '/' . $fd->value;
$okali = $obj->alias;
$im = new Imagick();
$im->setResolution(72,72);
$im->readImage($fd."[0]");
$im->setBackgroundColor('white');
$compression_type = Imagick::COMPRESSION_JPEG;
$im->setImageCompression($compression_type); 
$im->setImageCompressionQuality(90);
$im->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
$im->setImageAlphaChannel(Imagick::VIRTUALPIXELMETHOD_WHITE);
$im->setIteratorIndex(0);
$debtamps = bin2hex(random_bytes(5));
$nomabsortie = "slide-".$debtamps;
$nomsortie=uniqid($nomabsortie).".jpg";
$im->setGravity(\Imagick::GRAVITY_CENTER);
$geo=$im->getImageGeometry();
$sizex=$geo['width'];
$sizey=$geo['height'];
$ratio=$sizex/$sizey;
$kallet=$_SERVER['DOCUMENT_ROOT']."/uploads/slider/thumbslide/".$nomsortie;
$kalletb = "uploads/slider/thumbslide/".$nomsortie;
if ($sizex>600){
if ($ratio>1) {$im->scaleImage(405,510,true);$im->cropThumbnailImage(270,340);$im->writeImage($kallet);}
else {$im->scaleImage(270,340,true);$im->writeImage($kallet);}}
else {echo "<span class='attentionrecadrage'> Image trop petite pour générer une slide de bonne qualité. La largeur minimum de l'image doit être de<strong> 640 pixels</strong>. La largeur actuelle de votre image est de : ".$sizex." pixels. Merci d'utiliser un fichier pdf de dimensions suffisante.</span>";}
$gCms = CmsApp::get_instance();
$db = cmsms()->GetDb();
$querya = "SELECT item_id FROM cms_module_lisejourmaro_item WHERE alias='$okali'";
$dbx = $db->getOne($querya);
$query = "UPDATE cms_module_journaro_fieldval SET value='$kalletb' WHERE item_id='$dbx' AND fielddef_id=9";
$db->Execute($query);

Re: Imagick and Lise

Posted: Mon Mar 07, 2022 1:06 pm
by pierrepercee
OK I Got it !

Thousands test after...

I don't know how Imagick calculates the size but

Code: Select all

if ($sizex>600)

is not well working. Generated PDF are A4, resolution 220 dpi...But I set the resolution to 72dpi. 8.2inch(a4) x72=595. Ouch, I'm a perfect idiot....

Thanks to

Code: Select all

$config['debug'] = false;
Thanks to Velden and sorry for this bad joke !