Saturday, January 12, 2008

PDF Thumbnails through PHP, ImageMagick, Ghostscript - the Commandline solution

I had run into all sorts of issues with trying to deploy the imagemagick library in php. I was able to get it working fine on my xamp installations, but when I would try to get it working on IIS, I would run into issues.

GD worked fine, but unfortunately we deal with many complex image types. Some of them are only accessible to imagemagick such as PDFs.

And even worse, trying to get it running on an already live server was proving to be extremely difficult as I had to mess with the php.ini and reboot iis and perhaps the machine just to test certain changes - obviously this is not ideal with a server that is already hosting traffic.

After much frustration I came up with the idea that I could just remote php and ImageMagick via commandline. It has already prooved to be very powerful for me. This method might have a few drawbacks in terms of processing power, but its a lot better than remoting Photoshop or similar program.



Ingredients I used for Thumbnailing - you may try and substitute to see if it works for you.
Win2k3 Server, IIS, PHP5x, ImageMagick (any flavour i think), Ghostscript

Hopefully you have remote desktop/admin access otherwise this might not work for you... but maybe you can convince your provider to do this for you?

1) Download and install everything, you should already have PHP installed and working.

2) Install Imagemagick, Install Ghostscript which is necessary to render the PDF thumbnails.

3) but you will need to change a few settings.

one key one is changing permissions on IUSER_XXXXX on %system path%/system32/cmd.exe

you have to actually right click on the cmd.exe file and set the permissions on this file specifically.

4) make sure you have executable permissions in IIS for the website

5) i created a temp directory for easy testing such as
c:\temp\

6) I gave this directory full permissions for testing and also shared it on the network so it could be modified... this is temporary to see if mechanism works.

7) I then created this script... I will leave all the commenting so you can see more or less everything I tried and went through to get it to work...



/ < ? PHP

// MZs IMAGE MAGICK PDF THUMBNAILING MECHANISM
// REQUIRED INGREDIENTS :
// IMAGE MAGICK
// GHOSTSCRIPT
// !!!! NOTE TO RUN EXEC() ON IIS6.win2k6 I had to enable permissions on system32/cmd.exe for INETUSR_XXXXX
//
///
//

function CreateThumbnail($sourcefile, $destfile, $percentage=25 ) {
//$percentage = "25%";
//create command to send out
//exec(,$output,$return);
$command = "convert.exe -colorspace cmyk ".$sourcefile." -thumbnail ".$percentage."%x".$percentage."% ".$destfile;
//$command = 'start /B "convert" "convert.exe -colorspace cmyk '.$sourcefile.' -thumbnail 25%x25% '.$destfile.'"';
//$command = "dir";
echo $command;

$ret = exec($command, $output);
//echo $ret;
//echo sizeof($output[0]);
//echo $output[1].'
';
//echo $output[2].'
';
//echo 'the output'.$output.'
';

return true;
}


$sourcefile = "e:\\test\\1.pdf";
$destfile= "e:\\test\\1.pdf";


if ( $_GET['sourcefile'] ) {
$sourcefile = $_GET['sourcefile'];
}

if ($_GET['destfile'] ) {
$destfile = $_GET['destfile'];
} else {
$destfile = 'thumb-'.$sourcefile;
}

if ($_GET['percentage'] ) {
$percentage = $_GET['percentage'];
} else {
$percentage = 40;
}

CreateThumbnail($sourcefile,$destfile, $percentage) ;


?>


I still have a lot more to write, so I'll keep upating the blog as I make the script better.

you can pass in variables into the script such as thumbnail.php?sourcefile=sourcefile.pdf&destfile=destfile.jpg&percentage=25
and it creates a jpg thumbnail 25% the size of the

Labels: , , , ,