Flipkart

Thursday, April 8, 2010

PHP : Convert PDF Image file to JPEG Using Image magick

For this you need to  insatll Ghostscript first, you can download Ghostscript from here.

< ?php

define('THUMB_WIDTH', 150);
define('THUMB_HEIGHT', 212);


function makeThumbnail($in, $out) {
    $width = THUMB_WIDTH;
    $height = THUMB_HEIGHT;
    $identify = shell_exec("identify -verbose ".$in);
                $matches = array();
                $pattern = '/Geometry: ([0-9]{1,})x([0-9]{1,})/';
        $result = preg_match($pattern, $identify, $matches);
        if( $result && count($matches) ) {
            $h = $matches[1];
            $w =  $matches[2];

        }

    $thumbRatio = $width/$height;
    $inRatio = $w/$h;
    $isLandscape = $inRatio > $thumbRatio;

    $size = ($isLandscape ? '1000x'.$height : $width.'x1000');
    $xoff = ($isLandscape ? floor((($inRatio*$height)-$width)/2) : 0);
    $command = system("convert $in -resize $size -crop {$width}x{$height}+{$xoff}+0 ".
        "-colorspace RGB -strip -quality 90 $out");

//     exec($command);
//     echo $command;
}
$file = 'scan0001.pdf';
$out = 'scan0001_thumb.jpg';
makeThumbnail($file,$out);

? >

No comments:

Post a Comment