You are here

function image_gd_overlay in ImageCache Actions 7

Same name and namespace in other branches
  1. 8 image_overlay.inc \image_gd_overlay()

GD toolkit specific implementation of the image overlay effect.

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

stdClass $image: An image object.

stdClass $layer: Image object to be placed over or under the $image image.

int $x: Position of the overlay.

int $y: Position of the overlay.

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

boolean $reverse: Flag to indicate that the 'overlay' actually goes under the image.

Return value

boolean true on success, false otherwise.

File

./image_overlay.inc, line 68
extension to imageapi, provide an overlay action for blending two layers, preserving transparency.

Code

function image_gd_overlay(stdClass $image, stdClass $layer, $x, $y, $alpha = 100, $reverse = FALSE) {

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

    // imagecopy() cannot be used and we have to use the slow library.
    module_load_include('inc', 'imagecache_actions', 'watermark');
    $watermark = new watermark();
    $result_img = $watermark
      ->create_watermark($lower->resource, $upper->resource, $x, $y, $alpha);

    // Watermark creates a new image resource, so clean up both old images.
    imagedestroy($lower->resource);
    imagedestroy($upper->resource);
    $image->resource = $result_img;
    $image->info = $lower->info;
  }
  return TRUE;
}