栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在PHP中将tiff转换为jpg?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在PHP中将tiff转换为jpg?

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

<?phpfunction 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 resolutioncmyk2rgb($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';//this is a TIFF file in RGB format with a 96 DPI resolutioncmyk2rgb($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/zh/book.imagick.php

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

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

要么

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/506911.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号