8.5.x file.module file_validate_is_image(FileInterface $file)
8.0.x file.module file_validate_is_image(FileInterface $file)
8.1.x file.module file_validate_is_image(FileInterface $file)
8.2.x file.module file_validate_is_image(FileInterface $file)
8.3.x file.module file_validate_is_image(FileInterface $file)
8.4.x file.module file_validate_is_image(FileInterface $file)
8.6.x file.module file_validate_is_image(FileInterface $file)
6.x file.inc file_validate_is_image(&$file)
7.x file.inc file_validate_is_image(stdClass $file)

Check that the file is recognized by image_get_info() as an image.

Parameters

$file: A Drupal file object.

Return value

An array. If the file is not an image, it will contain an error message.

Related topics

File

includes/file.inc, line 800
API for handling file uploads and server file management.

Code

function file_validate_is_image(&$file) {
  $errors = array();
  $info = image_get_info($file->filepath);
  if (!$info || empty($info['extension'])) {
    $errors[] = t('Only JPEG, PNG and GIF images are allowed.');
  }
  return $errors;
}