function GDToolkit::load

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

Loads an image from a file.

Return value

bool TRUE or FALSE, based on success.

1 call to GDToolkit::load()
GDToolkit::getImage in core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php
Retrieves the image.

File

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

Class

GDToolkit
Defines the GD2 toolkit for image manipulation within Drupal.

Namespace

Drupal\system\Plugin\ImageToolkit

Code

protected function load() {
    // Return immediately if the image file is not valid.
    if (!$this->isValid()) {
        return FALSE;
    }
    // Invalidate the image object and return if there's no function to load the
    // image file.
    $function = 'imagecreatefrom' . image_type_to_extension($this->getType(), FALSE);
    if (!function_exists($function)) {
        $this->logger
            ->error("The image toolkit '@toolkit' can not process image '@image'.", [
            '@toolkit' => $this->getPluginId(),
            '@image' => $this->getSource(),
        ]);
        $this->preLoadInfo = NULL;
        return FALSE;
    }
    // Invalidate the image object and return if the load fails.
    try {
        $image = $function($this->getSource());
    } catch (\Throwable $t) {
        $this->logger
            ->error("The image toolkit '@toolkit' failed loading image '@image'. Reported error: @class - @message", [
            '@toolkit' => $this->getPluginId(),
            '@image' => $this->getSource(),
            '@class' => get_class($t),
            '@message' => $t->getMessage(),
        ]);
        $this->preLoadInfo = NULL;
        return FALSE;
    }
    $this->setImage($image);
    if (imageistruecolor($image)) {
        return TRUE;
    }
    else {
        // Convert indexed images to truecolor, copying the image to a new
        // truecolor image, so that filters work correctly and don't result
        // in unnecessary dither.
        $data = [
            'width' => imagesx($image),
            'height' => imagesy($image),
            'extension' => image_type_to_extension($this->getType(), FALSE),
            'transparent_color' => $this->getTransparentColor(),
            'is_temp' => TRUE,
        ];
        if ($this->apply('create_new', $data)) {
            imagecopy($this->getImage(), $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
        }
    }
    return (bool) $this->getImage();
}

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