ImageMagick不会改变图片的分辨率

时间:2014-11-03 13:41:52

标签: php imagemagick resolution dpi

我有一个项目,我有一个hotfolder系统,我将文件放在一个文件夹中,它执行一个PHP脚本,使用ImageMagick在文件上做一些魔术。

我想用ImageMagick做的其中一个操作是改变图片分辨率的简单过程。

我想要的是以72 DPI分辨率拍摄照片。这是我的代码的一部分:

$im = new Imagick();
$im->setResolution(72,72);
$im->readimage($xmlConfig->general->input); //Input is TIF 300 DPI
$im->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
$im->setImageResolution(72,72);
$im->resampleImage(72,72,imagick::FILTER_UNDEFINED,0);
$im->setimageformat($xmlConfig->extension); //added in EDIT
$im->writeimage($xmlConfig->general->output); //Output is also 300 DPI Filename is something.jpg added in EDIT
$im->destroy();

那么,我做错了什么?因为我不能让这个工作。我已经尝试了所有可用的组合,试图让这个正确,并且谷歌搜索作为一个疯子,仍然没有得到我想要的结果。

**编辑:*当我做一些测试时,我注意到这确实有效。但是当我将带有setimageformat的部分添加到JPEG并将文件名设置为 something.jpg 时,它出错了。

即使我在上面指定了72,JPEG也总是300 DPI。

2 个答案:

答案 0 :(得分:5)

我发现我需要这样做才能删除附加到文件的任何配置文件,以便能够对图片进行更改。

$im->stripimage();

答案 1 :(得分:2)

更新了答案

我做了以下事情:

# Create a TIF with density 300 and undefined units
convert -size 1000x1000 -density 300 xc:red input.tif

# Check what got created
identify -verbose input.tif | egrep -i "units|resol"
Resolution: 300x300
Units: Undefined

运行您的代码,修改如下:

#!/usr/local/bin/php
<?php
$im = new Imagick();
$im->setResolution(72,72);
$im->readimage('input.tif'); //Input is TIF 300 DPI
$im->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
$im->setImageResolution(72,72);
$im->resampleImage(72,72,imagick::FILTER_UNDEFINED,0);
$im->setimageformat('jpeg');
$im->writeimage("output.jpg");
$im->destroy();
?>

然后检查并得到了这个:

identify -verbose output.jpg | egrep -i "units|resol"
Resolution: 72x72
Units: PixelsPerInch

原始答案

我的回答是你的ImageMagick可能是一个过时的版本,你应该更新它。我基于以下内容:

# Create a TIF with density 300 and undefined units
convert -size 1000x1000 -density 300 xc:red input.tif

# Check what got created
identify -verbose input.tif | egrep -i "units|resol"
Resolution: 300x300
Units: Undefined

# Run your script
./go.php

# Check results
identify -verbose output.tif | egrep -i "units|resol"
Resolution: 72x72
Units: PixelsPerInch

我的版本如下:

Version: ImageMagick 6.8.9-8 Q16 x86_64 2014-10-26 http://www.imagemagick.org