【问题标题】:Convert tiff to jpg in php?在php中将tiff转换为jpg?
【发布时间】:2012-12-29 19:46:58
【问题描述】:

我有一个保存 TIFF 图像的服务器。大多数客户端可以读取和显示 TIFF 图像,所以没有问题。但是,有些客户端无法处理这种格式,但可以处理 JPG。 我想使用 PHP 的 GD 库为没有 TIFF 读取能力的客户端进行服务器端转换。但我注意到 GD 也无法读取 TIFF 文件。

Imagick 不能在 Windows 中工作,我的想法是创建一个 imageFetcher.php,它获取客户端想要的实际图像作为参数。它检查客户端的类型,如果需要,转换图像并输出 JPG,否则它只输出 TIFF。

有人知道我怎么能做这样的事吗?

提前致谢。

【问题讨论】:

  • 看看Image Magick
  • @MikeBrant 谁说他们在网络环境中使用它们?仅仅因为正在使用 http 服务器并不意味着它们在网站上。如果他们与需要 TIFF 文件的供应商合作,而他们恰好通过 http 传输这些文件怎么办?
  • @PatrickJamesMcDougle 因为 OP 正在讨论客户端浏览器能够显示 TIFF 文件的问题。当然,如果您正在处理数字图像,您可以下载 TIFF 或其他任何东西,但将它们用于 Web 显示毫无意义。
  • @MikeBrant 他们在哪里提到浏览器?

标签: php image gd


【解决方案1】:

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 文件格式 - 将 CMIK 转换为 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');

【讨论】:

  • 查看 ELSE 部分。我还在顶部提到了它的拍摄位置“在php.net/gd 的论坛中写了以下评论:”
  • 基于Debian的Linux PHP安装命令sudo apt-get install php-imagick
【解决方案2】:

我使用“convert”和 ImageMagick 解决了这个问题,而不必将其安装为 DLL。这实际上是有史以来最好的决定,因为它也解决了 PDF 的问题。所以我只是使用:

$command = "convert ".$filename."[0] ".$destination;
exec($command);

[0] 用于 PDF,因此它将始终占据第一页,但它也适用于 TIFF。

您现在只需要在您的 Windows 机器上进行“转换”,上面的 PHP 将适用于两者。所以只需安装this

【讨论】:

  • 请确保$filename$destination 变量内容是可信的。将exec 与用户提供的内容一起使用是危险的。
【解决方案3】:

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);
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-14
    • 1970-01-01
    • 2011-03-21
    • 1970-01-01
    • 2013-07-07
    相关资源
    最近更新 更多