Imagemagick - 创建PDF第一页的缩略图 - PHP

时间:2016-12-23 07:29:28

标签: php imagemagick image-manipulation imagick

我想使用imageMagick为第一页创建在线PDF的缩略图。问题是我得到一个缩略图但不是文档的第一页,或者我可以生成它但不是我想要的大小,即200px宽。

以下是代码。

    <?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 个答案:

答案 0 :(得分:0)

尝试在链接中添加[0];这在Imagemagick本地工作,但我不确定Imagick和在线。

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