在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';
cmyk2rgb($file);
$file = str_replace('.', '-rgb.', $file);
to300dpi($file);
$file = str_replace('.', '-300.', $file);
tiff2jpg($file);
$file = str_replace('.tif', '.jpg', $file);
to300dpi($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';
cmyk2rgb($file);
$file = str_replace('.', '-rgb.', $file);
to300dpi($file);
$file = str_replace('.', '-300.', $file);
tiff2jpg($file);
$file = str_replace('.tif', '.jpg', $file);
to300dpi($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');