Index: imageapi_gd.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/imageapi/imageapi_gd.module,v
retrieving revision 1.12
diff -u -p -r1.12 imageapi_gd.module
--- imageapi_gd.module	9 Jul 2008 02:49:47 -0000	1.12
+++ imageapi_gd.module	9 Oct 2008 07:28:38 -0000
@@ -32,6 +32,10 @@ function imageapi_gd_settings_form() {
 
 function imageapi_gd_image_open($image) {
   if ($image->res =  _gd_open($image->source, $image->info['extension'])) {
+    // Retain transparency for PNGs
+    if ($image->info['extension'] == 'png') {
+      imagesavealpha($image->res, TRUE);
+    }
     return $image;
   }
   return false;
@@ -44,7 +48,11 @@ function imageapi_gd_image_close($image,
 function imageapi_gd_image_crop(&$image, $x, $y, $width, $height) {
   $res = _gd_create_tmp($image, $width, $height);
 
-  if (!imagecopyresampled($res, $image->res, 0, 0, $x, $y, $width, $height, $width, $height)) {
+  // Support 'cropping' to a size larger than the source - but do not copy null values into new canvas - so PNGs and GIFs can remain transparent.
+  $targetwidth = min($image->info['width'], $width);
+  $targetheight = min($image->info['height'], $height);
+
+  if (!imagecopyresampled($res, $image->res, 0, 0, $x*(-1), $y*(-1), $targetwidth, $targetheight, $targetwidth, $targetheight)) {
     return false;
   }
   // destroy the original image and return the modified image.
@@ -134,7 +142,7 @@ if (!function_exists('imagefilter')) {
 function _gd_create_tmp($image, $width, $height) { 
   $res = imagecreatetruecolor($width, $height);
   
-  if ($image->info['extension'] == 'gif' || $image->info['extension'] == 'jpg') {
+  if ($image->info['extension'] == 'gif') {
     // grab transparent color index from src.
     $transparent = imagecolortransparent($image->res);
 
@@ -142,22 +150,23 @@ function _gd_create_tmp($image, $width, 
     if ($transparent >= 0) {
       // get color(r,g,b) for index;
       $transparent = imagecolorsforindex($image->res, $transparent);
-      // allocate to new image and get new index.
-      $transparent =  (isset($color['alpha'])) 
-        ? imagecolorallocatealpha($res, $color['red'], $color['green'], $color['blue'], $color['alpha'])
-        : imagecolorallocate($res, $color['red'], $color['green'], $color['blue']);
+      // Allocate the values to the new image and get new color.
+      $transparent =  (isset($transparent['alpha'])) 
+        ? imagecolorallocatealpha($res, $transparent['red'], $transparent['green'], $transparent['blue'], $transparent['alpha'])
+        : imagecolorallocate($res, $transparent['red'], $transparent['green'], $transparent['blue']);
  
-      $transparent = imagecolorallocate($res,  $transparent['red'], $transparent['green'],  $transparent['blue']);
       // flood with our new transparent color.
       imagefill($res, 0, 0, $transparent);
       // tell the new image which color is transparent.
       imagecolortransparent($res, $transparent);
     }
   }
-  elseif ($image->info['extension'] == 'png') {
+  elseif ($image->info['extension'] == 'png' || $image->info['extension'] == 'jpg') {
+    imagesavealpha($res, true);
     imagealphablending($res, false);
-    $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127);
-    imagefill($res, 0, 0, $transparency);
+    $transparency = imagecolorallocatealpha($res, 255, 255, 255, 127);
+    #imagefill($res, 0, 0, $transparency); // imagefill doesn't always work with transparent apparently
+    imagefilledrectangle($res, 0, 0, $width, $height, $transparency);
     imagealphablending($res, true);
     imagesavealpha($res, true);
   }
