issue110, handle Nikons NEF RAW photos
authorAndreas Unterkircher <unki@netshadow.at>
Sat, 12 Apr 2008 19:12:42 +0000 (21:12 +0200)
committerAndreas Unterkircher <unki@netshadow.at>
Sat, 12 Apr 2008 19:12:42 +0000 (21:12 +0200)
Signed-off-by: Andreas Unterkircher <unki@netshadow.at>
phpfspot.class.php
phpfspot_cfg.php.dist
phpfspot_img.php

index eb2312bf709a9b5ff0302d4538fa18480e6c2a1b..0615cff77a5d7742cc29461b42cf2cf4e8c54b13 100644 (file)
@@ -1337,7 +1337,7 @@ class PHPFSPOT {
    } // showCredits()
 
    /**
-    * create_thumbnails for the requested width
+    * create thumbnails for the requested width
     *
     * this function creates image thumbnails of $orig_image
     * stored as $thumb_image. It will check if the image is
@@ -1354,13 +1354,13 @@ class PHPFSPOT {
          return false;
       }
 
-      $details = getimagesize($orig_image);
-      
+      $mime = $this->get_mime_info($orig_image);
+
       /* check if original photo is a support image type */
-      if(!$this->checkifImageSupported($details['mime']))
+      if(!$this->checkifImageSupported($mime))
          return false;
 
