--- imageapi.orig/imageapi_imagemagick.module	2009-01-06 00:01:17.000000000 +0100
+++ imageapi/imageapi_imagemagick.module	2009-02-11 03:51:17.000000000 +0100
@@ -119,7 +119,47 @@
   return true;
 }
 
+function _imageapi_imagemagick_rotate_target_size($width, $height, $angle) {
+  $center_x = floor($width/2);
+  $center_y = floor($height/2);
+
+  // convert to radians and precompute ...
+  $cosangle = cos(deg2rad($angle+180));
+  $sinangle = sin(deg2rad($angle+180));
+
+  // caluculate new width and height.
+  $corners=array(array(0, 0), array($width, 0), array($width, $height), array(0, $height));
+  $max_x = $min_x = $max_y = $min_y = 0;
+  foreach ($corners as $key => $value) {
+    $value[0] -= $center_x;        //Translate coords to center for rotation
+    $value[1] -= $center_y;
+    $x = $value[0] * $cosangle + $value[1] * $sinangle;
+    $y = $value[1] * $cosangle - $value[0] * $sinangle;
+    $max_x = max($x, $max_x);
+    $min_x = min($x, $min_x);
+    $max_y = max($y, $max_y);
+    $min_y = min($y, $min_y);
+  }
+  return array(round($max_x - $min_x), round($max_y - $min_y));
+}
+
 function imageapi_imagemagick_image_rotate(&$image, $degrees, $bgcolor) {
+  if ($degrees == 0) {
+    return true;
+  }
+  switch ($degrees) {
+    case 90:
+    case 270:
+      $tmp = $image->info['width'];
+      $image->info['width']  = $image->info['height'];
+      $image->info['height'] = $tmp;
+      break;
+    case 180:
+      break;
+    default:
+      list($image->info['width'], $image->info['height']) =
+        _imageapi_imagemagick_rotate_target_size($image->info['width'], $image->info['height'], $degrees);
+  }
   if (is_numeric($bgcolor)) {
     $bgcolor = '#'. str_pad(dechex($bgcolor), 6, 0);
   }
