Imagemagick - create a thumbnail of the first page of a PDF - PHP

843 views Asked by At

I would like to create a thumbnail of an online PDF for the first page using imageMagick. The problem is I do get a thumbnail but not for the first page of the document, or I can generate it but not for the size that I want, i.e 200px wide.

Here is the code below.

    <?php
    function createOnlineDocumentThumbnail2($index, $output, $path)
    {
        $pdf = new \Imagick($path);
        $pdf->setIteratorIndex($index);
        $pdf->setResolution(320, 320);
        //$pdf = $pdf->flattenImages();
        $pdf->setBackgroundColor('white');
        //$pdf->thumbnailImage(200, 0);
        $pdf->setImageColorspace(255);
        $pdf->setCompression(\Imagick::COMPRESSION_JPEG);
        $pdf->setCompressionQuality(100);
        $pdf->setImageFormat('jpeg');
        $pdf->writeImage($output);
        $pdf->clear();
        $pdf->destroy();
    }


    function createOnlineDocumentThumbnail($index, $output, $path)
    {
        $pdf = new \Imagick($path);
        $pdf->setIteratorIndex($index);
        $pdf->setResolution(320, 320);
        $pdf = $pdf->flattenImages();
        $pdf->setBackgroundColor('white');
        $pdf->thumbnailImage(200, 0);
        $pdf->setImageColorspace(255);
        $pdf->setCompression(\Imagick::COMPRESSION_JPEG);
        $pdf->setCompressionQuality(100);
        $pdf->setImageFormat('jpeg');
        $pdf->writeImage($output);
        $pdf->clear();
        $pdf->destroy();
    }


    $link1 = "http://www.flar.net/uploads/default/calendar/99f3a2304c1754aecffab145a5c80b98.pdf";
    $link2 = "http://www.pdf995.com/samples/pdf.pdf";

    createOnlineDocumentThumbnail2(0, 'output/link1.jpg', $link1);
    createOnlineDocumentThumbnail2(0, 'output/link2.jpg', $link2);
    createOnlineDocumentThumbnail(0, 'output/link1_small.jpg', $link1);
    createOnlineDocumentThumbnail(0, 'output/link2_small.jpg', $link2);
1

There are 1 answers

1
Bonzo On

Try adding a [0] to your link; this works locally on Imagemagick but I am not sure about Imagick and online.

$link1 = "http://www.flar.net/uploads/default/calendar/99f3a2304c1754aecffab145a5c80b98.pdf";
$link1 .= '[0]';