-      switch($details['mime']) {
+      switch($mime) {
 
          case 'image/jpeg':
 
@@ -1399,27 +1399,23 @@ class PHPFSPOT {
             $handler = "gd";
             break;
 
-         case 'image/tiff':
+         case 'image/x-portable-pixmap':
 
             $src_img = new Imagick($orig_image);
-            print_r($src_img->queryFormats());
-            
             $handler = "imagick";
-            exit(1);
             break;
 
       }
 
+      if(!isset($src_img) || empty($src_img)) {
+         print "Can't load image from ". $orig_image ."\n";
+         return false;
+      }
 
       switch($handler) {
 
          case 'gd':
 
-            if(!isset($src_img) || empty($src_img)) {
-               print "Can't load image from ". $orig_image ."\n";
-               return false;
-            }
-
             /* grabs the height and width */
             $cur_width = imagesx($src_img);
             $cur_height = imagesy($src_img);
@@ -1433,45 +1429,71 @@ class PHPFSPOT {
                imagedestroy($src_img);
                return true;
             }
+            break;
 
-            // If the image will be rotate because EXIF orientation said so
-            // 'virtually rotate' the image for further calculations
-            if($rotate == 90 || $rotate == 270) {
-               $tmp = $cur_width;
-               $cur_width = $cur_height;
-               $cur_height = $tmp;
-            }
+         case 'imagick':
 
-            /* calculates aspect ratio */
-            $aspect_ratio = $cur_height / $cur_width;
+            $cur_width = $src_img->getImageWidth();
+            $cur_height = $src_img->getImageHeight();
 
-            /* sets new size */
-            if($aspect_ratio < 1) {
-               $new_w = $width;
-               $new_h = abs($new_w * $aspect_ratio);
-            } else {
-               /* 'virtually' rotate the image and calculate it's ratio */
-               $tmp_w = $cur_height;
-               $tmp_h = $cur_width;
-               /* now get the ratio from the 'rotated' image */
-               $tmp_ratio = $tmp_h/$tmp_w;
-               /* now calculate the new dimensions */
-               $tmp_w = $width;
-               $tmp_h = abs($tmp_w * $tmp_ratio);
-
-               // now that we know, how high they photo should be, if it
-               // gets rotated, use this high to scale the image
-               $new_h = $tmp_h;
-               $new_w = abs($new_h / $aspect_ratio);
-
-               // If the image will be rotate because EXIF orientation said so
-               // now 'virtually rotate' back the image for the image manipulation
-               if($rotate == 90 || $rotate == 270) {
-                  $tmp = $new_w;
-                  $new_w = $new_h;
-                  $new_h = $tmp;
-               }
+            // If requested width is more then the actual image width,
+            // do not generate a thumbnail, instead safe the original
+            // as thumbnail but with lower quality. But if the image
+            // is to heigh too, then we still have to resize it.
+            if($width >= $cur_width && $cur_height < $this->cfg->thumb_height) {
+               $src_img->setCompressionQuality(75);
+               $src_img->setImageFormat('jpeg');
+               $src_img->writeImage($thumb_image);
+               $src_img->clear();
+               $src_img->destroy();
+               return true;
             }
+            break;
+
+      }
+
+      // If the image will be rotate because EXIF orientation said so
+      // 'virtually rotate' the image for further calculations
+      if($rotate == 90 || $rotate == 270) {
+         $tmp = $cur_width;
+         $cur_width = $cur_height;
+         $cur_height = $tmp;
+      }
+
+      /* calculates aspect ratio */
+      $aspect_ratio = $cur_height / $cur_width;
+
+      /* sets new size */
+      if($aspect_ratio < 1) {
+         $new_w = $width;
+         $new_h = abs($new_w * $aspect_ratio);
+      } else {
+         /* 'virtually' rotate the image and calculate it's ratio */
+         $tmp_w = $cur_height;
+         $tmp_h = $cur_width;
+         /* now get the ratio from the 'rotated' image */
+         $tmp_ratio = $tmp_h/$tmp_w;
+         /* now calculate the new dimensions */
+         $tmp_w = $width;
+         $tmp_h = abs($tmp_w * $tmp_ratio);
+
+         // now that we know, how high they photo should be, if it
+         // gets rotated, use this high to scale the image
+         $new_h = $tmp_h;
+         $new_w = abs($new_h / $aspect_ratio);
+
+         // If the image will be rotate because EXIF orientation said so
+         // now 'virtually rotate' back the image for the image manipulation
+         if($rotate == 90 || $rotate == 270) {
+            $tmp = $new_w;
+            $new_w = $new_h;
+            $new_h = $tmp;
+         }
+      }
+
+      switch($handler) {
+
+         case 'gd':
 
             /* creates new image of that size */
             $dst_img = imagecreatetruecolor($new_w, $new_h);
@@ -1515,6 +1537,38 @@ class PHPFSPOT {
 
          case 'imagick':
 
+            $src_img->resizeImage($new_w, $new_h, Imagick::FILTER_LANCZOS, 1);
+
+            /* needs the image to be flipped horizontal? */
+            if($flip_hori) {
+               $this->_debug("(FLIP)");
+               $src_img->rotateImage(new ImagickPixel(), 90);
+               $src_img->flipImage();
+               $src_img->rotateImage(new ImagickPixel(), -90);
+            }
+            /* needs the image to be flipped vertical? */
+            if($flip_vert) {
+               $this->_debug("(FLIP)");
+               $src_img->flipImage();
+            }
+
+            if($rotate) {
+               $this->_debug("(ROTATE)");
+               $src_img->rotateImage(new ImagickPixel(), $rotate);
+            }
+
+            $src_img->setCompressionQuality(75);
+            $src_img->setImageFormat('jpeg');
+
+            if(!$src_img->writeImage($thumb_image)) {
+               print "Can't write thumbnail ". $thumb_image ."\n";
+               return false;
+            }
+
+            $src_img->clear();
+            $src_img->destroy();
+            return true;
+
             break;
 
       }
@@ -1593,10 +1647,27 @@ class PHPFSPOT {
          return;
       }
 
-      $file_md5 = md5_file($full_path);
-
       $this->_debug("Image [". $idx ."] ". $this->shrink_text($this->parse_uri($details['uri'], 'filename'), 20) ." Thumbnails:");
 
+      /* If Nikon NEF format, we need to treat it another way */
+      if(isset($this->cfg->dcraw_bin) &&
+         file_exists($this->cfg->dcraw_bin) &&
+         is_executable($this->cfg->dcraw_bin) &&
+         preg_match('/\.nef$/i', $details['uri'])) {
+
+         $ppm_path = preg_replace('/\.nef$/i', '.ppm', $full_path);
+
+         /* if PPM file does not exist, let dcraw convert it from NEF */
+         if(!file_exists($ppm_path)) {
+            system($this->cfg->dcraw_bin ." -a ". $full_path);
+         }
+
+         /* for now we handle the PPM instead of the NEF */
+         $full_path = $ppm_path;
+
+      }
+
+      $file_md5 = md5_file($full_path);
       $changes = false;
 
       foreach($resolutions as $resolution) {
@@ -2002,7 +2073,14 @@ class PHPFSPOT {
     */
    public function checkifImageSupported($mime)
    {
-      if(in_array($mime, Array("image/jpeg", "image/png", "image/tiff")))
+      $supported_types =  Array(
+         "image/jpeg",
+         "image/png",
+         "image/x-portable-pixmap",
+         "image/tiff"
+      );
+
+      if(in_array($mime, $supported_types))
          return true;
 
       return false;
@@ -2819,6 +2897,33 @@ class PHPFSPOT {
 
    } // getCurrentPage()
 
+   /**
+    * return mime info
+    *
+    * this function tries to find out the correct mime-type
+    * for the provided file.
+    * @param string $file
+    * @return string
+    */
+   public function get_mime_info($file)
+   {
+      $details = getimagesize($orig_image);
+
+      /* if getimagesize() returns empty, try at least to find out the
+         mime type.
+      */
+      if(empty($details) && function_exists('mime_content_type')) {
+
+         // mime_content_type is marked as deprecated in the documentation,
+         // but is it really necessary to force users to install a PECL
+         // extension?
+         $details['mime'] = mime_content_type($file);
+      }
+
+      return $details['mime'];
+
+   } // get_mime_info()
+
 } // class PHPFSPOT
 
 ?>
index 3e3a802b14572c7a769840bf29c53870bbc756d3..c792d062ba29ee645e998922da4e27e91a552019 100644 (file)
@@ -113,6 +113,12 @@ class PHPFSPOT_CFG {
    /* no need to modified anything below this line */
    var $error_found = 0;
 
+   /* path to dcraw to decode Nikon's NEF format */
+   // var $dcraw_bin = "/usr/bin/dcraw";
+
+   /* path to cjpeg to create JPEG from dcraw's PPM output */
+   // var $cjpeg_bin = "/usr/bin/cjpeg";
+
    public function __construct()
    {
 
index f3eb3088f4b363030ccb11b88b7c4a080768dd58..bc8d3d11badc4b22067eba044e8c15deaf50c8f4 100644 (file)
@@ -100,10 +100,8 @@ class PHPFSPOT_IMG {
       if(!is_readable($fullpath)) {
          $this->parent->showTextImage("File ". basename($fullpath) ." is not readable. Check the permissions");
          return;
-      }
-
-      $tmp = getimagesize($fullpath);
-      $mime = $tmp['mime'];
+      } 
+      $mime = $this->parent->get_mime_info($fullpath);
 
       if(!$this->parent->checkifImageSupported($mime)) {
          $this->parent->showTextImage("Unsupported Image Type");