...

View Full Version : imagecreatefromgif()



treeleaf20
11-03-2009, 10:10 PM
All,
I have the following code:


function createThumbnail($filename) {

$final_width_of_image = 100;
$final_height_of_image = 100;
$path_to_image_directory = 'uploaded_files/';
$path_to_thumbs_directory = 'thumbs/';


if(preg_match('/[.](jpg)|(JPG)|(jpeg)|(JPEG)$/', $filename)) {
$im = imagecreatefromjpeg($path_to_image_directory . $filename);
} else if (preg_match('/[.](gif)|(GIF)$/', $filename)) {
$im = imagecreatefromgif($path_to_image_directory . $filename);
} else if (preg_match('/[.](png)|(PNG)$/', $filename)) {
$im = imagecreatefrompng($path_to_image_directory . $filename);
}

$ox = imagesx($im);
$oy = imagesy($im);

$nx = $final_width_of_image;
$ny = $final_height_of_image;

$nm = imagecreatetruecolor($nx, $ny);

imagecopyresized($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);

if(!file_exists($path_to_thumbs_directory)) {
if(!mkdir($path_to_thumbs_directory)) {
die("There was a problem. Please try again!");
}
}

imagejpeg($nm, $path_to_thumbs_directory . $filename);
}


When I do this though, it says the .gif file I've uploaded isn't a valid GIF file. Any ideas why that is the case?

Fumigator
11-03-2009, 10:26 PM
Don't rely on a file's extension to determine image type. Use getimagesize() and look at the third index in the return array to find the image type. Here's a conversion chart:



$types = array(
1 => 'GIF',
2 => 'JPG',
3 => 'PNG',
4 => 'SWF',
5 => 'PSD',
6 => 'BMP',
7 => 'TIFF(intel byte order)',
8 => 'TIFF(motorola byte order)',
9 => 'JPC',
10 => 'JP2',
11 => 'JPX',
12 => 'JB2',
13 => 'SWC',
14 => 'IFF',
15 => 'WBMP',
16 => 'XBM'
);

treeleaf20
11-03-2009, 10:42 PM
So how can I pull that and then look it up in the array to get the file name?

I have:


$x = @getimagesize($file);
echo "The file type is ".$x[2];


I get "The file type is 2". So it's a JPG from your array. How can I look this up?

Then I have the following:


if(preg_match('/[.](jpg)|(JPG)|(jpeg)|(JPEG)$/', $filename)) {
$im = imagecreatefromjpeg($path_to_image_directory . $filename);
} else if (preg_match('/[.](gif)|(GIF)$/', $filename)) {
$im = imagecreatefromgif($path_to_image_directory . $filename);
} else if (preg_match('/[.](png)|(PNG)$/', $filename)) {
$im = imagecreatefrompng($path_to_image_directory . $filename);
}


Can I save the result from the array and then do the preg_match on that variable to determine what type of thing to create?

Thanks.

Fumigator
11-03-2009, 11:55 PM
Why even bother looking at the file extension? Just use that value you get from getimagesize() in your "if" statements (or even better, use a SWITCH statement).



$x = @getimagesize($file);
switch($x[2]) {
case 1:
$im = imagecreatefromgif($path_to_image_directory . $filename);
break;
case 2:
$im = imagecreatefromjpeg($path_to_image_directory . $filename);
break;
case 3:
$im = imagecreatefrompng($path_to_image_directory . $filename);
break;
default:
echo "file is not a valid image file.";
}

treeleaf20
11-04-2009, 02:10 AM
I'm not that familiar with SWITCH statements. I'm guessing this mean it's only valid for GIF, JPG and PNG? Which would be perfect because those are the only file types I want. Is this how it works?

Fumigator
11-04-2009, 04:02 PM
Sometimes there is just no substitute for a good old fashioned PHP Manual study session. :thumbsup:

http://us2.php.net/manual/en/control-structures.switch.php



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum