在PHP中将TIFF转换为JPG?

16

我有一台服务器保存TIFF图像,大多数客户端可以读取和显示TIFF图像,所以没有问题。但是,有些客户端无法处理此格式,但可以处理JPG。

我考虑使用PHP的GD库来进行服务器端转换,以便为那些无法读取TIFF的客户端提供支持。但我注意到GD也无法读取TIFF文件。

Imagick在windows上不起作用,我的想法是创建一个imageFetcher.php,它将实际的客户端请求的图像作为参数获取。然后检查客户端的类型,如果需要,将图像转换并输出为JPG格式,否则简单地输出TIFF格式。

有人有什么办法可以做到这样吗?

提前感谢。


1
看一下Image Magick。 - Louis XIV
@MikeBrant 谁说他们在 Web 上下文中使用它们了?仅仅因为使用了 HTTP 服务器并不意味着它们在网站上。如果他们正在与需要 TIFF 文件的供应商合作,并且碰巧通过 HTTP 进行传输,那该怎么办? - Patrick James McDougle
@PatrickJamesMcDougle 因为原帖讨论的是客户端浏览器无法显示TIFF文件的问题。当然,如果你在数字图像方面工作,可以提供TIFF格式的下载或其他服务,但在网页展示方面使用它们毫无意义。 - Mike Brant
@MikeBrant 他们在哪里提到浏览器? - Patrick James McDougle
3个回答

19

http://www.php.net/gd论坛中,有如下评论:

IE不能显示TIFF文件,标准PHP发行版不支持TIFF格式的转换。

