Search code examples
phpimagemagickimagick

php Imagemagick jpg black background


I have a php script to create jpg thumbnail of pdf as follows;

<?php
$file ="test.pdf";
$im = new imagick(realpath($file).'[0]');
$im->setImageFormat("jpg");
$im->resizeImage(200,200,1,0);
// start buffering
ob_start();
$thumbnail = $im->getImageBlob();
$contents =  ob_get_contents();
ob_end_clean();
echo "<img src='data:image/jpg;base64,".base64_encode($thumbnail)."' />";
?>

But the resulting jpg have black background instead of white.. How can I fix this??


Solution

  • I solved it by;

    $im = new imagick(realpath($file).'[0]');
    $im->setCompression(Imagick::COMPRESSION_JPEG);
    $im->setCompressionQuality(100);
    $im->setImageFormat("jpeg");
    $im->writeImage("imagename.jpg");