php function - Check if a file is a valid image
function fGet_image_info will grab details about an image, Including the height, width, file extension, mime type and size of file. function is_image will return an error message if the file could not be found or is not an image.
function fGet_image_info($file) {
if (!is_file($file)) {
return FALSE;
}
$details = FALSE;
$data = @getimagesize($file);
$intSize_of_file = @filesize($file);
if (isset($data) && is_array($data)) {
$arrImage_exts = array('1' => 'gif', '2' => 'jpg', '3' => 'png');
$arrImage_ext = array_key_exists($data[2], $arrImage_exts) ? $arrImage_exts[$data[2]] : '';
$details = array('width' => $data[0],
'height' => $data[1],
'extension' => $arrImage_ext,
'intSize_of_file' => $intSize_of_file,
'mime_type' => $data['mime']);
}
return $details;
}
// Checks if the file is an image or not using the fGet_image_info function
function is_image($file) {
$info = fGet_image_info($file);
if (!$info || empty($info['extension'])) {
$errors = 'Only JPEG, PNG and GIF images are allowed.';
}
return $errors;
}



