Convert Tiff to Jpg in PHP

Convert tiff to jpg in php?

In the forum at http://www.php.net/gd the following comment is written:

IE doesn't show TIFF files and standard PHP distribution doesn't support converting to/from TIFF.

ImageMagick (http://www.imagemagick.org/script/index.php) is a free software that can read, convert and write images in a large variety of formats. For Windows users it includes a PHP extension php_magickwand_st.dll (and yes, it runs under PHP 5.0.4).

When converting from TIFF to JPEG, you must also convert from CMYK color space to RGB color space as IE can't show CMYK JPGs either. Please note:
-TIFF files may have RGB or CMYK color space
-JPEG files may have RGB or CMYK color space

Here are example functions using ImageMagick extension:
- convert TIFF to JPEG file formats
- convert CMIK to RGB color space
- set image resolution to 300 DPIs (doesn't change image size in pixels)

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

?>

Note - Although ImageMagick correctly sets JPEG files resolution to 300 DPIs, some programs might not notice it.

ELSE

Use the "imagick" PECL extension

http://pecl.php.net/package/imagick

http://php.net/manual/en/book.imagick.php

Depending on sources and destinations (files? urls? http response?) you'll do something like:

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

OR

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

Image Convert Tiff to png/jpg

Use the encode() method to convert an image and save() to save converted image.

Image::make('full/path/to/image.tiff')->encode('png')->save('path/to/saved/image.png');

Also, you've just updated the question with the code and the error. To fix the error, you need to install ImageMagick module first.

Converting TIFF byte array into JPEG byte array in PHP

The GD extension currently does not support loading TIFF images. You can use Imagick extension instead:

try {
$im = new Imagick();
$im->readImageBlob($tiff_bytes);
$im->setFormat('JPEG');
file_put_contents('test.jpeg', $im->getImageBlob());
} catch (Exception $e) {
trigger_error($e->getMessage(), E_USER_ERROR);
}

In the code above, $tiff_bytes is a binary string of a TIFF image.

Alternatively, you can install the official command line tools, save the TIFF image to filesystem, and convert it to JPEG using the following command:

convert file.jpg file.tiff 

There is a number of ways to execute a shell command in PHP. I prefer exec() for the cases when I do not need much of control over the execution, and proc_open() when I need full control over the contents of the file descriptors, i.e. in most cases.

How to convert images (GIF, JPG, JPEG, PNG) to TIFF in PHP?

i think this links helps you, checkout this:

  1. http://pecl.php.net/package/imagick
  2. http://php.net/manual/en/book.imagick.php,

Converting a TIFF image to PNG/JPG/GIF in PHP without Imagick

This is tricky because of the tiff format. You can do this for most input formats with native PHP functions to create an image object from the source file and then save that using imagejpeg or imagepng. But tiff has patent issues and I don't think it's supported. Have a look at the PHP GD and image functions available on your server. May be some help in the comments here: http://php.net/manual/en/function.pdf-open-image-file.php

TIFF file and image converting (PHP/HTML)

If you end up with bigger jpeg files than the TIFF file this maybe means that the TIFF files are CCIT Fax4 images (bilevel images often generated by scanner software) and that your JPEG files end up as RGB (24bit) images. The best you can do while targeting the web is to convert them as either GIF or paletized PNG files. I would'nt go with JPEG compression as it will generate too much artefacts on black and white text images.



Related Topics



Leave a reply



Submit