Porl123 Posted September 30, 2009 Share Posted September 30, 2009 I made this function to verify images posted on profiles for this website I'm making and it seemed okay but I've just realised it's pretty slow if it's verifying more than 1-2 pictures on a single page and was just wondering if there are any ways of modifying my already made function or whether there's a better way all together. I'd prefer not to verify it with substr() or regex or anything of that nature because that just recognises the link extension and excludes things like php generated images which I'd rather not shut off if I can help it Anyway here's the function: function images($link) { list( , , $type) = @getimagesize($link); $a = 0; if($type == 1) { $a = 1; } elseif($type == 2) { $a = 1; } elseif($type == 3) { $a = 1; } elseif($type == 6) { $a = 1; } else { $a = 0; } if($a == 1) { $ret = $link; } else { $ret = ''; } return $ret; } if you can help out at all with this I really would appreciate it. anyway thanks! (: Link to comment https://forums.phpfreaks.com/topic/176105-image-link-verifying/ Share on other sites More sharing options...
kratsg Posted September 30, 2009 Share Posted September 30, 2009 if(isset($_POST['image']) && !empty($_POST['image']) && !ctype_space($_POST['image'])){ if(!(list($width,$height,$type,$attr) = @getimagesize($_POST['image']))){//If accessing the filename image is impossible, or if it isn't a valid picture, getimagesize() will generate an error of level E_WARNING. On read error, getimagesize() will generate an error of level E_NOTICE. $error_messages['Bad Image'][$error] = 'We were not able to read the image specified from the URL. This could be because it was not a valid image or there was an error in reading the image file.';$error++; } if(!$width || !$height) {//Note: Some formats may contain no image or may contain multiple images. In these cases, getimagesize() might not be able to properly determine the image size. getimagesize() will return zero for width and height in these cases. $error_messages['Bad Image'][$error] = 'We were not able to determine the width or height of the image specified from the URL because the image format may have contained no image or may contain multiple images. We suggest using PNG, GIF, or JPG to avoid this problem.';$error++; } else if($width != 475 || $height != 100){ $error_messages['Bad Image'][$error] = 'Your site\'s banner image is not 475px X 100px. We determined that it was <b>'.$width.'</b>px X <b>'.$height.'</b>px.';$error++; } if(!in_array(strtolower(image_type_to_extension($type)),array('.gif','.jpeg','.jpg','.png'))){ $error_messages['Bad Image'][$error] = 'The image you supplied is an invalid filetype: <b>'.strtolower(image_type_to_extension($type)).'</b>';$error++; } } Above is a general error handler that I've used for validating images... it works pretty nicely. getimagesize() will be slow if it tries to call/lookup images on servers that are slow. It's like calling a webpage that loads slowly (the function is only as fast as what it calls). Link to comment https://forums.phpfreaks.com/topic/176105-image-link-verifying/#findComment-927972 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.