You are here

function image_gd_textimage_create_transparent in Textimage 7.3

Create a truecolor transparent image.

If file type does not allow transparency, the image will be filled in white. For .gif files, takes transparency from the original image resource if available and no transparent color is specified in input.

Parameters

object $image: An image object.

int $width: The width of the image, in pixels.

int $height: The height of the image, in pixels.

array $transparent: The transparent color array red/blue/green for gif transparency.

Return value

resource A GD image handle.

File

effects/textimage_text.gd.inc, line 324
GD2 toolkit for image manipulation within Textimage.

Code

function image_gd_textimage_create_transparent(stdClass $image, $width, $height, $transparent = NULL) {
  $res = imagecreatetruecolor($width, $height);
  if ($image->info['mime_type'] == 'image/png') {
    imagealphablending($res, FALSE);
    $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127);
    imagefill($res, 0, 0, $transparency);
    imagealphablending($res, TRUE);
    imagesavealpha($res, TRUE);
  }
  elseif ($image->info['mime_type'] == 'image/gif') {
    if (empty($transparent)) {

      // Grab transparent color index from image resource.
      if (isset($image->resource) && ($transparent = imagecolortransparent($image->resource) >= 0)) {

        // The original has a transparent color, allocate to the new image.
        $transparent_color = imagecolorsforindex($image->resource, $transparent);
        $transparent = imagecolorallocate($res, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
      }
      else {

        // No incoming image or no transparency channel, no color specified,
        // fill white.
        $transparent = imagecolorallocate($res, 255, 255, 255);
      }
    }
    else {

      // Get transparent from the input argument.
      $transparent = imagecache_actions_hex2rgba($transparent);
      $transparent = imagecolorallocate($res, $transparent['red'], $transparent['green'], $transparent['blue']);
    }

    // Flood with our transparent color.
    if ($transparent >= 0) {
      imagefill($res, 0, 0, $transparent);
      imagecolortransparent($res, $transparent);
    }
  }
  else {
    imagefill($res, 0, 0, imagecolorallocate($res, 255, 255, 255));
  }
  $new_image = clone $image;
  $new_image->info['width'] = $width;
  $new_image->info['height'] = $height;
  $new_image->resource = $res;
  return $new_image;
}