file_validate_is_image(FileInterface $file)
Checks that the file is recognized as a valid image.
\Drupal\file\FileInterface $file: A file entity.
array An empty array if the file is a valid image or an array containing an error message if it's not.
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; }
© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!file!file.module/function/file_validate_is_image/8.1.x