1
votes

I'm using Imagick to generate JPG thumbnails for PDFs files but some of them generate with black areas (http://i.imgur.com/fKBncKw.jpg) – I'm assuming it's caused by transparency in the PDFs but can anything be done about it?

Code I'm using to generate these:

$imagick = new Imagick($filename);
$imagick->setIteratorIndex(0);
$imagick->setImageFormat('jpg');
return $imagick->getImageBlob();

Is there a way to flatten the PDF and/or add a white background so that the black areas don't appear?

3
did you find a fix for this? same problem here.. - silk

3 Answers

3
votes

Here is a solution which will only work for PNG to JPG. This code adds a white background to the transparent areas in PNG and converts it to JPG.

What it does?

This code takes all PNG images from a folder, converts them to JPG's with white backgrounds and saves them in another folder.

<?php
ini_set('max_execution_time', 3000); 

$dir = 'transparent/';
$arr = scandir($dir);

for($i=0;$i<count($arr);$i++)
{
    if($i==0 || $i==1)
    {
    }
    else{

    $input_file = "transparent/".$arr[$i];
    $output_file = "White/".str_replace('.png','.jpg',$arr[$i]);

    $input = imagecreatefrompng($input_file);
    list($width, $height) = getimagesize($input_file);
    $output = imagecreatetruecolor($width, $height);
    $white = imagecolorallocate($output,  255, 255, 255);
    imagefilledrectangle($output, 0, 0, $width, $height, $white);
    imagecopy($output, $input, 0, 0, 0, 0, $width, $height);
    imagejpeg($output, $output_file);

    }
}
?>

Its Image Processing and GD in PHP. PHP Manual

Hope it helps, you can change it as you want.

1
votes

Try this code: Imagick::setCompressionQuality

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

Alternative solution: this may help:

<?php

//Note that the function returns an Imagick object and does not modify the existing object. Below is my code for converting a PNG with transparency into a JPG with a background color. This code illustrates the difference.


$im = new Imagick($filename);
$im->setImageBackgroundColor('white');

$im->flattenImages(); // This does not do anything.
$im = $im->flattenImages(); // Use this instead.

$im->setImageFormat('jpg');
$im->writeImage('image.jpg');

?>
0
votes

use like this

class ImageConvertorLib{

private $CI;

/**
 * loading codeIgniter instance
 */
public function __construct(){
    $this->CI =& get_instance();
}

public function pdfToJpg($param){
    $filename = $param['filename'];
    $image_name = $param['image_name'];
    $path = $param['path'];
    $db_path = $param['db_path'];
    $im = new Imagick();
    $im->setResolution(220,220);
    $im->readimage($filename."[0]");
    $im->setImageFormat('jpeg');
    $im->setImageBackgroundColor('#ffffff');
    $im->flattenImages();
    $image_name = $image_name.".jpg";//"save_as_name.jpg";
    $imageprops = $im->getImageGeometry();
    /*if ($imageprops['width'] <= 175 && $imageprops['height'] <= 300) {
     // don't upscale
     } else {
     $im->resizeImage(175,300, imagick::FILTER_LANCZOS, 0.9, true);
     }*/

    $im->writeImage($path.$image_name);
    if($im){
        $Img = array();
        $Img['status'] = 1;
        $Img['image'] = ($db_path.$image_name);
        return $Img;
    }
    $im->clear();
    $im->destroy();
}

}