Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. GDToolkit.php

function 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 174

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();
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal