function file_validate_is_image
Same name in other branches
- 7.x includes/file.inc \file_validate_is_image()
- 8.9.x core/modules/file/file.module \file_validate_is_image()
- 10 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.
See also
1 call to file_validate_is_image()
- ValidatorTest::testFileValidateIsImage in core/
modules/ file/ tests/ src/ Kernel/ ValidatorTest.php - This ensures a specific file is actually an image.
File
-
core/
modules/ file/ file.module, line 371
Code
function file_validate_is_image(FileInterface $file) {
$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.