jamesMcKey
jamesMcKey

Reputation: 491

Using Imagick-resizeImage to create thumbnails creates dark images - PHP

I am able to successfully resize (to the proper resized dimensions) an image that I have created from one page of a pdf document. However, I do not understand why the result is a dark resized image with a patch of white. Please, can someone advise?

PHP code:

 // Create image from first page of pdf document
 $im = new imagick('1Mpublic.pdf[0]');
 $im->setImageFormat('jpg');

 $imageHeight =  $im -> getImageHeight();
 $imageWidth = $im -> getImageWidth();

 $desiredImgWidth = 200;
 $desiredImgHeight = resizedImageHeight($imageWidth, $imageHeight, 
 $desiredImgWidth);

 $im -> resizeImage($desiredImgWidth, $desiredImgHeight, 
 imagick::STYLE_NORMAL, 1);


 // Save image
 $page = '1';
 $saveToFolder = 'thumbnailFolder';
 $fileName = 'thanhThumbNail_'.$page.'.jpg';
 $saveImgToPath = $saveToFolder.'/'.$fileName;
 $result = file_put_contents($saveImgToPath, $im);





 function resizedImageHeight($imgWidth, $imgHeight, $desiredImgWidth){

     $quoient = $imgWidth/$imgHeight;
     $height = $desiredImgWidth/$quoient;

     return $height;
 } 

Resulting thumbnail image:

enter image description here

Link to original PDF can be found here:

https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4905263/pdf/ksgt-06-04-1091539.pdf

Upvotes: 1

Views: 450

Answers (2)

fmw42
fmw42

Reputation: 53081

Your PDF has transparency. JPG does not support transparency and shows black where the PDF was transparent. Just turn the transparency off. In Imagemagick command line:

convert -density 300 ksgt-06-04-1091539.pdf[0] -alpha off result.jpg

enter image description here

See setImageAlphaChannel at https://www.php.net/manual/en/imagick.setimagealphachannel.php

Upvotes: 1

emcconville
emcconville

Reputation: 24419

Looks like the background color is not defined. You need to set the background color before reading the PDF document.

// Create image from first page of pdf document
$im = new imagick();
$im->setBackgroundColor('WHITE');
$im->readImage('1Mpublic.pdf[0]');
$im->setImageFormat('jpg');

Upvotes: 1

Related Questions