some much more flexible thumbnail generation
authorAndreas Unterkircher <unki@netshadow.at>
Wed, 20 Jun 2007 20:02:26 +0000 (20:02 +0000)
committerAndreas Unterkircher <unki@netshadow.at>
Wed, 20 Jun 2007 20:02:26 +0000 (20:02 +0000)
git-svn-id: file:///var/lib/svn/phpfspot/trunk@118 fa6a889d-dae6-447d-9e79-4ba9a3039384

phpfspot.class.php

index 18c00cfee37005b3ce9cac9a6f0c550d96ccd1c7..06ded529c331c1804c6942d02ea3a7eb25daf5be 100644 (file)
@@ -117,25 +117,39 @@ class PHPFSPOT {
          }
       }
 
          }
       }
 
-
       $details = $this->get_photo_details($photo);
       $details = $this->get_photo_details($photo);
-      $meta = $this->get_meta_informations($this->translate_path($details['directory_path']) ."/". $details['name']);
-      $info = getimagesize($this->translate_path($details['directory_path']) ."/thumbs/". $this->cfg->photo_width ."_". $details['name']);
+      $orig_path = $this->translate_path($details['directory_path']) ."/". $details['name'];
+      $thumb_path = $this->translate_path($details['directory_path']) ."/thumbs/". $this->cfg->photo_width ."_". $details['name'];
+
+      /* If the thumbnail doesn't exist yet, try to create it */
+      if(!file_exists($thumb_path)) {
+         $this->gen_thumb($photo, 0, 1);
+      }
+
+      $meta = $this->get_meta_informations($orig_path);
 
 
-      $this->tmpl->assign('description', $details['description']);
-      $this->tmpl->assign('image_name', $details['name']);
+      if(file_exists($thumb_path)) {
 
 
-      $this->tmpl->assign('width', $info[0]);
-      $this->tmpl->assign('height', $info[1]);
-      $this->tmpl->assign('ExifMadeOn', strftime("%a %x %X", $meta['FileDateTime']));
-      $this->tmpl->assign('ExifMadeWith', $meta['Make'] ." ". $meta['Model']);
-      $this->tmpl->assign('ExifOrigResolution', $meta['ExifImageWidth'] ."x". $meta['ExifImageLength']);
-      $this->tmpl->assign('ExifFileSize', round($meta['FileSize']/1024, 1));
-      $this->tmpl->assign('image_url', 'phpfspot_img.php?idx='. $photo ."&amp;width=". $this->cfg->photo_width);
-      $this->tmpl->assign('image_url_full', 'phpfspot_img.php?idx='. $photo);
+         $info = getimagesize($thumb_path);
 
 
-      $this->tmpl->assign('tags', $this->get_photo_tags($photo));
+         $this->tmpl->assign('description', $details['description']);
+         $this->tmpl->assign('image_name', $details['name']);
+
+         $this->tmpl->assign('width', $info[0]);
+         $this->tmpl->assign('height', $info[1]);
+         $this->tmpl->assign('ExifMadeOn', strftime("%a %x %X", $meta['FileDateTime']));
+         $this->tmpl->assign('ExifMadeWith', $meta['Make'] ." ". $meta['Model']);
+         $this->tmpl->assign('ExifOrigResolution', $meta['ExifImageWidth'] ."x". $meta['ExifImageLength']);
+         $this->tmpl->assign('ExifFileSize', round($meta['FileSize']/1024, 1));
+    
+         $this->tmpl->assign('image_url', 'phpfspot_img.php?idx='. $photo ."&amp;width=". $this->cfg->photo_width);
+         $this->tmpl->assign('image_url_full', 'phpfspot_img.php?idx='. $photo);
+
+         $this->tmpl->assign('tags', $this->get_photo_tags($photo));
+      }
+      else {
+         print "Can't open file ". $thumb_path ."\n";
+      }
 
       if($previous_img) {
          $this->tmpl->assign('previous_url', "javascript:showImage(". $previous_img .");");
 
       if($previous_img) {
          $this->tmpl->assign('previous_url', "javascript:showImage(". $previous_img .");");
@@ -532,34 +546,50 @@ class PHPFSPOT {
 
    } // check_config_table
 
 
    } // check_config_table
 
+   /**
+    * Generates a thumbnail from photo idx
+    *
+    * This function will generate JPEG thumbnails from provided F-Spot photo
+    * indizes.
+    *
+    * 1. Check if all thumbnail generations (width) are already in place and
+    *    readable
+    * 2. Check if the md5sum of the original file has changed
+    * 3. Generate the thumbnails if needed
+    */
    public function gen_thumb($idx = 0, $fromcmd = 0, $force = 0)
    {
    public function gen_thumb($idx = 0, $fromcmd = 0, $force = 0)
    {
-      $details = $this->get_photo_details($idx);
+      $resolutions = Array(
+         $this->cfg->thumb_width,
+         $this->cfg->bubble_width,
+         $this->cfg->photo_width,
+      );
 
 
+      $details = $this->get_photo_details($idx);
       $full_path = $this->translate_path($details['directory_path'])  ."/". $details['name'];
       $file_md5 = md5_file($full_path);
 
       if($fromcmd) print "Image [". $idx ."] ". $details['name'] ." Thumbnails:";
 
       $full_path = $this->translate_path($details['directory_path'])  ."/". $details['name'];
       $file_md5 = md5_file($full_path);
 
       if($fromcmd) print "Image [". $idx ."] ". $details['name'] ." Thumbnails:";
 
-      /* if the file hasn't changed there is no need to regen the thumb */
-      if(!$force && $file_md5 == $this->getMD5($idx)) {
-         if($fromcmd) print " file has not changed - skipping\n";
-         return;
-      }
-
-      $resolutions = Array(
-         $this->cfg->thumb_width,
-         $this->cfg->bubble_width,
-         $this->cfg->photo_width
-      );
-      
       $error = 0;
 
       $error = 0;
 
-      /* create thumbnails for the requested resolutions */
       foreach($resolutions as $resolution) {
       foreach($resolutions as $resolution) {
-         if($fromcmd) print " ". $resolution ."px";
-         if(!$this->create_thumbnail($full_path, $resolution))
-            $error = 1;
+         $thumb_path = $this->translate_path($details['directory_path'])  ."/thumbs/". $resolution ."_". $details['name'];
+         if(!file_exists($thumb_path)) {
+
+            if($fromcmd) print " ". $resolution ."px";
+            if(!$this->create_thumbnail($full_path, $resolution))
+               $error = 1;
+
+         }
+         /* if the file hasn't changed there is no need to regen the thumb */
+         elseif($file_md5 != $this->getMD5($idx) || $force) {
+
+            if($fromcmd) print " ". $resolution ."px";
+            if(!$this->create_thumbnail($full_path, $resolution))
+               $error = 1;
+
+         }
       }
 
       /* set the new/changed MD5 sum for the current photo */
       }
 
       /* set the new/changed MD5 sum for the current photo */