You are here

public function ImagemagickFormatMapper::validateMap in ImageMagick 8.2

Same name and namespace in other branches
  1. 8.3 src/ImagemagickFormatMapper.php \Drupal\imagemagick\ImagemagickFormatMapper::validateMap()
  2. 8 src/ImagemagickFormatMapper.php \Drupal\imagemagick\ImagemagickFormatMapper::validateMap()

Validates the format map.

The map is an associative array with ImageMagick image formats (e.g. JPEG, GIF87, etc.) as keys and an associative array of format variables as value. Each array element is structured like the following:


  'TIFF' => [
    'mime_type' => 'image/tiff',
    'enabled' => true,
    'weight' => 10,
    'exclude_extensions' => 'tif, tifx',
  ],

The format variables are as follows:

  • 'mime_type': the MIME type of the image format. This is used to resolve the supported file extensions, e.g. ImageMagick 'JPEG' format is mapped to MIME type 'image/jpeg' which in turn will be mapped to 'jpeg jpg jpe' image file extensions.
  • 'enabled': (optional) defines if the fomat needs to be enabled within the toolkit. Defaults to TRUE.
  • 'weight': (optional) is used in cases where an image file extension is mapped to more than one ImageMagick format. It is needed in file format conversions, e.g. convert from 'png' to 'gif': shall 'GIF' or 'GIF87' internal Imagemagick format be used? The format will lower weight will be used. Defaults to 0.
  • 'exclude_extensions': (optional) is used to limit the file extensions to be supported by the toolkit, if the mapping MIME type <-> file extension returns more extensions than needed, and we do not want to alter the MIME type mapping.

Parameters

array[] $map: An associative array with formats as keys and an associative array of format variables as value.

Return value

array[][] An array of arrays of error strings.

Overrides ImagemagickFormatMapperInterface::validateMap

File

src/ImagemagickFormatMapper.php, line 79

Class

ImagemagickFormatMapper
Provides the ImageMagick format mapper.

Namespace

Drupal\imagemagick

Code

public function validateMap(array $map) {
  $errors = [];

  // Get current config object and change the format map.
  $data = $this->configFactory
    ->get('imagemagick.settings')
    ->get();
  $data['image_formats'] = $map;

  // Validates against schema.
  $schema_errors = $this
    ->checkConfigSchema($this->typedConfig, 'imagemagick.settings', $data);
  if ($schema_errors !== TRUE) {
    foreach ($schema_errors as $key => $value) {
      list(, $path) = explode(':', $key);
      $components = explode('.', $path);
      if ($components[0] === 'image_formats') {
        if (isset($components[2])) {
          $errors[$components[1]]['variables'][$components[2]][] = $value;
        }
        else {
          $errors[$components[1]]['format'][] = $value;
        }
      }
    }
  }

  // Other checks.
  foreach ($map as $key => $value) {
    if (Unicode::strtoupper($key) != $key) {

      // Formats must be typed in uppercase.
      $errors[$key]['format'][] = $this
        ->t("The format (@key) must be entered in all uppercase characters.", [
        '@key' => $key,
      ])
        ->render();
    }
    if (!isset($value['mime_type'])) {

      // Formats must have a MIME type mapped.
      $errors[$key]['format'][] = $this
        ->t("Missing mime_type variable.")
        ->render();
    }
    else {

      // MIME type must exist.
      try {
        (new MimeType($value['mime_type']))
          ->getExtensions();
      } catch (MimeTypeMappingException $e) {
        $errors[$key]['variables']['mime_type'][] = $e
          ->getMessage();
      }
    }
  }
  return $errors;
}