function file_validate_is_image

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

Parameters

Drupal\file\File $file: A file entity.

Return value

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

See also

hook_file_validate()

1 call to file_validate_is_image()
ValidatorTest::testFileValidateIsImage in drupal/core/modules/file/lib/Drupal/file/Tests/ValidatorTest.php
This ensures a specific file is actually an image.

File

drupal/core/modules/file/file.module, line 423
Defines a "managed_file" Form API field and a "file" field for Field module.

Code

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