--- canvasactions.inc	2009-12-28 04:26:53.000000000 +0100
+++ canvasactions.imagemagick.inc	2010-10-18 12:10:24.240436507 +0200
@@ -296,26 +296,36 @@ function imageapi_gd_image_definecanvas(
  * 
  * $targetsize is an array expected to contain a width,height and a left,top
  * offset.
+ *
+ * Patched with code at issue http://drupal.org/node/844386
  */
 function imageapi_imagemagick_image_definecanvas(&$image, $action = array()) {
   $targetsize = $action['targetsize'];
   $RGB = $action['RGB'];
+  $backgroundcolor = $RGB['HEX'] != '' ? '#'. $RGB['HEX'] : 'None';
   
   # TODO needs work.
   #
-  $crop = " -background '#{$RGB['HEX']}' -crop   '{$targetsize['width']}x{$targetsize['height']}-{$targetsize['left']}-{$targetsize['top']}!' ";
-  $draw = '';
+  $background = " -background '{$backgroundcolor}' ";
+  
+  $crop = " -crop   '{$targetsize['width']}x{$targetsize['height']}-{$targetsize['left']}-{$targetsize['top']}!' ";
 
   $draw = " -draw '";
-  $draw .= " fill '#{$RGB['HEX']}' polygon 0,0 {$targetsize['width']},0 {$targetsize['width']},{$targetsize['height']} 0,{$targetsize['height']} ";
+  $draw .= " fill '{$RGB['HEX']}' polygon 0,0 {$targetsize['width']},0 {$targetsize['width']},{$targetsize['height']} 0,{$targetsize['height']} ";
   $draw .= " ' ";
   #$draw .= " -composite ";
   $draw .= " +repage ";
 
-  $compose = " $crop $draw ";
-  $image->ops[] = $compose; 
+  $extent = " -extent '{$targetsize['width']}x{$targetsize['height']}-{$targetsize['left']}-{$targetsize['top']}!'";
+
+  if ($action['under']) {
+    $compose = " $background $crop $extent ";
+  } else {
+    $compose = " $background $crop $draw ";
+  }
+
+  $image->ops[] = $compose;
   return TRUE; 
-  
 }
 
 
