Php ImageMagick不会更改图片的分辨率

Php ImageMagick不会更改图片的分辨率,php,imagemagick,resolution,dpi,Php,Imagemagick,Resolution,Dpi,我有一个项目,在那里我有一个hotfolder系统,我把一个文件放到一个文件夹中,它执行一个PHP脚本,用ImageMagick对文件进行一些处理 我想对ImageMagick执行的操作之一是更改图片分辨率的简单过程 我想做的是得到72 DPI分辨率的图片。这是我代码的一部分: $im = new Imagick(); $im->setResolution(72,72); $im->readimage($xmlConfig->general->input); //Inpu

我有一个项目,在那里我有一个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时,它就出错了

JPEG始终为300 DPI,即使我在上面指定了72。

更新了答案

我做了以下工作:

# 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
# 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
运行您的代码,修改如下:

#!/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();
?>
Version: ImageMagick 6.8.9-8 Q16 x86_64 2014-10-26 http://www.imagemagick.org
原始答案

我的答案是你的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
# 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
我的版本如下:

#!/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();
?>
Version: ImageMagick 6.8.9-8 Q16 x86_64 2014-10-26 http://www.imagemagick.org
最新答案

我做了以下工作:

# 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
# 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
运行您的代码,修改如下:

#!/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();
?>
Version: ImageMagick 6.8.9-8 Q16 x86_64 2014-10-26 http://www.imagemagick.org
原始答案

我的答案是你的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
# 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
我的版本如下:

#!/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();
?>
Version: ImageMagick 6.8.9-8 Q16 x86_64 2014-10-26 http://www.imagemagick.org

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

$im->stripimage();

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

$im->stripimage();

您好,我已经编辑了上面的问题,因为我注意到这段代码实际上是有效的,当我将JPEG设置为输出格式,将文件名设置为JPG时,它就错了。你能不能也测试一下,看看是否得到同样的结果。在这种情况下,我需要升级到Imagemagick的最新版本,因为我有6.8.8,我认为问题在于我的TIF中包含了一个配置文件,其中说明图像是300 DPI。您需要在附加了配置文件的图像上测试代码,该配置文件的DPI与目标DPI不同。然后,如果我在转换上运行stripimage命令,我会更改DPI,否则它只使用原始TIFF中配置文件的分辨率。啊,是的。我可以想象。嗨,我编辑了我上面的问题,因为我注意到这段代码实际上是有效的,当我将JPEG设置为输出格式,将文件名设置为JPG时,它就错了。你能不能也测试一下,看看是否得到同样的结果。在这种情况下,我需要升级到Imagemagick的最新版本,因为我有6.8.8,我认为问题在于我的TIF中包含了一个配置文件,其中说明图像是300 DPI。您需要在附加了配置文件的图像上测试代码,该配置文件的DPI与目标DPI不同。然后,如果我在转换上运行stripimage命令,我会更改DPI,否则它只使用原始TIFF中配置文件的分辨率。啊,是的。我可以想象。这是我拔完头发后唯一能修好它的方法。这是我拔完头发后唯一能修好它的方法