La Web del Programador: Comunidad de Programadores
 
    Pregunta:  63434 - CONVERTIR IMAGEN TIFF EN JPEG CON PHP
Autor:  Jose Montañez
Por favor necesito publicar en web imagenes que tengo en un servidor, estas imagenes estan en tiff. Me aconsejan cambiarlas a pdf o jpeg para poder visualizarlas.
El sistema que tengo es windows 2008 Server R2 y mi aplicacion esta en php.

  Respuesta:  sergio_between Damian
Internet explorer no soporta el formato de imagen TIFF y además PHP no soporta de manera nativa la conversión de imagenes tiff a jpeg

ImageMagick (http://www.imagemagick.org/script/index.php) es un software que convierte una gran variedad de formatos. Para usuarios de widnows existe una extensión php,php_magickwand_st.dll (corre sobre PHP 5.0.4).

Cuando se convierte desde tiif, también debes comvertir desde el espacio CMYK a RGB
porque internet explorer una vez más no soporta CMYK.
Las imagenes Tiff podrían tener RGB o CMYK en su espacio de colores.

En este ejemplo uso ImageMagick extension, y la imagen la establezco a jpeg con 300dpis. No se cambia en absoluto el tamaño de la imagen.


<?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';
//imagen de 96 dpis de resolución.

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

?>