Home » Php » Trying to scale an image using Imagick onto A4 page and convert it into a PDF using FPDF – PHP

Trying to scale an image using Imagick onto A4 page and convert it into a PDF using FPDF – PHP

Posted by: admin October 18, 2018 Leave a comment

Questions:

I have an image that I would like to scale (but keeping the aspect ratio) and occupy the entire PDF page – at least as much as possible – as generated by FPDF in portrait mode. I would also like to center the image in the page. To do this I need to know the dimensions of the image and page dimensions generated.

I am very confused about the units used. I would like to resize my .jpg image using imagemagick.
FPDF asks for the width and height in ‘user unit’ (FPDF-image), and Imagick returns the dimensions of an image in an undetermined unit(Imagick::getImageGeometry)

For example I get the following back when i execute the PHP code below:

 $pdf = new FPDF();
 $filename = 'thumbnailFolder/page_2.pdf';
 $pdf -> Image('myFilePath/myImage.jpg');

 echo 'pageWidth: '.$pdf -> GetPageWidth().'<br>';
 echo 'pageHeight: '.$pdf -> GetPageHeight().'<br>';



 // Get image geometry
 $sourcePathOfImage = 'thumbnailFolder/labpage1.jpg';;
 $im = new imagick($sourcePathOfImage);
 $geo = $im->getImageGeometry();
 $srcImgWidth = $geo['width'];
 $srcImgHeight = $geo['height'];

 echo 'imgWidth: '.$srcImgWidth.'<br>';
 echo 'imgHeight: '.$srcImgHeight.'<br>';

output:

 pageWidth: 210.00155555556
 pageHeight: 297.00008333333
 imgWidth: 914
 imgHeight: 1304

My question is, how do i make sense of the dimensions of the units involved so that I can make the correct calculations for resizing and subsequent placement of the image onto the A4 page?

Answers: