diff --git a/modules/system/image.gd.inc b/modules/system/image.gd.inc
index 39f86dc..7d580f8 100644
--- a/modules/system/image.gd.inc
+++ b/modules/system/image.gd.inc
@@ -138,7 +138,7 @@ function image_gd_rotate(stdClass $image, $degrees, $background = NULL) {
     $background = imagecolortransparent($image->resource);
 
     // If no transparent colors, use white.
-    if ($background == 0) {
+    if ($background == 0 && $background < imagecolorstotal($image->resource)) {
       $background = imagecolorallocatealpha($image->resource, 255, 255, 255, 0);
     }
   }
@@ -147,7 +147,7 @@ function image_gd_rotate(stdClass $image, $degrees, $background = NULL) {
   // transparency flags. For GIF images, keep a record of the transparent color.
   if ($image->info['extension'] == 'gif') {
     $transparent_index = imagecolortransparent($image->resource);
-    if ($transparent_index != 0) {
+    if ($transparent_index != 0 && $transparent_index < imagecolorstotal($image->resource)) {
       $transparent_gif_color = imagecolorsforindex($image->resource, $transparent_index);
     }
   }
@@ -305,7 +305,7 @@ function image_gd_create_tmp(stdClass $image, $width, $height) {
     // Grab transparent color index from image resource.
     $transparent = imagecolortransparent($image->resource);
 
-    if ($transparent >= 0) {
+    if ($transparent >= 0 && $transparent < imagecolorstotal($image->resource)) {
       // The original must have a transparent color, allocate to the new image.
       $transparent_color = imagecolorsforindex($image->resource, $transparent);
       $transparent = imagecolorallocate($res, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
