1 image.gd.inc _image_gd_get_colorindex(stdClass $image, $background = NULL)

Determines the correct color index value for the background.


$image: An image object. The $image->resource values will be modified by this call.

$background: A string specifying an RGBA color in the formats: '#RGB', 'RGB', '#RGBA', 'RGBA', '#RRGGBB', 'RRGGBB', '#RRGGBBAA', 'RRGGBBAA' or '0xRRGGBBAA'.

Return value

$background: An hexadecimal color identifier or FALSE if the allocation failed. If no color is provided this will default to transparent For images that support transparency, otherwise it will be white.

See also



core/modules/system/image.gd.inc, line 403
GD2 toolkit for image manipulation within Backdrop.


function _image_gd_get_colorindex(stdClass $image, $background = NULL) {
  if (isset($background)) {
    $rgba = image_hex2rgba($background);
    $background = imagecolorallocatealpha($image->resource, $rgba['red'], $rgba['green'], $rgba['blue'], $rgba['alpha']);
  // Set the background color as transparent if $background is NULL.
  else {
    // Get the current transparent color.
    $background = imagecolortransparent($image->resource);

    // If using a PNG with alpha transparency, the color may be -1. Allocate a
    // dedicated full alpha transparent color.
    if ($background === -1) {
      $background = imagecolorallocatealpha($image->resource, 255, 255, 255, 127);
    // Allocating a transparent color may fail (e.g. on a jpg). Use white as
    // a fallback.
    if ($background === FALSE) {
      $background = imagecolorallocatealpha($image->resource, 255, 255, 255, 0);

  return $background;