dongmangwei3822 2018-04-14 06:16
浏览 825
已采纳

如何使用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);
$im->setImageFormat('png');

// 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:

https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4905263/pdf/ksgt-06-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
    

    Makes:

    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
    <?php
    
    $target_width = 400;
    
    # get natural size, calculate density we need for target width
    $im = new imagick();
    $im->pingImage($argv[1]);
    $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);
    $im->readImage($argv[1]);
    
    # and write back
    $im->writeImage($argv[2]);
    

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?