问题描述
我正在使用Imagick :: resizeImage创建pdf文件每一页的缩略图PNG图像.但是,我返回的图像确实很模糊.我该如何使其更加锐利?任何指针将不胜感激.
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.
我尝试将Imagick :: resizeImage的模糊"参数调整为0.1-1,但没有成功.
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;
}
原始pdf链接:
https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4905263/pdf/ksgt-06-04-1091539.pdf
推荐答案
首先渲染PDF到正确的像素数,而不是先渲染然后调整栅格大小.这样会更快,并且您可以确保内容的清晰度是正确的.
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.
例如:
$ time convert -density 50 ksgt-06-04-1091539.pdf[0] x2.png
real 0m0.325s
user 0m0.299s
sys 0m0.024s
制作:
-density 50
制作的页面与您的样本425上的像素数相同.
The -density 50
makes a page about the same number of pixels across as your sample, 425.
在imagick中,您可以这样操作(正如@ fmw42的出色回答已经说过):
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]);
不幸的是,在imagick中同时执行ping和读取操作有点慢:
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
不是imagick,而是 vipsthumbnail
可以执行ping并读取一项操作:
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
如果速度很重要,可能值得考虑. libvips具有php绑定,因此您可以直接调用它,但是如果这样做,您将遇到麻烦的许可问题,因为它使用GPL库poppler进行PDF渲染. ImageMagick使用GhostScript并出于同样的原因对此进行了解释.
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.
这篇关于如何使使用Imagick :: resizeImage的缩略图更清晰-PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!