How to create a thumbnail of a (remote) PDF with PHP

I have had a problem recently where I have been developing a website for a client, and they want their uploaded pdfs to be thumbnailed. This was a problem because their host didn’t have imagemagic and ghostscript installed.

However the nice people at 5quidHost (my hosts) do have it installed.

This is the code I have used:

<?php
putenv('PATH=/usr/local/bin:' . getenv('PATH'));
/*
 * I have chosen to use the $_GET variable so the remote
 * site can tell this script which file to thumbnail.
 * If you use this on the same server then you could get
 * rid of this and give it the filename straight from the
 * upload
 */
if (!isset($_GET['filename'])) {
	die("Why hello there");
}
if (isset($_GET['text'])) {
	$textToWrite = $_GET['text'];
} else {
	$textToWrite = FALSE;
}

$url = "http://remotesite.com/" . $_GET['filename'];
$localUrl = './up/'.$_GET['filename'];
/* Here we download the pdf and save it locally */
$pdf = file_get_contents($url);

$fp  = fopen($localUrl, 'w+') or die('Could not create the file');
fputs($fp, $pdf) or die('Could not write to the file');
fclose($fp);
unset($pdf);

$im = new Imagick($localUrl. '[0]');
$im->setImageFormat( "png" );

// If 0 is provided as a width or height parameter,
// aspect ratio is maintained#
$im->thumbnailImage(100, 0);

/* If there is an annotation, draw it */
if ($textToWrite) {
	$draw = new ImagickDraw();
	$text = new ImagickDraw();
	$draw->setFillColor('white');
	$draw->setStrokeColor( new ImagickPixel( 'red' ) );
	$draw->rectangle( 4, 100, 96, 127 );    // Draw the rectangle
	$text->setFillColor('red');

	/* Font properties */
	$text->setFont('Bookman-DemiItalic');
	$text->setFontSize( 20 );

	/* Create text */
	$im->drawImage( $draw );
	$texttowrite = stripslashes($textToWrite);
	$im->annotateImage($text, 10, 120, 0, $texttowrite);
}

/* Display the image */
header( "Content-Type: image/png" );
echo $im;
$im->clear();
$im->destroy();
unlink($localUrl); //Delete the downloaded pdf
?>

Basically what this does is it downloads the pdf from the remote website, saves it in a temporary folder (‘up’ in my case), then makes a new Imagick object using that pdf. You could just change $localUrl to the remote url, but I have found that it makes a thumbnail of the last page if you do that.

Line 2 probably won’t be necessary on other peoples servers but because my hosts had imagemagick installed in /usr/local/bin, which wasn’t in the PATH env for some reason, I had to have it.

The [0] on line 29 is me telling it to use the first page. If you put [0] after the url it will treat it as if it is part of the url, and thus you will get a 404 error.

If you have this script on the same server then instead of line 57 you could use $im->writeImage($filename);

which would then write the image to the specified file name.

On the remote server I have a little function that is called once the upload is completed:

private function thumbnail($filename,$text) {
/* This is the url of the script above! */
$url = 'http://otherhost/thumbnail.php?filename=' . $filename;
if ($text != null) {
$url .= '&text='. $text;
}
$thumbnail = $this->_thumbnails . $filename . '.png';
$gif = file_get_contents($url);
$fp  = fopen($thumbnail, 'w+') or die('Could not create the file');

fputs($fp, $gif) or die('Could not write to the file');
fclose($fp);
unset($gif);
}

I think that is pretty self explanatory.

The one problem I am having at the moment is that it refuses to set a background colour for the image. This means if you use jpg as the image format you will get a black background. By using png format at least I get a transparent background. But maybe its just the pdfs I’m using.

Well thats about it for now. If a human does find this and finds it useful or can’t get it to work leave a comment below and I’ll get back to you 😀


2 thoughts on “How to create a thumbnail of a (remote) PDF with PHP

Jamil

I’m a little lost by this paragraph:

“The [0] on line 23 is me telling it to use the first page. If you put [0] after the url it will treat it as if it is part of the url, and thus you will get a 404 error.”

First, because line 23 is empty but also because where you use [0], you are appending it to the $localUrl, so isn’t that part of the URL?

Reply

    Christopher Rogers

    Sorry it should say line 29. The $localUrl is the file path of the downloaded file. EG /tmp/myfile.pdf
    Thus this would become /tmp/myfile.pdf[0]

    Hope that helps 🙂

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *