Leo*_*een 29 php pdf jpeg imagemagick
我正在使用一个小脚本将PDF转换为JPG.这有效但质量很差.
剧本:
$im = new imagick( 'document.pdf[ 0]' );
$im->setImageColorspace(255);
$im->setResolution(300, 300);
$im->setCompressionQuality(95);
$im->setImageFormat('jpeg');
$im->writeImage('thumb.jpg');
$im->clear();
$im->destroy();
Run Code Online (Sandbox Code Playgroud)
还有一件事,我希望保留PDF的原始大小,但转换的大小与JPG相同.
小智 39
它可以使用setResolution
,但您需要在加载图像之前完成.尝试这样的事情:
// instantiate Imagick
$im = new Imagick();
$im->setResolution(300,300);
$im->readimage('document.pdf[0]');
$im->setImageFormat('jpeg');
$im->writeImage('thumb.jpg');
$im->clear();
$im->destroy();
Run Code Online (Sandbox Code Playgroud)
从PDF中生成的图像质量可以通过density
在读取PDF之前设置(这是DPI)来改变- 这会转到ghostscript (gs)
下面,它会光栅化PDF.为了获得良好的结果,超级采样的密度是您需要的密度的两倍,并用于resample
恢复到所需的DPI.colorspace
如果您想要RGB JPEG,请记住将其更改为RGB.
典型的命令行版本convert
可能是:
convert -density 600 document.pdf[0] -colorspace RGB -resample 300 output.jpg
Run Code Online (Sandbox Code Playgroud)
如果您需要裁剪它,-shave
如果图像在页面中居中,则重新采样后的命令通常是明智的.
至于PHP IMagick扩展,好吧,我从不亲自使用它 - 所以我不确定你如何指定文件读取提示,但我希望它是可能的.
小智 5
$im = new imagick();
//this must be called before reading the image, otherwise has no effect
$img->setResolution(200,200);
//read the pdf
$img->readImage("{$pdf_file}[0]");
Run Code Online (Sandbox Code Playgroud)