php 在php中将tiff转换为jpg?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14342355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:05:59  来源:igfitidea点击:

Convert tiff to jpg in php?

phpimagegd

提问by Ramesh

I have a server which holds TIFF images. Most clients can read and display TIFF images, so there's no problem. However, some clients can't handle this format but can handle JPG. I thought of using PHP's GD library to do a server side conversion for clients without TIFF reading abilities. But I noticed GD can't read TIFF files too.

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

Imagick not working in windows, My idea was to create an imageFetcher.php which gets as a parameter the actual image the client wants. It checks the client's type and if needed converts the image and outputs a JPG, otherwise it simply outputs the TIFF.

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

does anyone have any idea on how I could do such a thing?

有没有人知道我怎么做这样的事情?

Thanks in advance.

提前致谢。

回答by Techie

In the forum at http://www.php.net/gdthe following comment is written:

http://www.php.net/gd的论坛中,写了以下评论:

IE doesn't show TIFF files and standard PHP distribution doesn't support converting to/from TIFF.

IE 不显示 TIFF 文件,标准 PHP 发行版不支持与 TIFF 之间的转换。

ImageMagick (http://www.imagemagick.org/script/index.php) is a free software that can read, convert and write images in a large variety of formats. For Windows users it includes a PHP extension php_magickwand_st.dll (and yes, it runs under PHP 5.0.4).

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

When converting from TIFF to JPEG, you must also convert from CMYK color space to RGB color space as IE can't show CMYK JPGs either. Please note: -TIFF files may have RGB or CMYK color space -JPEG files may have RGB or CMYK color space

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

Here are example functions using ImageMagick extension: - convert TIFF to JPEG file formats - convert CMIK to RGB color space - set image resolution to 300 DPIs (doesn't change image size in pixels)

以下是使用 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 />";

?>

Note - Although ImageMagick correctly sets JPEG files resolution to 300 DPIs, some programs might not notice it.

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

ELSE

别的

Use the "imagick" PECL extension

使用“imagick”PECL 扩展

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

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

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

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

Depending on sources and destinations (files? urls? http response?) you'll do something like:

根据来源和目的地(文件?网址?http 响应?),您将执行以下操作:

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

OR

或者

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

回答by coderama

I solved this using "convert" and ImageMagick, rather than having to install it as a DLL. Which was actually the best decision ever, because it solved the problem for PDFs as well. So I simply use:

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

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

The [0] is there for PDFs, so it will always take the first page, but it works as is for TIFF too.

[0] 用于 PDF,所以它总是占据第一页,但它也适用于 TIFF。

All you need now is to have 'convert' on your Windows Machine and the above PHP will work for both. So simply install this.

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

回答by ykay says Reinstate Monica

Tifs can have more than one page so a more comprehensive approach is needed. Here is an example:

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