I have a project were I need overlay as in adding one to another. I will be happy to write the a new mod, but would prefer to help add it to yours.
The GD code:
http://codingforums.com/showthread.php?t=72317

so what do you say?

thanks for you your help
Marcus

Comments

dopry’s picture

I would love to see layering primitives appear in ImageAPI. Add Layer / Merge Layers... Layers can also have properties such as stack depth, blend mode, which would be bonus, but not necessary. I'd much prefer to continue the development of ImageAPI by implementing more primitives so aggregate functions can be more easily built.

dman’s picture

FWIW, I've found the GD imagecopy function to be inadequate (broken) when merging alpha transparency, which is a useful requirement when thinking about watermarks.

Over in imagecache_actions are two implementations of an imageapi_gd_image_overlay() function that have been working pretty well in practice for the last year - but I'd appreciate any code review of it before nominating it for inclusion in imageapi.module - which is where it really deserves to live.
(there are two - one built-in, one algorithmic - to compensate for GD alpha problems)

The code

/**
 * 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!
 *
 *
 * @ingroup imageapi
 * @param $overlay may be a filename or an imageAPI object
 * @param alpha from 0-100.
 * @return bool success
 */
function imageapi_gd_image_overlay(&$image, $overlay, $x, $y, $alpha) {
  if (is_string($overlay) ) {
    if (! file_exists($overlay)) {
      watchdog('imagecache', "Image file does not exist. Attempted to overlay $overlay");
    }
    $overlay = imageapi_image_open($overlay);
  }

  $x_ins = _imagecache_keyword_filter($x, $image->info['width'], $overlay->info['width']);
  $y_ins = _imagecache_keyword_filter($y, $image->info['height'], $overlay->info['height']);

  // If the given alpha is 100%, we can use imagecopy - which actually works, 
  // Is more efficient, and seems to retain the overlays partial transparancy
  if($alpha == 100) {
    imagealphablending($image->res, TRUE);
    imagesavealpha($image->res, TRUE);
    imagealphablending($overlay->res, TRUE);
    imagesavealpha($overlay->res, TRUE);
    imagecopy($image->res, $overlay->res, $x_ins, $y_ins, 0, 0, $overlay->info['width'], $overlay->info['height']);
    imagedestroy($overlay->res);
    return TRUE;
  }
  // else imagecopymerge fails and we have to use the slow library 

  require_once('watermark.inc');
  $watermark = new watermark();
  $image->res = $watermark->create_watermark($image->res, $overlay->res, $x_ins, $y_ins, $alpha);

  imagedestroy($overlay->res);
  return TRUE;
}

The watermark.inc algorithm

drewish’s picture

dman, bump / how about taking a look and seeing how this would fit into the current version of ImageAPI.

markDrupal’s picture

This is definitely something I would like to see in Image API. I already have a hack in my code, but this looks better.

markDrupal’s picture

Version: 6.x-1.0-alpha1 » 6.x-1.x-dev
StatusFileSize
new7.69 KB

Here is a patch against image api 1.5. I also stopped the image from being destroyed if you pass an image. If you pass a file name the image will be destroyed.

BUT (That is a big but) my image creation time jumped 1000% when using this overlay. It went from 1 second pageload to 10 second page load. And didn't actually fix anything for me.

I won't be using this patch and I'll go back to:

imagecopy
imagecopymerge

markDrupal’s picture

StatusFileSize
new7.15 KB

here is a better patch, Fixed the file path

dman’s picture

my image creation time jumped 1000% when using this overlay.

I did warn you in the comments! :-)
Seeing as it is imagecache and I needed proper transparency, I could live with it, but would love if someone could find the right way to blend transparent image + transparent image at % alpha using the built-ins.
I just kept getting jaggies and black lumps when I tried.

FWIW, the current generation of imagecache_actions has added a few small tweaks to that routine to try and support gifs and conflicts between background & forground size. May need a complete rewrite though.