function GDToolkit::getRequirements

Same name and namespace in other branches
  1. 9 core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php \Drupal\system\Plugin\ImageToolkit\GDToolkit::getRequirements()
  2. 8.9.x core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php \Drupal\system\Plugin\ImageToolkit\GDToolkit::getRequirements()
  3. 11.x core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php \Drupal\system\Plugin\ImageToolkit\GDToolkit::getRequirements()

Overrides ImageToolkitBase::getRequirements

File

core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php, line 493

Class

GDToolkit
Defines the GD2 toolkit for image manipulation within Drupal.

Namespace

Drupal\system\Plugin\ImageToolkit

Code

public function getRequirements() {
    $requirements = [];
    $info = gd_info();
    $requirements['version'] = [
        'title' => $this->t('GD library'),
        'value' => $info['GD Version'],
    ];
    // Check if toolkit supported image formats can be actually processed by the
    // GD library installed with PHP.
    $check_formats = [
        IMG_GIF => 'GIF',
        IMG_JPG => 'JPEG',
        IMG_PNG => 'PNG',
        IMG_WEBP => 'WEBP',
    ];
    $supported_formats = array_filter($check_formats, fn($type) => imagetypes() & $type, ARRAY_FILTER_USE_KEY);
    $unsupported_formats = array_diff_key($check_formats, $supported_formats);
    $descriptions = [];
    if ($supported_formats) {
        $descriptions[] = $this->formatPlural(count($supported_formats), 'Supported image file format: %formats.', 'Supported image file formats: %formats.', [
            '%formats' => implode(', ', $supported_formats),
        ]);
    }
    if ($unsupported_formats) {
        $requirements['version']['severity'] = REQUIREMENT_WARNING;
        $unsupported = $this->formatPlural(count($unsupported_formats), 'Unsupported image file format: %formats.', 'Unsupported image file formats: %formats.', [
            '%formats' => implode(', ', $unsupported_formats),
        ]);
        $fix_info = $this->t('Check the <a href="https://www.php.net/manual/en/image.installation.php">PHP GD installation documentation</a> if you want to add support.');
        $descriptions[] = $this->t('@unsupported<br>@ref', [
            '@unsupported' => $unsupported,
            '@ref' => $fix_info,
        ]);
    }
    // Check for filter and rotate support.
    if (!function_exists('imagefilter') || !function_exists('imagerotate')) {
        $requirements['version']['severity'] = REQUIREMENT_WARNING;
        $descriptions[] = $this->t('The GD Library for PHP is enabled, but was compiled without support for functions used by the rotate and desaturate effects. It was probably compiled using the official GD libraries from the <a href="https://libgd.github.io/">gdLibrary site</a> instead of the GD library bundled with PHP. You should recompile PHP --with-gd using the bundled GD library. See <a href="https://www.php.net/manual/book.image.php">the PHP manual</a>.');
    }
    if (count($descriptions) > 1) {
        $requirements['version']['description'] = [
            '#theme' => 'item_list',
            '#items' => $descriptions,
        ];
    }
    else {
        $requirements['version']['description'] = $descriptions[0];
    }
    return $requirements;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.