gpt4 book ai didi

php - 在php中将tiff转换为jpg?

转载 作者:行者123 更新时间:2023-12-04 13:07:17 26 4
gpt4 key购买 nike

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

Imagick 不能在 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 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');

关于php - 在php中将tiff转换为jpg?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14342355/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com