# This patch file was generated by NetBeans IDE
# This patch can be applied using context Tools: Apply Diff Patch action on respective folder.
# It uses platform neutral UTF-8 encoding.
# Above lines and this line are ignored by the patching process.
Index: intelius/sysadmin/dcats/sites/all/modules/contrib/imageapi/imageapi_gd.module
--- intelius/sysadmin/dcats/sites/all/modules/contrib/imageapi/imageapi_gd.module Base (1.4)
+++ intelius/sysadmin/dcats/sites/all/modules/contrib/imageapi/imageapi_gd.module Locally Modified (Based On 1.4)
@@ -406,3 +406,60 @@
 
   return $img;
 }
+
+/**
+* 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) {
+  $destroy_image_on_exit = FALSE;
+  if (is_string($overlay) ) {
+    if (! file_exists($overlay)) {
+      watchdog('imagecache', "Image file does not exist. Attempted to overlay $overlay");
+    }
+    $overlay = imageapi_image_open($overlay);
+    $destroy_image_on_exit = TRUE;
+  }
+  if (is_resource($overlay)){
+    $r = $overlay;
+    unset($overlay);
+    $overlay->resource = $r;
+  }
+
+  $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->resource, TRUE);
+    imagesavealpha($image->resource, TRUE);
+    imagealphablending($overlay->resource, TRUE);
+    imagesavealpha($overlay->resource, TRUE);
+    imagecopy($image->resource, $overlay->resource, $x_ins, $y_ins, 0, 0, $overlay->info['width'], $overlay->info['height']);
+  }
+  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, $overlay->resource, $x_ins, $y_ins, $alpha);
+  }
+
+  if($destroy_image_on_exit){
+    imagedestroy($overlay->resource);
+  }
+  return TRUE;
+}
\ No newline at end of file
Index: intelius/sysadmin/dcats/sites/all/modules/contrib/imageapi/watermark.inc
--- intelius/sysadmin/dcats/sites/all/modules/contrib/imageapi/watermark.inc No Base Revision
+++ intelius/sysadmin/dcats/sites/all/modules/contrib/imageapi/watermark.inc Locally New
@@ -0,0 +1,113 @@
+<?php
+// $ID $
+/**
+ * @file routine for image layering
+ *
+ */
+
+
+/****************************************************************************************************************************************/
+/**
+ * Note that the below code is laboriously slow - it takes and compares every pixel of the two inputs
+ * and calculates a new valus for each of them. this is only a fallback because reliable image toolkit
+ * transparencies was buggy on certain platforms :(
+ *
+ * DOES NOT WORK WELL on indexed color yet
+ */
+
+//Niko (http://www.codeguru.com.ua)
+class watermark {
+  function create_watermark($main_img_obj, $watermark_img_obj, $x_ins, $y_ins, $alpha_level = 100) {
+    $alpha_level /= 100;
+    # Should this change to match both images?
+    $main_img_obj_w = max(imagesx($main_img_obj), imagesx($watermark_img_obj));
+    $main_img_obj_h = max(imagesy($main_img_obj), imagesy($watermark_img_obj));
+    # dman: start with the MAX, then let the caller crop it later according to its own logic.
+    #$main_img_obj_w = imagesx($main_img_obj);
+    #$main_img_obj_h = imagesy($main_img_obj);
+    $watermark_img_obj_w = imagesx($watermark_img_obj);
+    $watermark_img_obj_h = imagesy($watermark_img_obj);
+
+    $main_img_obj_min_x = $x_ins;
+    $main_img_obj_min_y = $y_ins;
+
+    $return_img = imagecreatetruecolor($main_img_obj_w, $main_img_obj_h);
+    imagesavealpha($return_img, true);
+    imagealphablending($return_img, false);
+
+    // Support indexed color (gif) if I have to
+    $transparent_ix = imagecolortransparent($main_img_obj);
+    if ($transparent_ix >= 0) {
+      $transparent = imagecolorsforindex($main_img_obj, $transparent_ix);
+      // Allocate the values to the new image and get new color.
+      $transparent = imagecolorallocatealpha($return_img, $transparent['red'], $transparent['green'], $transparent['blue'], $transparent['alpha']);
+      imagecolortransparent($return_img, $transparent);
+    }
+
+
+    for ($y = 0; $y < $main_img_obj_h; $y++) {
+      for ($x = 0; $x < $main_img_obj_w; $x++) {
+
+        $return_color = NULL;
+
+        $watermark_x = $x - $main_img_obj_min_x;
+        $watermark_y = $y - $main_img_obj_min_y;
+
+        $main_rgb = imagecolorsforindex($main_img_obj, imagecolorat($main_img_obj, $x, $y));
+
+        if ($watermark_x >= 0 && $watermark_x < $watermark_img_obj_w && $watermark_y >= 0 && $watermark_y < $watermark_img_obj_h) {
+          $watermark_rbg = imagecolorsforindex($watermark_img_obj, imagecolorat($watermark_img_obj, $watermark_x, $watermark_y));
+
+          $watermark_alpha = round(((127 - $watermark_rbg['alpha']) / 127), 2);
+          $watermark_alpha = $watermark_alpha * $alpha_level;
+
+          $avg_red = $this->_get_ave_color($main_rgb['red'], $watermark_rbg['red'], $watermark_alpha);
+          $avg_green = $this->_get_ave_color($main_rgb['green'], $watermark_rbg['green'], $watermark_alpha);
+          $avg_blue = $this->_get_ave_color($main_rgb['blue'], $watermark_rbg['blue'], $watermark_alpha);
+
+          // TODO figure out the maths for merging two transparent images
+          $new_alpha = min($watermark_rbg['alpha'], $main_rgb['alpha']);
+          #$new_alpha = $main_rgb['alpha'];
+          #$new_alpha = 0;
+
+          $return_color = $this->_get_image_color($return_img, $avg_red, $avg_green, $avg_blue, $new_alpha);
+        }
+        else {
+/*
+          // allow watermark to overflow from the image. Rare, but could be useful
+          if ($x > imagesx($main_img_obj) || $y > imagesy($main_img_obj)) {
+            $watermark_rbg = imagecolorsforindex($watermark_img_obj, imagecolorat($watermark_img_obj, $watermark_x, $watermark_y));
+            $watermark_alpha = 0;#round(((127 - $watermark_rbg['alpha']) / 127), 2);
+            $return_color = $this->_get_image_color($return_img, $watermark_rbg['red'], $watermark_rbg['green'], $watermark_rbg['blue'], $watermark_alpha);
+          }
+          else {
+*/
+            $return_color = imagecolorat($main_img_obj, $x, $y);
+/*
+          }
+*/
+        }
+        if ($return_color == $transparent_ix) {
+          $return_color = $transparent;
+        }
+        imagesetpixel($return_img, $x, $y, $return_color);
+      }
+    }
+    return $return_img;
+  }
+
+  function _get_ave_color($color_a, $color_b, $alpha_level) {
+    return round((($color_a * (1 - $alpha_level)) + ($color_b * $alpha_level)));
+  }
+
+  function _get_image_color($im, $r, $g, $b, $alpha) {
+    $c = imagecolorexactalpha($im, $r, $g, $b, $alpha);
+    if ($c != -1)
+      return $c;
+    $c = imagecolorallocate($im, $r, $g, $b, $alpha);
+    if ($c != -1)
+      return $c;
+    return imagecolorclosest($im, $r, $g, $b, $alpha);
+  }
+}
+/****************************************************************************************************************************************/
