You are here

function geofield_map_file_validate_is_image in Geofield Map 8.2

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

file_validate_is_image()

File

./geofield_map.module, line 260
Contains the geofield_map.module.

Code

function geofield_map_file_validate_is_image(FileInterface $file) {
  $errors = [];

  /* @var \Drupal\Core\Image\ImageFactory $image_factory */
  $image_factory = \Drupal::service('image.factory');
  $image = $image_factory
    ->get($file
    ->getFileUri());
  $supported_extensions = $image_factory
    ->getSupportedExtensions();
  if ($svg_image_support = \Drupal::service('module_handler')
    ->moduleExists('svg_image')) {
    $supported_extensions[] = 'svg';
  }

  // Check before if it is an SVG file, when might be handled.

  /* @var \Drupal\file\Entity\File $file */
  if ($svg_image_support && svg_image_is_file_svg($file)) {
    $errors = [];
  }
  elseif (!$image
    ->isValid()) {
    $errors[] = t('The image file is invalid or the image type is not allowed. Allowed types: %types', [
      '%types' => implode(', ', $supported_extensions),
    ]);
  }
  return $errors;
}