KencOur Zuckerberg
KencOur Zuckerberg

Reputation: 21

Create Thumbnail of ONLINE PDF for first page only using Imagick

I tried to make a thumbnail of a pdf file which is hosted on another server. My current code is:

<?php
$im = new imagick("http://www.d3publisher.us/Walkthroughs/Naruto_NC_3_DS.pdf");
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $im;
?>

The problem is that code is only generating thumbnail for LAST PAGE of the pdf file. How can I make a thumbnail for first page only? I tried to add [0] at the imagick line.

$im = new imagick("http://www.d3publisher.us/Walkthroughs/Naruto_NC_3_DS.pdf[0]");

but it didn't work. It only work for local pdf file, i.e:

$im = new imagick("my-pdf-file.pdf[0]");

Please help me solve this problem.. Thanks..

Upvotes: 2

Views: 9156

Answers (3)

John Doe
John Doe

Reputation: 11

Try...

$im->setImageIndex(0); //this will return 1th page of the pdf file
$im->setImageFormat('jpg');

Upvotes: 1

Leo smith
Leo smith

Reputation: 116

"This can be done with Imagick::setIteratorIndex. .."

..or not. Simply has no effect . Setting it to one crashes something, setting it to 0 gets the last page..

function make_thumbnail($filename)
    {

    try
        {
        $imagick= new Imagick($filename);
        }
    catch(ImagickException $e)
        {
        // failed to make a thimbynail. what now?
        // load up our trusty truetype font png instead?
        $imagick->destroy();
        return "0"; // shove any rubbish in the db - it will just say no image available when asked.
        }
    $imagick->setIteratorIndex(0);// rewind to first page or image of a multi series
    $imagick->setImageFormat("png"); // turn it into a png
    $imagick = $imagick->flattenImages(); // remove any transparency

    $imagick->scaleImage(300,0);  //resize...to less than 300px wide
    $d = $imagick->getImageGeometry();
        $h = $d['height'];
        if($h > 300) 
        $imagick->scaleImage(0,300); 
    $imagick->setImageCompression(\Imagick::COMPRESSION_UNDEFINED);
    $imagick->setImageCompressionQuality(0);
    $imagick->setIteratorIndex(0);
    $a = $imagick->getImageBlob(); // output as bytestream
    $imagick->destroy();
    return $a;
    }

Upvotes: 0

emcconville
emcconville

Reputation: 24419

You'll need to reset the active image to the first page. This can be done with Imagick::setIteratorIndex.

<?php
$im = new imagick("http://www.d3publisher.us/Walkthroughs/Naruto_NC_3_DS.pdf");
$im->setIteratorIndex(0);
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $im;
?>

Upvotes: 5

Related Questions