
Originally Posted by
mc12345678
If, however, going to use code like above, I would recommend that the applicable image type constant be used to maximize compatibility and code understanding when comparing the image type against $image_type. Such as: IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_PNG, etc. instead of just a number. Based on looking over a few other things it seems that if going to go to such extremes, then should also validate that the image is large enough to support evaluation with the applicable function so that won't throw another error that was just attempted to be prevented.
i beg to differ that using a numeric value affects compatibility in any way. i agree that using the constant helps in readability.
given that, i think the following code addresses the problems i have encountered and is an improvement over the base code in this plugin. i have chosen to not do any of the size validations.
PHP Code:
function load_imageGD($src_name) {
// create an image of the given filetype
$file_ext = substr($src_name, strrpos($src_name, '.'));
$image_type = exif_imagetype($src_name);
switch ($image_type) {
case 'IMAGETYPE_GIF':
if(!function_exists("imagecreatefromgif")) return false;
$image = imagecreatefromgif($src_name);
break;
case 'IMAGETYPE_PNG':
if(!function_exists("imagecreatefrompng")) return false;
$image = imagecreatefrompng($src_name);
break;
case 'IMAGETYPE_JPEG':
if(!function_exists("imagecreatefromjpeg")) return false;
$image = imagecreatefromjpeg($src_name);
break;
}
return $image;
Bookmarks