You are here

function file_validate_is_image in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/file/file.module \file_validate_is_image()

Checks that the file is recognized as a valid image.

Parameters

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

Return value

array 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 core/modules/file/src/Tests/ValidatorTest.php
This ensures a specific file is actually an image.

File

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

Code

function file_validate_is_image(FileInterface $file) {
  $errors = array();
  $image_factory = \Drupal::service('image.factory');
  $image = $image_factory
    ->get($file
    ->getFileUri());
  if (!$image
    ->isValid()) {
    $supported_extensions = $image_factory
      ->getSupportedExtensions();
    $errors[] = t('Image type not supported. Allowed types: %types', array(
      '%types' => implode(' ', $supported_extensions),
    ));
  }
  return $errors;
}