2018-04-14 06:16
浏览 704

如何使用Imagick :: resizeImage制作缩略图看起来更清晰 - PHP?

I am using Imagick::resizeImage to create a thumbnail PNG image of each page of a pdf file. However, the image I am getting back is really blurry. How can I make it a little sharper? Any pointers would be really appreciated.

I have tried adjusting the 'blur' paramter of Imagick::resizeImage between 0.1 - 1, without success.

$pdfPage = '1Mpublic.pdf[0]';
$im = new imagick($pdfPage);

// Resize thumbnail image
$imgHeight =  $im -> getImageHeight();
$imgWidth = $im -> getImageWidth();
$desiredWidth = 200;
$desiredHeight = resizedImageHeight($imgWidth, $imgHeight, $desiredWidth);

$im -> setResolution(1500, 1500);
$im -> resizeImage($desiredWidth, $desiredHeight, imagick::STYLE_NORMAL, 0.1);

 /* Resize image */
function resizedImageHeight($imgWidth, $imgHeight, $desiredImgWidth){

    $quoient = $imgWidth/$imgHeight;
    $height = $desiredImgWidth/$quoient;

    return $height;

enter image description here

original pdf link:

图片转代码服务由CSDN问答提供 功能建议

我使用Imagick :: resizeImage创建pdf文件每页的缩略图PNG图像。 然而,我回来的图像真的很模糊。 我怎样才能让它变得更清晰? 任何指针都会非常感激。

我试过调整Imagick :: resizeImage的'blur'参数在0.1 - 1之间,但没有成功。

  $ pdfPage ='1Mpublic.pdf [0]'; 
 $ im = new imagick($ pdfPage); 
 $ im-> setImageFormat('png'); 
 //调整缩略图大小 image 
 $ imgHeight = $ im  - >  getImageHeight(); 
 $ imgWidth = $ im  - >  getImageWidth(); 
 $ desiredWidth = 200; 
 $ desiredHeight = resizedImageHeight($ imgWidth,$ imgHeight,$ desiredWidth); 
 $ im  - >  setResolution(1500,1500); 
 $ im  - >  resizeImage($ desiredWidth,$ desiredHeight,imagick :: STYLE_NORMAL,0.1); 
 / *调整大小图像* / 
function resizedImageHeight($ imgWidth,$ imgHeight,$ desiredImgWidth){
 $ quoient = $ imgWidth /  $ imgHeight; 
 $ height = $ desiredImgWidth / $ quoient; 
返回$ height; 

原始pdf链接: 04-1091539.pdf

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongwei2882 2018-04-16 08:39

    Rather than rendering and then resizing the raster, it might be better to render the PDF to the right number of pixels in the first place. It'll be faster, and you can be sure that the amount of sharpness is correct for the content.

    For example:

    $ time convert -density 50 ksgt-06-04-1091539.pdf[0] x2.png
    real    0m0.325s
    user    0m0.299s
    sys 0m0.024s


    direct sizing output

    The -density 50 makes a page about the same number of pixels across as your sample, 425.

    In imagick you could do it like this (as @fmw42's excellent answer already says):

    #!/usr/bin/env php
    $target_width = 400;
    # get natural size, calculate density we need for target width
    $im = new imagick();
    $geo = $im->getImageGeometry();
    $natural_width = $geo['width'];
    $density = 72.0 * $target_width / $natural_width;
    # load at correct resolution for target_width
    $im = new imagick();
    $im->setResolution($density, $density);
    # and write back

    Doing both the ping and the read is a little slow in imagick, unfortunately:

    $ time ./pdfthumb.php ksgt-06-04-1091539.pdf x.png
    real    0m2.773s
    user    0m2.737s
    sys 0m0.036s

    It's not imagick, but vipsthumbnail can do the ping and read in one operation:

    $ time vipsthumbnail ksgt-06-04-1091539.pdf -s 400x -o x2.png
    real    0m0.064s
    user    0m0.064s
    sys 0m0.011s

    It might be worth considering if speed is important. libvips has a php binding so you can call it directly, but if you do that you'll run into awful licensing problems because it uses the GPL library poppler for PDF rendering, sigh. ImageMagick uses GhostScript and shells out to that for the same reason.

    打赏 评论
  • douweibeng5219 2018-04-16 06:34

    Unfortunately, I do not know Imagick that well. But in Imagemagick command line, I would do what is sometimes called supersampling. That is use a large density to read the PDF, then resize down by the inverse scale factor.

    For example, nominal density is 72 dpi. I would read the input PDF at 4*72=288 dpi. Then after rasterizing, I would resize by 1/4=25% or for a larger result by something larger than 25%, say 50%. Here is your first page done both ways:

    convert -density 288 ksgt-06-04-1091539.pdf[0] -resize 25% result1.png

    enter image description here

    convert -density 288 ksgt-06-04-1091539.pdf[0] -resize 50% result2.png

    enter image description here

    In Imagick, the key is something like:

    $imagick = new Imagick(); 
    $imagick->setImageResolution(288, 288);
    打赏 评论

相关推荐 更多相似问题