You are here

public function GDToolkit::getTransparentColor in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php \Drupal\system\Plugin\ImageToolkit\GDToolkit::getTransparentColor()
  2. 9 core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php \Drupal\system\Plugin\ImageToolkit\GDToolkit::getTransparentColor()

Gets the color set for transparency in GIF images.

Return value

string|null A color string like '#rrggbb', or NULL if not set or not relevant.

File

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

Class

GDToolkit
Defines the GD2 toolkit for image manipulation within Drupal.

Namespace

Drupal\system\Plugin\ImageToolkit

Code

public function getTransparentColor() {
  if (!$this
    ->getResource() || $this
    ->getType() != IMAGETYPE_GIF) {
    return NULL;
  }

  // Find out if a transparent color is set, will return -1 if no
  // transparent color has been defined in the image.
  $transparent = imagecolortransparent($this
    ->getResource());
  if ($transparent >= 0) {

    // Find out the number of colors in the image palette. It will be 0 for
    // truecolor images.
    $palette_size = imagecolorstotal($this
      ->getResource());
    if ($palette_size == 0 || $transparent < $palette_size) {

      // Return the transparent color, either if it is a truecolor image
      // or if the transparent color is part of the palette.
      // Since the index of the transparent color is a property of the
      // image rather than of the palette, it is possible that an image
      // could be created with this index set outside the palette size.
      // (see http://stackoverflow.com/a/3898007).
      $rgb = imagecolorsforindex($this
        ->getResource(), $transparent);
      unset($rgb['alpha']);
      return Color::rgbToHex($rgb);
    }
  }
  return NULL;
}