You are here

function imageapi_gd_image_overlay in ImageCache Actions 6

Same name and namespace in other branches
  1. 5.3 imagecache_canvasactions.module \imageapi_gd_image_overlay()
  2. 5.2 imagecache_canvasactions.module \imageapi_gd_image_overlay()

Place one image over another This modifies the passed image by reference

This func is nominated for inclusion in imageapi package. Until then, we do it ourselves.

NOTE that the PHP libraries are not great at merging images SO we include a library that does it pixel-by-pixel which is INCREDIBLY inefficient. If this can be improved, in a way that supports all transparency, please let us know!

A watermark is layer onto image, return the image. An underlay is image onto layer, return the layer. Almost identical, but seeing as we work with resource handles, the handle needs to be swapped before returning.

Parameters

$image: Base imageapi object.

$overlay: May be a filename or an imageAPI object

$x: Position of the overlay

$y: Position of the overlay

$alpha: Transparency of the overlay from 0-100. 0 is totally transparent. 100 (default) is totally opaque.

$reverse: BOOL flag to indicate the 'overlay' actually goes under the image. As the imageapi callbacks modify the $image object by reference, this is needed to replace the old image resource with the new one.

Return value

bool success

File

./imagecache_canvasactions.module, line 169
A collection of canvas (layer) type manipulations for imagecache - including "Watermark"

Code

function imageapi_gd_image_overlay(&$image, &$layer, $x, $y, $alpha = 100, $reverse = FALSE) {
  if (empty($layer->resource)) {
    trigger_error("Invalid input to " . __FUNCTION__ . " 'layer' is not a valid resource");

    #dpm($layer);
    return FALSE;
  }

  // If the given alpha is 100%, we can use imagecopy - which actually works,
  // Is more efficient, and seems to retain the overlays partial transparancy
  // Still does not work great for indexed gifs though?
  if ($alpha == 100 && $layer->info['mime_type'] != 'image/gif') {
    imagealphablending($image->resource, TRUE);
    imagesavealpha($image->resource, TRUE);
    imagealphablending($layer->resource, TRUE);
    imagesavealpha($layer->resource, TRUE);
    imagecopy($image->resource, $layer->resource, $x, $y, 0, 0, $layer->info['width'], $layer->info['height']);
    imagedestroy($layer->resource);

    #imagealphablending($image->resource, FALSE);
  }
  else {

    // Else imagecopymerge fails and we have to use the slow library
    require_once 'watermark.inc';
    $watermark = new watermark();
    $image->resource = $watermark
      ->create_watermark($image->resource, $layer->resource, $x, $y, $alpha);
    imagedestroy($layer->resource);
  }
  if ($reverse) {

    // When doing underlay, It's the second image object that we really care about.
    // Update that with the result
    $layer->resource = $image->resource;
    $layer->info = $image->info;
  }
  return TRUE;
}