]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add caching to gallery thumbnail generation
authorBart Visscher <bartv@thisnet.nl>
Tue, 7 Feb 2012 21:33:47 +0000 (22:33 +0100)
committerBart Visscher <bartv@thisnet.nl>
Wed, 8 Feb 2012 20:12:30 +0000 (21:12 +0100)
apps/gallery/ajax/thumbnail.php
apps/gallery/lib/photo.php

index ad91edfa3c8adbc73a96a58cb418959cdc194460..2dfe936d9ddd03a54f9f1900e9974881e58d5d7f 100644 (file)
@@ -25,22 +25,14 @@ require_once('../../../lib/base.php');
 OC_JSON::checkLoggedIn();
 OC_JSON::checkAppEnabled('gallery');
 
-$box_size = 200;
 $img = $_GET['img'];
 
-$imagePath = OC_Filesystem::getLocalFile($img);
-
-if(file_exists($imagePath)) {
-  $image = new OC_Image($imagePath);
-  $image->centerCrop();
-  $image->resize($box_size, $box_size);
-  $image->fixOrientation();
-
-  $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');
-
-  $image->show();
+$image = OC_Gallery_Photo::getThumbnail($img);
+if ($image) {
+       $offset = 3600 * 24; // 24 hour
+       // 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');
+       $image->show();
 }
index d1fb166aee9a900940d5ce2081106dde7b3fd593..15783cb341a298c2f1ac1f8ae52eeec3e8a806b8 100644 (file)
@@ -21,7 +21,7 @@
 * 
 */
 
-class OC_Gallery_Photo{
+class OC_Gallery_Photo {
        public static function create($albumId, $img){
                $stmt = OC_DB::prepare('INSERT INTO *PREFIX*gallery_photos (album_id, file_path) VALUES (?, ?)');
                $stmt->execute(array($albumId, $img));
@@ -65,5 +65,34 @@ class OC_Gallery_Photo{
     $stmt = OC_DB::prepare("UPDATE *PREFIX*gallery_photos SET file_path = ?, album_id = ? WHERE album_id = ? and file_path = ?");
     $stmt->execute(array($newpath, $newAlbumId, $oldAlbumId, $oldpath));
   }
-}
 
+       public static function getThumbnail($image_name) {
+               $imagePath = OC_Filesystem::getLocalFile($image_name);
+               if(!file_exists($imagePath)) {
+                       return null;
+               }
+               $save_dir = OC_Config::getValue("datadirectory").'/'. OC_User::getUser() .'/gallery/';
+               $save_dir .= dirname($image_name). '/';
+               $image_name = basename($image_name);
+               $thumb_file = $save_dir . $image_name;
+               if (file_exists($thumb_file)) {
+                       $image = new OC_Image($thumb_file);
+               } else {
+                       $image = new OC_Image($imagePath);
+                       if ($image->valid()) {
+                               $image->centerCrop();
+                               $image->resize(200);
+                               $image->fixOrientation();
+                               if (!is_dir($save_dir)) {
+                                       mkdir($save_dir, 0777, true);
+                               }
+                               $image->save($thumb_file);
+                       }
+               }
+               if ($image->valid()) {
+                       //var_dump($image, $image->resource());
+                       return $image;
+               }
+               return null;
+       }
+}