function file_validate_is_image

Same name and namespace in other branches
  1. 7.x includes/file.inc \file_validate_is_image()
  2. 9 core/modules/file/file.module \file_validate_is_image()
  3. 8.9.x 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 empty array if the file is a valid image or an array containing an error message if it's not.

Deprecated

in drupal:10.2.0 and is removed from drupal:11.0.0. Use the 'file.validator' service instead.

See also

https://www.drupal.org/node/3363700

hook_file_validate()

1 call to file_validate_is_image()
LegacyValidatorTest::testFileValidateIsImage in core/modules/file/tests/src/Kernel/LegacyValidatorTest.php
This ensures a specific file is actually an image.

File

core/modules/file/file.module, line 232

Code

function file_validate_is_image(FileInterface $file) {
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use the \'file.validator\' service instead. See https://www.drupal.org/node/3363700', E_USER_DEPRECATED);
  $errors = [];
  $image_factory = \Drupal::service('image.factory');
  $image = $image_factory->get($file->getFileUri());
  if (!$image->isValid()) {
    $supported_extensions = $image_factory->getSupportedExtensions();
    $errors[] = t('The image file is invalid or the image type is not allowed. Allowed types: %types', [
      '%types' => implode(', ', $supported_extensions),
    ]);
  }
  return $errors;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.