You are here

function imagecache_alpha_image in ImageCache Actions 6

Same name and namespace in other branches
  1. 5.3 transparency.inc \imagecache_alpha_image()
  2. 5.2 transparency.inc \imagecache_alpha_image()
  3. 6.2 coloractions/transparency.inc \imagecache_alpha_image()

Given an image, manipulate the transparancy behaviour.

implementation of hook_image()

Either convert light parts of an image to see-through, or place a solid colour behind areas that would otherwise be see-though

An imagecache_action_hook() . Handle a pipelined image transformation.

To save a partially transparent image, the image resource must be switched to PNG. REMEMBER TO SWITCH IT BACK if needed

Parameters

$image handle on the image definition, including a gd image resource: to act upon

$data settings for this process.:

Return value

bool success

File

./transparency.inc, line 86
Helper functions for the alpha action for imagecache

Code

function imagecache_alpha_image(&$image, $data = array()) {
  if (!$data['flatten']) {

    // given an image, convert dark areas to opaque,
    // light to transparent,
    return png_color2alpha($image, $data['RGB']['HEX']);
  }
  else {

    // Do the opposite, flatten the transparency ONTO the given colour
    $info = $image->info;
    if (!$info) {
      watchdog("imagecache", "Problem converting image to fill behind. Source image returned no info");

      #dsm($source);
      return;

      // error
    }
    $base_image = imagecreatetruecolor($info['width'], $info['height']);
    imagesavealpha($base_image, TRUE);
    imagealphablending($base_image, FALSE);

    // Start with a solid colour
    $background_rgb = hex_to_rgb($data['RGB']['HEX']);

    // Setting the background colour here solid is what flattens the image
    $background_color = @imagecolorallocatealpha($base_image, $background_rgb['red'], $background_rgb['green'], $background_rgb['blue'], 0);

    // But what I really want to do is set it
    // coloured rgb AND 100% transparent, in the hope that
    // a failure to render transparency would instead render
    // THAT colour.
    $background_color = @imagecolorallocatealpha($base_image, $background_rgb['red'], $background_rgb['green'], $background_rgb['blue'], 0);

    // But that still doesn't work.
    // Yet somehow I've seen transparent images that fallback to white, not silver.
    imagefill($base_image, 0, 0, $background_color);

    // And set the overlay behaviour back again
    imagealphablending($base_image, TRUE);

    // Place the current image over it
    $foreground = $image->resource;
    $success = imagecopy($base_image, $image->resource, 0, 0, 0, 0, $info['width'], $info['height']);
    $image->resource = $base_image;
    return TRUE;
  }
}