You are here

function coloractions_alpha_effect in ImageCache Actions 8

Same name and namespace in other branches
  1. 7 coloractions/transparency.inc \coloractions_alpha_effect()

Image effect callback for the alpha effect.

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

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

Parameters

stdClass $image:

array $data:

Return value

bool true on success, false otherwise.

1 string reference to 'coloractions_alpha_effect'
imagecache_coloractions_image_effect_info in coloractions/imagecache_coloractions.module
Implements hook_image_effect_info().

File

coloractions/transparency.inc, line 120
Helper functions for the alpha effect.

Code

function coloractions_alpha_effect(stdClass $image, array $data) {

  // @todo: Extract to GD specific function.
  if (!$data['flatten']) {

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

    // Do the opposite, flatten the transparency ONTO the given color.
    $info = $image->info;
    if (!$info) {
      watchdog('imagecache_actions', "Problem converting image to fill behind. Source image returned no info");
      return FALSE;
    }
    $base_image = imagecreatetruecolor($info['width'], $info['height']);
    imagesavealpha($base_image, TRUE);
    imagealphablending($base_image, FALSE);

    // Start with a solid color.
    $background_rgb = imagecache_actions_hex2rgba($data['RGB']['HEX']);

    // Setting the background color here solid is what flattens the image.
    // But what I really want to do is set it colored 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 behavior back again.
    imagealphablending($base_image, TRUE);

    // Place the current image over it.
    if ($result = imagecopy($base_image, $image->resource, 0, 0, 0, 0, $info['width'], $info['height'])) {
      imagedestroy($image->resource);
      $image->resource = $base_image;
    }
    return $result;
  }
}