ImageMagick (http://www.imagemagick.org/script/index.php)是一个免费软件,可以读取、转换和编写各种格式的图像。对于Windows用户,它包括一个PHP扩展php_magickwand_st.dll(而且,它可以在PHP 5.0.4上运行)。

当从TIFF转换为JPEG时,还必须将CMYK颜色空间转换为RGB颜色空间,因为IE也无法显示CMYK JPG。请注意: -TIFF文件可能具有RGB或CMYK颜色空间 -JPEG文件可能具有RGB或CMYK颜色空间

以下是使用ImageMagick扩展的示例功能: - 将TIFF转换为JPEG文件格式 - 将CMYK转换为RGB颜色空间 - 将图像分辨率设置为300 DPI(不更改像素大小)

<?php

function cmyk2rgb($file) {
    $mgck_wnd = NewMagickWand();
    MagickReadImage($mgck_wnd, $file);

    $img_colspc = MagickGetImageColorspace($mgck_wnd);
    if ($img_colspc == MW_CMYKColorspace) {
        echo "$file was in CMYK format<br />";
        MagickSetImageColorspace($mgck_wnd, MW_RGBColorspace);
    }
    MagickWriteImage($mgck_wnd, str_replace('.', '-rgb.', $file));
}

function tiff2jpg($file) {
    $mgck_wnd = NewMagickWand();
    MagickReadImage($mgck_wnd, $file);

    $img_colspc = MagickGetImageColorspace($mgck_wnd);
    if ($img_colspc == MW_CMYKColorspace) {
        echo "$file was in CMYK format<br />";
        MagickSetImageColorspace($mgck_wnd, MW_RGBColorspace);
    }
    MagickSetImageFormat($mgck_wnd, 'JPG' );
    MagickWriteImage($mgck_wnd, str_replace('.tif', '.jpg', $file));
}

function to300dpi($file) {
    $mgck_wnd = NewMagickWand();
    MagickReadImage($mgck_wnd, $file);
    $img_units = MagickGetImageUnits($mgck_wnd);
    switch ($img_units) {
        case MW_UndefinedResolution: $units= 'undefined'; break;
        case MW_PixelsPerInchResolution: $units= 'PPI'; break;
        case MW_PixelsPerCentimeterResolution: $units= 'PPcm'; break;
    }
    list($x_res, $y_res) = MagickGetImageResolution($mgck_wnd);
    echo "$file<br /> x_res=$x_res $units - y_res=$y_res $units<br />";
    if($x_res == 300 && $y_res == 300 && $img_units == MW_PixelsPerInchResolution) {return; }
    MagickSetImageResolution($mgck_wnd, 300 , 300);
    MagickSetImageUnits($mgck_wnd, MW_PixelsPerInchResolution);
    MagickWriteImage($mgck_wnd, str_replace('.', '-300.', $file));
}

$file='photos/test-cmyk.tif';
//this is a TIFF file in CMYK format with a 96 DPI resolution

cmyk2rgb($file);
$file = str_replace('.', '-rgb.', $file);

to300dpi($file);
$file = str_replace('.', '-300.', $file);

tiff2jpg($file);
$file = str_replace('.tif', '.jpg', $file);

to300dpi($file);
/* no file name changes as ImageMagick reports 300 DPIs
$file = str_replace('.', '-300.', $file);
*/

list($width, $height, $type, $attr) = getimagesize($file);
$width = $width/3;
$height = $height/3;
echo "<img src=\"http://localhost/$file\" width=\"$width\" height=\"$height\" alt=\"getimagesize() example\" />";
echo "<br />$file => width=$width - height=$height - type=$type - attr=$attr<br /><br />";

$file='photos/test-rgb.tif';
//this is a TIFF file in RGB format with a 96 DPI resolution

cmyk2rgb($file);
$file = str_replace('.', '-rgb.', $file);

to300dpi($file);
$file = str_replace('.', '-300.', $file);

tiff2jpg($file);
$file = str_replace('.tif', '.jpg', $file);

to300dpi($file);
/* no file name changes as ImageMagick reports 300 DPIs
$file = str_replace('.', '-300.', $file);
*/

list($width, $height, $type, $attr) = getimagesize($file);
$width = $width/3;
$height = $height/3;
echo "<img src=\"http://localhost/$file\" width=\"$width\" height=\"$height\" alt=\"getimagesize() example\" />";
echo "<br />$file => width=$width - height=$height - type=$type - attr=$attr<br /><br />";

?>

注意 - 尽管ImageMagick将JPEG文件的分辨率正确设置为300 DPI,但有些程序可能没有注意到。

否则

使用"imagick" PECL扩展

http://pecl.php.net/package/imagick

http://php.net/manual/en/book.imagick.php

根据来源和目的地(文件?网址?HTTP响应?)你需要做一些如下的操作:

 $image = new Imagick('something.tiff');
    $image->setImageFormat('png');
    echo $image;

$image->writeImage('something.png');

2
看ELSE部分。此外,我已经在顶部提到了它是从哪里获取的:“在http://www.php.net/gd论坛中写下了以下评论:”。 - Techie
基于Debian的Linux PHP安装命令为 sudo apt-get install php-imagick - Ng Sek Long

3
我使用了“convert”和ImageMagick(不用作为DLL安装),解决了这个问题。这实际上是最佳决策,因为它也解决了PDF的问题。所以我只需使用以下命令:
$command = "convert ".$filename."[0] ".$destination;
exec($command);

[0] 表示 PDF 文件的第一页,但对于 TIFF 文件而言,它仍然有效。

现在您需要做的就是在 Windows 机器上安装 'convert' 命令,并且上述 PHP 代码对两种格式都适用。只需安装这个软件即可。


1
请确保 $filename$destination 变量的内容是可信的。使用用户提供的内容与 exec 结合使用是危险的。 - Quentin S.

1

TIF可以拥有多个页面,因此需要更全面的方法。这是一个例子:

    //given uploaded file $filename    
    $ext = strtolower(substr($filename, strrpos($filename, '.') + 1));

    if ($ext == 'tif' || $ext == 'tiff') {
        $images = new Imagick($upload_path . $filename);

        //if you want to delete the original tif
        unlink($upload_path . $filename);

        $name = strtolower(substr($filename, 0, strrpos($filename, '.')));
        $filename = $name . '.png';
        foreach ($images as $i => $image) {
            $image->setImageFormat("png");
            $image->writeImage($upload_path . $i . $filename);
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接