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;
}

Back To Homepage

Dean was on time and his computer diagnosis was spot on.The computer problem was fixed in a matter of minutes.Dean also noticed we had 4 computers in the house and set up our wireless router in the time he had left so we could share files.I can...

Hello, Just a quick note to say thank you to Dean for his excellent customer service he provided during a recent computer problem we had.He was more than happy to answer any questions we had, and took the time to give us a full explanation in our...

Hi there, I would like to thank Dean for the outstanding service I recently received. Dean was very thorough, explained to us in detail what was wrong with our computer,