Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
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:
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
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).
PHP Code:
$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.";
}
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?