3

I'm using a generic PHP based CMS, i wanted to create a script which read the pdf created a thumbnail and cached it. There were lots of different answers, and i did have a fair few problems with different versions of imagick, but this is script which worked for me.

some people might find it useful and maybe someone could advice me if it is optimised?

<?php

$loc = *the file location*;
$pdf = *the file name*;
$format = "jpg";
$dest = "$loc$pdf.$format";

if (file_exists($dest))
        {
        $im = new imagick();
        $im->readImage($dest);
        header( "Content-Type: image/jpg" );
        echo $im;
        exit;
        }
else
    {
        $im = new imagick($loc.$pdf.'[0]');
        $im->setImageFormat($format);

        $width = $im->getImageheight();
        $im->cropImage($width, $width, 0, 0);
        $im->scaleImage(110, 167, true);

        $im->writeImage($dest);

        header( "Content-Type: image/jpg" );
        echo $im;
        exit;
    }

?>
Jim Doodle
  • 569
  • 1
  • 6
  • 7

3 Answers3

2

Leverage PHP and ImageMagick to create PDF thumbnails

http://stormwarestudios.com/articles/leverage-php-imagemagick-create-pdf-thumbnails/

In this article, we discuss using PHP and ImageMagick to generate thumbnails from a given PDF, storing them in a temporary (or “cache”) directory, and serving them up to the web.

One of our more recent clients made a request to display PDF thumbnails published through the Joomla CMS that we’d deployed for them.

The requirement was fairly simple, but the execution was a little more involved. After installing ImageMagick, ImageMagick PHP bindings (which incidentally aren’t working, and a workaround was devised), and sleuthing some code, the following solution was determined:

<?php
function thumbPdf($pdf, $width)
{
    try
    {
        $tmp = 'tmp';
        $format = "png";
        $source = $pdf.'[0]';
        $dest = "$tmp/$pdf.$format";

        if (!file_exists($dest))
        {
            $exec = "convert -scale $width $source $dest";
            exec($exec);
        }

        $im = new Imagick($dest);
        header("Content-Type:".$im->getFormat());
        echo $im;
    }
    catch(Exception $e)
    {
        echo $e->getMessage();
    }
}

$file = $_GET['pdf'];
$size = $_GET['size'];
if ($file && $size)
{
    thumbPdf($file, $size);
}
?>

The above code assumes that you’ve provided appropriate permissions to the temporary directory (usually chmod 755 or chmod 777, depending on your level of courage), that you’ve saved the above code snippet in a file called thumbPdf.php, and placed this somewhere visible on your web server.

After obtaining parameters from GET, the code checks the destination temporary directory, and if the desired image is not present, it uses ImageMagick’s convert program to generate the PDF thumbnail, sized down to the appropriate proportion, and saves the image in the temporary directory. Finally, it reloads the thumbnail into an ImageMagick PHP object, and outputs the content to the browser.

Invoking the above code is done fairly easily; simply call the PHP script from inside an image tag, like so:

<img src="/path/to/thumbPdf.php?pdf=your.pdf&size=200" />

The above code would generate a thumbnail from the first page of “your.pdf”, sized 200 pixels wide by an appropriately-proportioned height.

Good luck, and happy webmastering!

Abid Hussain
  • 7,724
  • 3
  • 35
  • 53
  • 4
    That's a simple enough solution. Is there any benefit from calling the command line functions rather than the PHP extension? And are then any security issues calling the get variables without any serverside validation? – Jim Doodle Oct 08 '12 at 15:08
2

I know it's been discussed here:

Should I use a PHP extension for ImageMagick or just use PHP's Exec() function to run the terminal commands?

And to quote drew101:

You would benefit a lot using the PHP extensions instead of using exec or similar functions. Built in extensions will be faster and use less memory as you will not have to spawn new processes and read the output back. The image objects will be directly available in PHP instead of having to read file output, which should make the images easier to work with.

If you have a busy site, creating lots of processes to edit images may start to slow things down and consume additional memory.

Community
  • 1
  • 1
Jim Doodle
  • 569
  • 1
  • 6
  • 7
1

If you have not installed the Imagick php library for some reason you may use the ghost script and generate thumbnail of an pdf using the below example :

exec('gs -dSAFER -dBATCH -sDEVICE=jpeg -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r300 -sOutputFile=xyz.jpg xyz.pdf');
Muhammad Azeem
  • 1,129
  • 1
  • 12
  • 16
  • If you run that command in bash you get a `press to continue` for each page. To avoid that you need the `-dNOPAUSE` flag. So `gs -dSAFER -dBATCH -sDEVICE=jpeg -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dNOPAUSE -r300 -sOutputFile=xyz.jpg xyz.pdf` – Yes Barry Mar 08 '21 at 13:26