]> source.dussan.org Git - nextcloud-server.git/commitdiff
Fix for oc-972, oc-1144 and oc-1191
authorJohannes Willnecker <johannes@willnecker.com>
Thu, 5 Jul 2012 19:09:48 +0000 (21:09 +0200)
committerJörn Friedrich Dreyer <jfd@butonic.de>
Tue, 10 Jul 2012 14:58:37 +0000 (16:58 +0200)
apps/gallery/lib/managers.php
lib/image.php

index 495c51ea9c259c9b4256c51f08f8aeb3b6534b27..94e7565832dbae75553547d638162e23cd9dfc8b 100644 (file)
@@ -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'],
index e5c59bacdc50245a7db078b88510a4a38258a4a5..1d4d63707f38fddae938317aebd009afc326e775 100644 (file)
@@ -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;