Viewed   619 times

I have a server which holds TIFF images. Most clients can read and display TIFF images, so there's no problem. However, some clients can't handle this format but can handle JPG. I thought of using PHP's GD library to do a server side conversion for clients without TIFF reading abilities. But I noticed GD can't read TIFF files too.

Imagick not working in windows, My idea was to create an imageFetcher.php which gets as a parameter the actual image the client wants. It checks the client's type and if needed converts the image and outputs a JPG, otherwise it simply outputs the TIFF.

does anyone have any idea on how I could do such a thing?

Thanks in advance.

 Answers

5

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');
Tuesday, October 4, 2022
5

You could use PHP's memory i/o stream to save the image to and subsequently get the size in bytes.

What you do is:

$img = imagecreatetruecolor(100,100);
// do your processing here
// now save file to memory
imagejpeg($img, 'php://memory/temp.jpeg'); 
$size = filesize('php://memory/temp.jpeg');

Now you should know the size

I don't know of any (gd)method to get the size of an image resource.

Saturday, December 24, 2022
1

Since a TIFF can contain multiple frames but JPG can't, you need to convert each single frame into a JPG.

Taken from Windows Dev Center Samples:

public static string[] ConvertTiffToJpeg(string fileName) 
{ 
        using (Image imageFile = Image.FromFile(fileName)) 
        { 
            FrameDimension frameDimensions = new FrameDimension( 
                imageFile.FrameDimensionsList[0]); 

            // Gets the number of pages from the tiff image (if multipage) 
            int frameNum = imageFile.GetFrameCount(frameDimensions); 
            string[] jpegPaths = new string[frameNum]; 

            for (int frame = 0; frame < frameNum; frame++) 
            { 
                // Selects one frame at a time and save as jpeg. 
                imageFile.SelectActiveFrame(frameDimensions, frame); 
                using (Bitmap bmp = new Bitmap(imageFile)) 
                { 
                    jpegPaths[frame] = String.Format("{0}\{1}{2}.jpg",  
                        Path.GetDirectoryName(fileName), 
                        Path.GetFileNameWithoutExtension(fileName),  
                        frame); 
                    bmp.Save(jpegPaths[frame], ImageFormat.Jpeg); 
                } 
            } 

            return jpegPaths; 
        } 
} 
Thursday, October 6, 2022
 
5

I've finally found the way to convert from CMYK to RGB from within Pillow (PIL) without recurring to external call to ImageMagick.

#  first - extract the embedded profile:
tiff_embedded_icc_profile = ImageCms.ImageCmsProfile(io.BytesIO(tiff_img.info.get('icc_profile')))

#  second - get the path to desired profile (in my case sRGB)
srgb_profile_path = '/Library/Application Support/Adobe/Color/Profiles/Recommended/sRGB Color Space Profile.icm'

#  third - perform the conversion (must have LittleCMS installed)
converted_image = ImageCms.profileToProfile(
                                     tiff_img,
                                     inputProfile=tiff_embedded_icc_profile,
                                     outputProfile=srgb_profile_path,
                                     renderingIntent=0,
                                     outputMode='RGB'
                                    )
#  finally - save converted image:
converted_image.save(new_name + '.jpg', quality=95, optimize=True)

All colors are preserved.

Monday, December 12, 2022
 
sje397
 
4

I had this in an old project and worked for me. Anyway, I don't remember if my pngs had really alpha transparency or "gif" transparency...

$image = imagecreatefrompng($href);
$newImage = imagecreatefrompng($href2);
imagealphablending($image, true);
imagealphablending($newImage, true);
$newImage = imagerotate($newImage, $r, -1);
$blue = imagecolorallocate($newImage, 0, 0,255);
imagecolortransparent($newImage, $blue);
imagecopy($image,$newImage,$x,$y,0,0,imagesx($newImage) , imagesy($newImage));

From the manual, in that case, imagealphablending should be replaced by imagesavealpha

Monday, December 26, 2022
 
alisso
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :