function GDToolkit::load

Same name in other branches
  1. 8.9.x core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php \Drupal\system\Plugin\ImageToolkit\GDToolkit::load()
  2. 10 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 a GD resource from a file.

Return value

bool TRUE or FALSE, based on success.

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

File

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

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;
    }
    $function = 'imagecreatefrom' . image_type_to_extension($this->getType(), FALSE);
    if (function_exists($function) && ($resource = $function($this->getSource()))) {
        $this->setResource($resource);
        if (imageistruecolor($resource)) {
            return TRUE;
        }
        else {
            // Convert indexed images to truecolor, copying the image to a new
            // truecolor resource, so that filters work correctly and don't result
            // in unnecessary dither.
            $data = [
                'width' => imagesx($resource),
                'height' => imagesy($resource),
                'extension' => image_type_to_extension($this->getType(), FALSE),
                'transparent_color' => $this->getTransparentColor(),
                'is_temp' => TRUE,
            ];
            if ($this->apply('create_new', $data)) {
                imagecopy($this->getResource(), $resource, 0, 0, 0, 0, imagesx($resource), imagesy($resource));
                imagedestroy($resource);
            }
        }
        return (bool) $this->getResource();
    }
    return FALSE;
}

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