From eb5de4d4f7b98bb1c258dbb5ed5ecf9c2d39e746 Mon Sep 17 00:00:00 2001
From: Bart Visscher <bartv@thisnet.nl>
Date: Tue, 7 Feb 2012 22:33:01 +0100
Subject: [PATCH] Change gallery thumbnail generation to OC_Image

---
 apps/gallery/ajax/galleryOp.php |  2 +-
 apps/gallery/ajax/thumbnail.php | 65 ++++++---------------------------
 lib/image.php                   |  6 ++-
 lib/util.php                    |  2 +-
 4 files changed, 18 insertions(+), 57 deletions(-)

diff --git a/apps/gallery/ajax/galleryOp.php b/apps/gallery/ajax/galleryOp.php
index f819d87f66..0ec447d01f 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 6d25c7a253..ad91edfa3c 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 6de3ed9104..255d289ea2 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 abd918044b..43fb4413f0 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=''){
-- 
GitLab