php tiff,在PHP中将tiff转换为jpg?

小编典典

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(不更改以像素为单位的图像大小)

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
";

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
";

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
x_res=$x_res $units - y_res=$y_res $units
";

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 "\"getimagesize()";

echo "
$file => width=$width - height=$height - type=$type - attr=$attr
";

$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 "\"getimagesize()";

echo "
$file => width=$width - height=$height - type=$type - attr=$attr
";

?>

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

其他

使用“ imagick” PECL扩展名

根据源和目标(文件?URL?http响应?),您将执行以下操作:

$image = new Imagick('something.tiff');

$image->setImageFormat('png');

echo $image;

要么

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

2020-05-29

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值