diff --git a/apps/gallery/ajax/galleryOp.php b/apps/gallery/ajax/galleryOp.php
index f819d87f6660e2d28a6a6b0b1cb40e871f4c4a03..0ec447d01f68d0745a1a4e8df63397ab57bbc4d6 100644
--- a/apps/gallery/ajax/galleryOp.php
+++ b/apps/gallery/ajax/galleryOp.php
@@ -69,7 +69,7 @@ function handlePartialCreate($path) {
   if (!OC_Filesystem::is_dir($path)) OC_JSON::error(array('cause' => 'Invalid path given'));
 
   $album = OC_Gallery_Album::find(OC_User::getUser(), null, $path);
-  $albums;
+  $albums = array();
   OC_Gallery_Scanner::scanDir($path, $albums);
   OC_JSON::success(array('album_details' => $albums));
 }
diff --git a/apps/gallery/ajax/thumbnail.php b/apps/gallery/ajax/thumbnail.php
index 6d25c7a2536ad146f352e26b73cd67ea4d39b1ae..ad91edfa3c8adbc73a96a58cb418959cdc194460 100644
--- a/apps/gallery/ajax/thumbnail.php
+++ b/apps/gallery/ajax/thumbnail.php
@@ -25,65 +25,22 @@ require_once('../../../lib/base.php');
 OC_JSON::checkLoggedIn();
 OC_JSON::checkAppEnabled('gallery');
 
-function CroppedThumbnail($imgSrc,$thumbnail_width,$thumbnail_height) { //$imgSrc is a FILE - Returns an image resource.
-    //getting the image dimensions  
-		if(! function_exists('imagecreatefromjpeg'))
-			OC_Log::write('gallery','GD module not installed',OC_Log::ERROR);
-
-    list($width_orig, $height_orig) = getimagesize($imgSrc);   
-    switch (strtolower(substr($imgSrc, strrpos($imgSrc, '.')+1))) {
-      case "jpeg":
-      case "jpg":
-      case "tiff":
-        $myImage = imagecreatefromjpeg($imgSrc);
-        break;
-      case "png":
-        $myImage = imagecreatefrompng($imgSrc);
-        break;
-      default:
-        exit();
-    }
-		if(!$myImage) exit();
-    $ratio_orig = $width_orig/$height_orig;
-    
-    if ($thumbnail_width/$thumbnail_height > $ratio_orig) {
-       $new_height = $thumbnail_width/$ratio_orig;
-       $new_width = $thumbnail_width;
-    } else {
-       $new_width = $thumbnail_height*$ratio_orig;
-       $new_height = $thumbnail_height;
-    }
-    
-    $x_mid = $new_width/2;  //horizontal middle
-    $y_mid = $new_height/2; //vertical middle
-    
-    $process = imagecreatetruecolor(round($new_width), round($new_height)); 
-
-    imagecopyresampled($process, $myImage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
-    $thumb = imagecreatetruecolor($thumbnail_width, $thumbnail_height); 
-    imagecopyresampled($thumb, $process, 0, 0, ($x_mid-($thumbnail_width/2)), ($y_mid-($thumbnail_height/2)), $thumbnail_width, $thumbnail_height, $thumbnail_width, $thumbnail_height);
-
-    imagedestroy($process);
-    imagedestroy($myImage);
-    return $thumb;
-}
-
 $box_size = 200;
 $img = $_GET['img'];
 
 $imagePath = OC_Filesystem::getLocalFile($img);
 
-if(file_exists($imagePath))
-{
-	$image = CroppedThumbnail($imagePath, $box_size, $box_size);
+if(file_exists($imagePath)) {
+  $image = new OC_Image($imagePath);
+  $image->centerCrop();
+  $image->resize($box_size, $box_size);
+  $image->fixOrientation();
 
-	header('Content-Type: image/png');
-	$offset = 3600 * 24;
-	// calc the string in GMT not localtime and add the offset
-	header("Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT");
-	header('Cache-Control: max-age='.$offset.', must-revalidate');
-	header('Pragma: public');
+  $offset = 3600 * 24;
+  // calc the string in GMT not localtime and add the offset
+  header("Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT");
+  header('Cache-Control: max-age='.$offset.', must-revalidate');
+  header('Pragma: public');
 
-	imagepng($image);
-	imagedestroy($image);
+  $image->show();
 }
diff --git a/lib/image.php b/lib/image.php
index 6de3ed9104de26344a82810413f58a5e5aa1989f..255d289ea217e147b91fea5e9115762349785b0c 100644
--- a/lib/image.php
+++ b/lib/image.php
@@ -147,6 +147,10 @@ class OC_Image {
 				return false;
 			}
 		}
+		if (!$this->valid()) {
+			return false;
+		}
+
 		$retval = false;
 		switch(self::$imagetype) {
 			case IMAGETYPE_GIF:
@@ -257,7 +261,7 @@ class OC_Image {
 				$flip = true;
 				break;
 			case 8:
-				$rotate = 270;
+				$rotate = 90;
 				$flip = false;
 				break;
 		}
diff --git a/lib/util.php b/lib/util.php
index abd918044b5ef640c9645732c953340c04e43f52..43fb4413f04bc699fdbf5ae977dd1de22bf5a501 100644
--- a/lib/util.php
+++ b/lib/util.php
@@ -110,7 +110,7 @@ class OC_Util {
 	/**
 	 * @brief Add a custom element to the header
 	 * @param string tag tag name of the element
-	 * @param array $attributes array of attrobutes for the element
+	 * @param array $attributes array of attributes for the element
 	 * @param string $text the text content for the element
 	 */
 	public static function addHeader( $tag, $attributes, $text=''){