Merge branch 'master' into nefformat
authorAndreas Unterkircher <unki@netshadow.at>
Sat, 29 Mar 2008 08:43:02 +0000 (09:43 +0100)
committerAndreas Unterkircher <unki@netshadow.at>
Sat, 29 Mar 2008 08:43:02 +0000 (09:43 +0100)
1  2 
phpfspot.class.php

diff --combined phpfspot.class.php
index 3f7f7b299289fa3747006e4c3a3b5fc5a29ad40f,be752eedda5fbd7368de4f6d5a08535a2790c3b2..8260cc967ddc21234094f6e76588acb31d272013
@@@ -66,23 -66,31 +66,31 @@@ class PHPFSPOT 
        );
  
        /* Check necessary requirements */
-       if(!$this->checkRequirements()) {
+       if(!$this->check_requirements()) {
           exit(1);
        }
  
-       $this->db  = new PHPFSPOT_DB($this, $this->cfg->fspot_db);
+       /******* Opening F-Spot's sqlite database *********/
+       /* Check if database file is writeable */
        if(!is_writeable($this->cfg->fspot_db)) {
           print $this->cfg->fspot_db ." is not writeable for user ". $this->getuid() ."\n";
           exit(1);
        }
  
-       $this->dbver = $this->getFspotDBVersion();
+       /* open the database */
+       $this->db  = new PHPFSPOT_DB($this, $this->cfg->fspot_db);
  
-       if(!is_writeable(dirname($this->cfg->phpfspot_db))) {
-          print dirname($this->cfg->phpfspot_db) .": directory is not writeable for user ". $this->getuid() ."\n";
-          exit(1);
+       /* change sqlite temp directory, if requested */
+       if(isset($this->cfg->sqlite_temp_dir)) {
+          $this->db->db_exec("
+             PRAGMA
+                temp_store_directory = '". $this->cfg->sqlite_temp_dir ."'
+          ");
        }
  
+       $this->dbver = $this->getFspotDBVersion();
        if(!is_writeable($this->cfg->base_path ."/templates_c")) {
           print $this->cfg->base_path ."/templates_c: directory is not writeable for user ". $this->getuid() ."\n";
           exit(1);
           exit(1);
        }
  
-       $this->cfg_db = new PHPFSPOT_DB($this, $this->cfg->phpfspot_db);
+       /******* Opening phpfspot's sqlite database *********/
+       /* Check if directory where the database file is stored is writeable  */
+       if(!is_writeable(dirname($this->cfg->phpfspot_db))) {
+          print dirname($this->cfg->phpfspot_db) .": directory is not writeable for user ". $this->getuid() ."\n";
+          exit(1);
+       }
+       /* Check if database file is writeable */
        if(!is_writeable($this->cfg->phpfspot_db)) {
           print $this->cfg->phpfspot_db ." is not writeable for user ". $this->getuid() ."\n";
           exit(1);
        }
  
-       $this->check_config_table();
+       /* open the database */
+       $this->cfg_db = new PHPFSPOT_DB($this, $this->cfg->phpfspot_db);
  
-       /* include Smarty template engine */
-       if(!$this->check_readable($this->cfg->smarty_path .'/libs/Smarty.class.php')) {
-          exit(1);
+       /* change sqlite temp directory, if requested */
+       if(isset($this->cfg->sqlite_temp_dir)) {
+          $this->cfg_db->db_exec("
+             PRAGMA
+                temp_store_directory = '". $this->cfg->sqlite_temp_dir ."'
+          ");
        }
-       require $this->cfg->smarty_path .'/libs/Smarty.class.php';
-       /* overload Smarty class if our own template handler */
+       /* Check if some tables need to be created */
+       $this->check_config_table();
+       /* overload Smarty class with our own template handler */
        require_once "phpfspot_tmpl.php";
        $this->tmpl = new PHPFSPOT_TMPL($this);
  
        $max_size = 125; // max font size in %
        $min_size = 75; // min font size in %
  
+       // color
+       $max_sat = hexdec('cc');
+       $min_sat = hexdec('44');
        // get the largest and smallest array values
        $max_qty = max(array_values($tags));
        $min_qty = min(array_values($tags));
        // determine the font-size increment
        // this is the increase per tag quantity (times used)
        $step = ($max_size - $min_size)/($spread);
+       $step_sat = ($max_sat - $min_sat)/($spread);
  
        // loop through our tag array
        foreach ($tags as $key => $value) {
            // uncomment if you want sizes in whole %:
           $size = ceil($size);
  
+          $color = $min_sat + ($value - $min_qty) * $step_sat;
+          $r = '44';
+          $g = dechex($color);
+          $b = '88';
           if(isset($this->tags[$key])) {
-             $output.= "<a href=\"javascript:Tags('add', ". $key .");\" class=\"tag\" style=\"font-size: ". $size ."%;\">". $this->tags[$key] ."</a>, ";
+             $output.= "<a href=\"javascript:Tags('add', ". $key .");\" class=\"tag\" style=\"font-size: ". $size ."%; color: #". $r.$g.$b .";\">". $this->tags[$key] ."</a>, ";
           }
  
        }
              }
  
              $src_img = @imagecreatefromjpeg($orig_image);
 +            $handler = "gd";
              break;
  
           case 'image/png':
  
              $src_img = @imagecreatefrompng($orig_image);
 +            $handler = "gd";
              break;
  
 -      }
 +         case 'image/tiff':
 +
 +            $src_img = new Imagick($orig_image);
 +            print_r($src_img->queryFormats());
 +            
 +            $handler = "imagick";
 +            exit(1);
 +            break;
  
 -      if(!$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);
  
 -      // 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) {
 -         $result = imagejpeg($src_img, $thumb_image, 75);
 -         imagedestroy($src_img);
 -         return true;
 -      }
 +      switch($handler) {
  
 -      // 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 'gd':
  
 -      /* calculates aspect ratio */
 -      $aspect_ratio = $cur_height / $cur_width;
 +            if(!isset($src_img) || empty($src_img)) {
 +               print "Can't load image from ". $orig_image ."\n";
 +               return false;
 +            }
  
 -      /* 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;
 -         }
 -      }
 +            /* grabs the height and width */
 +            $cur_width = imagesx($src_img);
 +            $cur_height = imagesy($src_img);
 +
 +            // 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) {
 +               $result = imagejpeg($src_img, $thumb_image, 75);
 +               imagedestroy($src_img);
 +               return true;
 +            }
  
 -      /* creates new image of that size */
 -      $dst_img = imagecreatetruecolor($new_w, $new_h);
 +            // 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;
 +            }
  
 -      imagefill($dst_img, 0, 0, ImageColorAllocate($dst_img, 255, 255, 255));
 +            /* calculates aspect ratio */
 +            $aspect_ratio = $cur_height / $cur_width;
  
 -      /* copies resized portion of original image into new image */
 -      imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h, imagesx($src_img), imagesy($src_img));
 +            /* 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;
 +               }
 +            }
  
 -      /* needs the image to be flipped horizontal? */
 -      if($flip_hori) {
 -         $this->_debug("(FLIP)");
 -         $dst_img = $this->flipImage($dst_img, 'hori');
 -      }
 -      /* needs the image to be flipped vertical? */
 -      if($flip_vert) {
 -         $this->_debug("(FLIP)");
 -         $dst_img = $this->flipImage($dst_img, 'vert');
 -      }
 +            /* creates new image of that size */
 +            $dst_img = imagecreatetruecolor($new_w, $new_h);
  
 -      if($rotate) {
 -         $this->_debug("(ROTATE)");
 -         $dst_img = $this->rotateImage($dst_img, $rotate);
 -      }
 +            imagefill($dst_img, 0, 0, ImageColorAllocate($dst_img, 255, 255, 255));
  
 -      /* write down new generated file */
 -      $result = imagejpeg($dst_img, $thumb_image, 75);
 +            /* copies resized portion of original image into new image */
 +            imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h, imagesx($src_img), imagesy($src_img));
  
 -      /* free your mind */
 -      imagedestroy($dst_img);
 -      imagedestroy($src_img);
 +            /* needs the image to be flipped horizontal? */
 +            if($flip_hori) {
 +               $this->_debug("(FLIP)");
 +               $dst_img = $this->flipImage($dst_img, 'hori');
 +            }
 +            /* needs the image to be flipped vertical? */
 +            if($flip_vert) {
 +               $this->_debug("(FLIP)");
 +               $dst_img = $this->flipImage($dst_img, 'vert');
 +            }
  
 -      if($result === false) {
 -         print "Can't write thumbnail ". $thumb_image ."\n";
 -         return false;
 -      }
 +            if($rotate) {
 +               $this->_debug("(ROTATE)");
 +               $dst_img = $this->rotateImage($dst_img, $rotate);
 +            }
  
 -      return true;
 +            /* write down new generated file */
 +            $result = imagejpeg($dst_img, $thumb_image, 75);
 +
 +            /* free your mind */
 +            imagedestroy($dst_img);
 +            imagedestroy($src_img);
 +
 +            if($result === false) {
 +               print "Can't write thumbnail ". $thumb_image ."\n";
 +               return false;
 +            }
 +
 +            return true;
 +
 +            break;
 +
 +         case 'imagick':
 +
 +            break;
 +
 +      }
  
     } // create_thumbnail()
  
     /**
      * check if all requirements are met
      */
-    private function checkRequirements()
+    private function check_requirements()
     {
        if(!function_exists("imagecreatefromjpeg")) {
           print "PHP GD library extension is missing<br />\n";
           print "PEAR Console_Getopt package is missing<br />\n";
           $missing = true;
        }
+       @include_once $this->cfg->smarty_path .'/libs/Smarty.class.php';
+       if(isset($php_errormsg) && preg_match('/Failed opening.*for inclusion/i', $php_errormsg)) {
+          print "Smarty template engine can not be found in ". $this->cfg->smarty_path ."/libs/Smarty.class.php<br />\n";
+          $missing = true;
+       }
        ini_restore('track_errors');
  
        if(isset($missing))
  
        return true;
  
-    } // checkRequirements()
+    } // check_requirements()
  
     private function _debug($text)
     {
      */
     public function checkifImageSupported($mime)
     {
 -      if(in_array($mime, Array("image/jpeg", "image/png")))
 +      if(in_array($mime, Array("image/jpeg", "image/png", "image/tiff")))
           return true;
  
        return false;