From 3e53bf4a868405d1d5ef8ba0ecc07ecf5ebb3a59 Mon Sep 17 00:00:00 2001
From: Johannes Willnecker <johannes@willnecker.com>
Date: Thu, 5 Jul 2012 21:09:48 +0200
Subject: [PATCH] Fix for oc-972, oc-1144 and oc-1191

---
 apps/gallery/lib/managers.php | 38 +++++++++++++----
 lib/image.php                 | 80 +++++++++++++++++++++++++++++++----
 2 files changed, 101 insertions(+), 17 deletions(-)

diff --git a/apps/gallery/lib/managers.php b/apps/gallery/lib/managers.php
index 495c51ea9c2..94e7565832d 100644
--- a/apps/gallery/lib/managers.php
+++ b/apps/gallery/lib/managers.php
@@ -25,6 +25,15 @@ class DatabaseManager {
 		}
 	}
 
+	public function setFileData($path, $width, $height) {
+		$stmt = \OCP\DB::prepare('INSERT INTO *PREFIX*pictures_images_cache (uid_owner, path, width, height) VALUES (?, ?, ?, ?)');
+		$stmt->execute(array(\OCP\USER::getUser(), $path, $width, $height));
+		$ret = array('path' => $path, 'width' => $width, 'height' => $height);
+		unset($image);
+		$this->cache[$dir][$path] = $ret;
+		return $ret;
+	}
+
 	public function getFileData($path) {
 		$gallery_path = \OCP\Config::getSystemValue( 'datadirectory' ).'/'.\OC_User::getUser().'/gallery';
 		$path = $gallery_path.$path;
@@ -39,9 +48,7 @@ class DatabaseManager {
 		if (!$image->loadFromFile($path)) {
 			return false;
 		}
-		$stmt = \OCP\DB::prepare('INSERT INTO *PREFIX*pictures_images_cache (uid_owner, path, width, height) VALUES (?, ?, ?, ?)');
-		$stmt->execute(array(\OCP\USER::getUser(), $path, $image->width(), $image->height()));
-		$ret = array('path' => $path, 'width' => $image->width(), 'height' => $image->height());
+		$ret = $this->setFileData($path, $image->width(), $image->height());
 		unset($image);
 		$this->cache[$dir][$path] = $ret;
 		return $ret;
@@ -76,7 +83,7 @@ class ThumbnailsManager {
 
 		$image->fixOrientation();
 
-		$ret = $image->preciseResize(floor((150*$image->width())/$image->height()), 150);
+		$ret = $image->preciseResize($this->getThumbnailWidth($image), $this->getThumbnailHeight($image));
 		
 		if (!$ret) {
 			\OC_Log::write(self::TAG, 'Couldn\'t resize image', \OC_Log::ERROR);
@@ -87,13 +94,28 @@ class ThumbnailsManager {
 		$image->save($gallery_path.'/'.$path);
 		return $image;
 	}
-	
+
+	public function getThumbnailWidth($image) {
+		return floor((150*$image->widthTopLeft())/$image->heightTopLeft());
+	}
+
+	public function getThumbnailHeight($image) {
+		return 150;
+	}
+
 	public function getThumbnailInfo($path) {
 		$arr = DatabaseManager::getInstance()->getFileData($path);
 		if (!$arr) {
-			$thubnail = $this->getThumbnail($path);
-			unset($thubnail);
-			$arr = DatabaseManager::getInstance()->getFileData($path);
+			if (!\OC_Filesystem::file_exists($path)) {
+				\OC_Log::write(self::TAG, 'File '.$path.' don\'t exists', \OC_Log::WARN);
+				return false;
+			}
+			$image = new \OC_Image();
+			$image->loadFromFile(\OC_Filesystem::getLocalFile($path));
+			if (!$image->valid()) {
+				return false;
+			}
+			$arr = DatabaseManager::getInstance()->setFileData($path, $this->getThumbnailWidth($image), $this->getThumbnailHeight($image));
 		}
 		$ret = array('filepath' => $arr['path'],
 					 'width' => $arr['width'],
diff --git a/lib/image.php b/lib/image.php
index e5c59bacdc5..1d4d63707f3 100644
--- a/lib/image.php
+++ b/lib/image.php
@@ -107,6 +107,56 @@ class OC_Image {
 		return $this->valid() ? imagesy($this->resource) : -1;
 	}
 
+	/**
+	* @brief Returns the width when the image orientation is top-left.
+	* @returns int
+	*/
+	public function widthTopLeft() {
+		$o = $this->getOrientation();
+		OC_Log::write('core','OC_Image->widthTopLeft() Orientation: '.$o, OC_Log::DEBUG);
+		switch($o) {
+			case -1:
+			case 1:
+			case 2: // Not tested
+			case 3:
+			case 4: // Not tested
+				return $this->width();
+				break;
+			case 5: // Not tested
+			case 6:
+			case 7: // Not tested
+			case 8:
+				return $this->height();
+				break;
+		}
+		return $this->width();
+	}
+
+	/**
+	* @brief Returns the height when the image orientation is top-left.
+	* @returns int
+	*/
+	public function heightTopLeft() {
+		$o = $this->getOrientation();
+		OC_Log::write('core','OC_Image->heightTopLeft() Orientation: '.$o, OC_Log::DEBUG);
+		switch($o) {
+			case -1:
+			case 1:
+			case 2: // Not tested
+			case 3:
+			case 4: // Not tested
+				return $this->height();
+				break;
+			case 5: // Not tested
+			case 6:
+			case 7: // Not tested
+			case 8:
+				return $this->width();
+				break;
+		}
+		return $this->height();
+	}
+
 	/**
 	* @brief Outputs the image.
 	* @returns bool
@@ -209,34 +259,46 @@ class OC_Image {
 
 	/**
 	* (I'm open for suggestions on better method name ;)
-	* @brief Fixes orientation based on EXIF data.
-	* @returns bool.
+	* @brief Get the orientation based on EXIF data.
+	* @returns The orientation or -1 if no EXIF data is available.
 	*/
-	public function fixOrientation() {
+	public function getOrientation() {
 		if(!is_callable('exif_read_data')){
 			OC_Log::write('core','OC_Image->fixOrientation() Exif module not enabled.', OC_Log::DEBUG);
-			return false;
+			return -1;
 		}
 		if(!$this->valid()) {
 			OC_Log::write('core','OC_Image->fixOrientation() No image loaded.', OC_Log::DEBUG);
-			return false;
+			return -1;
 		}
 		if(is_null($this->filepath) || !is_readable($this->filepath)) {
 			OC_Log::write('core','OC_Image->fixOrientation() No readable file path set.', OC_Log::DEBUG);
-			return false;
+			return -1;
 		}
 		$exif = @exif_read_data($this->filepath, 'IFD0');
 		if(!$exif) {
-			return false;
+			return -1;
 		}
 		if(!isset($exif['Orientation'])) {
-			return true; // Nothing to fix
+			return -1;
 		}
-		$o = $exif['Orientation'];
+		return $exif['Orientation'];
+	}
+
+	/**
+	* (I'm open for suggestions on better method name ;)
+	* @brief Fixes orientation based on EXIF data.
+	* @returns bool.
+	*/
+	public function fixOrientation() {
+		$o = $this->getOrienation();
 		OC_Log::write('core','OC_Image->fixOrientation() Orientation: '.$o, OC_Log::DEBUG);
 		$rotate = 0;
 		$flip = false;
 		switch($o) {
+			case -1:
+				return false; //Nothing to fix
+				break;
 			case 1:
 				$rotate = 0;
 				$flip = false;
-- 
GitLab