3

After searching Google and SO, I found this little bit of code for creating thumbnails of PDF documents using ImageMagick.

The trouble for me is in implementing it into my WordPress theme. I think that I'm getting stuck on the path to cache that the script needs for temporary files.

I'm using it as described in the article:

<img src="http://localhost/multi/wp-content/themes/WPalchemy-theme/thumbPdf.php?pdf=http://localhost/multi/wp-content/uploads/2012/03/sample.pdf&size=200 />

which must be right (maybe... but I assume i am correct to use full URL to the actual file), because when I click on that URL I am taken to a page that reads the following error:

Unable to read the file: tmp/http://localhost/multi/wp-content/uploads/2012/03/sample.pdf.png

Now tmp is defined in the thumbPdf.php script, but I am confused as to what it's value should be. Is it a url or a path? Like timthumb.php, can i make it be relative to the thumbPdf.php script? (I tried ./cache which is the setting in timthumb -and was sure to have a /cache folder in my theme root, to no avail). also, fyi I put a /tmp folder in my root and still get the same error.

So how do I configure tmp to make this work?

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

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);
}

I have seen this answer: How do I convert a PDF document to a preview image in PHP? and am about to go try it next

Community
  • 1
  • 1
helgatheviking
  • 25,596
  • 11
  • 95
  • 152

1 Answers1

1

The error tells everything you need.

 Unable to read the file: tmp/http://localhost/multi/wp-content/uploads/2012/03/sample.pdf.png 

Script currently tries to read file from servers tmp/ folder.

 $tmp = 'tmp';
 $format = "png";
 $source = $pdf.'[0]';
 //$dest = "$tmp/$pdf.$format";
 $dest = "$pdf.$format";

Remember securitywise this doesn't really look so good, someone could exploit ImageMagic bug to achieve very nasty things by giving your script malformed external source pdf. You should at least check if the image is from allowed source like request originates from the same host.

Best way to work with ImageMagic is to always save the generated image and only generate a new image if generated image doesn't exist. Some ImageMagic operations are quite heavy on large files so you don't want to burden your server.

MTJ
  • 1,059
  • 1
  • 10
  • 23
  • Thanks! Great answer and great tips. Unfortunately, I have no way to really test this since I'm not working on this project any longer. – helgatheviking Jul 16 '13 at 13:39