tags:

views:

70

answers:

5

How can I check to see if the image is a PNG, GIF, TIFF and JPG and if so create the thumbnail and save it to the thumb file.

So far I can check and save for JPEG images.

Here is the code below.

<?php

//Name you want to save your file as
$save = 'members/3/images/thumbs/thumb-pic.jpg';

$file = 'members/3/images/pic.jpg';
echo "Creating file: $save"; 


list($width, $height) = getimagesize($file) ;

if ($width >= 180){
 $modwidth = 180;
 $modheight = ((180.0/$width) * $height);
} else {
 $modwidth = $width;
 $modheight = $height;
}

$tn = imagecreatetruecolor($modwidth, $modheight) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

// Here we are saving the .jpg, you can make this gif or png if you want
//the file name is set above, and the quality is set to 100%
imagejpeg($tn, $save, 100) ; 
?>
+1  A: 

I'd go with the imagemagick module, specifically Imagick::identifyImage() method. It returns array like below - check what's in format key.

Array
(
    [imageName] => /some/path/image.jpg
    [format] => JPEG (Joint Photographic Experts Group JFIF format)
    [geometry] => Array
        (
            [width] => 90
            [height] => 90
        )

    [type] => TrueColor
    [colorSpace] => RGB
    [resolution] => Array
        (
            [x] => 300
            [y] => 300
        )

    [units] => PixelsPerInch
    [fileSize] => 1.88672kb
    [compression] => JPEG
    [signature] => 9a6dc8f604f97d0d691c0286176ddf992e188f0bebba98494b2146ee2d7118da
)
leafnode
How do I add this to my code?
ImAGe
Relying on imagick for something as simple as generating thumbnails is sorta sketchy... Not many hosts I've been on have had it.
brianreavis
+2  A: 

Here's one way to do it, using an associative array to assign each format to the appropriate imagecreatefrom... function:

$handlers = array(
    'jpg'  => 'imagecreatefromjpeg',
    'jpeg' => 'imagecreatefromjpeg',
    'png'  => 'imagecreatefrompng',
    'gif'  => 'imagecreatefromgif'
);

$extension = strtolower(substr($file, strrpos($file, '.')+1));
if ($handler = $handlers[$extension]){
    $image = $handler($file);
    //do the rest of your thumbnail stuff here
}else{
    //throw an 'invalid image' error
}
brianreavis
A: 

You may use mime functions. Try echo mime_content_type('php.gif'):

doc
A: 

I would do it as brianreavis has answered but I would not rely on the image file's extension to determine the image type but would rather use the exif_imagetype function.

$filetypeinfo = exif_imagetype($file);
switch($filetypeinfo){
 case 1:
 $extension = 'gif';
 break;
 case 2:
 $extension = 'jpg';
 break;
 case 3:
 $extension = 'png';
 break;
}
vsr
+1  A: 

You've got it mostly down.

Use exif_image type or use pathinfo($image_path, PATHINFO_EXTENSION) to get the file type. Then save that to a variable ($type, for example).

Then, use a switch statement to run the appropriate imagefrom* function (check the imagefromjpeg page "see also" for a list), using the variable you set of the filetype as the switch comparison.

At the end, use the same basic switch statement to run image* (imagepng, etc [check the see also on the imagejpeg page for them]) to save the thumb back to the original type (this is important for png and gifs which may have transparency, which in that case you need to enable the saving of transparency).

BTW, for transparency, check out imagesavealpha and imagealphablending.

Cryophallion