You are here

protected function ImagemagickFormatMapper::resolveEnabledExtensions in ImageMagick 8.2

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

Returns the enabled image file extensions, processing the config map.

Results are cached for subsequent access. Saving the config will invalidate the cache.

Return value

array An associative array with file extensions as keys and their ImageMagick format as values.

2 calls to ImagemagickFormatMapper::resolveEnabledExtensions()
ImagemagickFormatMapper::getEnabledExtensions in src/ImagemagickFormatMapper.php
Gets the list of currently enabled image file extensions.
ImagemagickFormatMapper::getFormatFromExtension in src/ImagemagickFormatMapper.php
Gets the image format, given the image file extension.

File

src/ImagemagickFormatMapper.php, line 216

Class

ImagemagickFormatMapper
Provides the ImageMagick format mapper.

Namespace

Drupal\imagemagick

Code

protected function resolveEnabledExtensions() {
  if ($cache = $this->cache
    ->get("imagemagick:enabled_extensions")) {
    $extensions = $cache->data;
  }
  else {

    // Get configured image formats.
    $image_formats = $this->configFactory
      ->get('imagemagick.settings')
      ->get('image_formats');

    // Get only enabled formats.
    $enabled_image_formats = array_keys($this
      ->resolveEnabledFormats());

    // Apply defaults.
    foreach ($enabled_image_formats as $format) {
      if (isset($image_formats[$format]) && is_array($image_formats[$format])) {
        $image_formats[$format] += [
          'mime_type' => NULL,
          'weight' => 0,
          'exclude_extensions' => NULL,
        ];
      }
    }

    // Scans the enabled formats to determine enabled file extensions and
    // their mapping to the internal Image/GraphicsMagick format.
    $extensions = [];
    $excluded_extensions = [];
    foreach ($enabled_image_formats as $format) {
      $format_extensions = (new MimeType($image_formats[$format]['mime_type']))
        ->getExtensions();
      $weight_checked_extensions = [];
      foreach ($format_extensions as $ext) {
        if (!isset($extensions[$ext])) {
          $weight_checked_extensions[$ext] = $format;
        }
        else {

          // Extension is already present in the array, lower weight format
          // prevails.
          if ($image_formats[$format]['weight'] < $image_formats[$extensions[$ext]]['weight']) {
            $weight_checked_extensions[$ext] = $format;
          }
        }
      }
      $extensions = array_merge($extensions, $weight_checked_extensions);

      // Accumulate excluded extensions.
      if ($image_formats[$format]['exclude_extensions']) {
        $exclude_extensions_string = Unicode::strtolower(preg_replace('/\\s+/', '', $image_formats[$format]['exclude_extensions']));
        $excluded_extensions = array_merge($excluded_extensions, array_intersect($format_extensions, explode(',', $exclude_extensions_string)));
      }
    }

    // Remove the excluded extensions.
    $excluded_extensions = array_unique($excluded_extensions);
    $excluded_extensions = array_combine($excluded_extensions, $excluded_extensions);
    $extensions = array_diff_key($extensions, $excluded_extensions);
    ksort($extensions);
    $this->cache
      ->set("imagemagick:enabled_extensions", $extensions, Cache::PERMANENT, $this->configFactory
      ->get('imagemagick.settings')
      ->getCacheTags());
  }
  return $extensions;
}