在PHP中将tiff转换为jpg? [英] Convert tiff to jpg in php?

查看:394
本文介绍了在PHP中将tiff转换为jpg?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



有没有人知道如何做这样的事情?



提前致谢。

解决方案

在论坛中 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是CMYK格式< 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是CMYK格式< br />;
MagickSetImageColorspace($ mgck_wnd,MW_RGBColorspace);
}
MagickSetImageFormat($ mgck_wnd,'JPG');
MagickWriteImage($ mgck_wnd,str_replace('。tif','.jpg',$ file));
}

函数to300dpi($ file){
$ mgck_wnd = NewMagickWand();
MagickReadImage($ mgck_wnd,$ file);
$ img_units = MagickGetImageUnits($ mgck_wnd);
switch($ img_units){
case MW_UndefinedResolution:$ units ='undefined';打破;
case MW_PixelsPerInchResolution:$ units ='PPI';打破;
case MW_PixelsPerCentimeterResolution:$ units ='PPcm';打破;
}
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';
//这是一个CMYK格式的TIFF文件,96 DPI分辨率

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

到300dpi($ file);
$ file = str_replace('。',' - 300。',$ file);

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

到300dpi($ file);
/ *没有文件名更改,因为ImageMagick报告300 DPI
$ 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';
//这是RGB格式的TIFF文件,96 DPI分辨率

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

到300dpi($ file);
$ file = str_replace('。',' - 300。',$ file);

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

到300dpi($ file);
/ *没有文件名更改,因为ImageMagick报告300 DPI
$ 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,但某些程序可能不会注意到它。 / p>

ELSE



使用imagickPECL扩展程序



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;

OR

  $图像 - > writeImage( 'something.png'); 


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.

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.

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

Thanks in advance.

解决方案

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

IE doesn't show TIFF files and standard PHP distribution doesn't support converting to/from 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).

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

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)

<?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.

ELSE

Use the "imagick" PECL extension

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

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

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

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

OR

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

这篇关于在PHP中将tiff转换为jpg?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