]> git.andy128k.dev Git - ipf.git/commitdiff
Image Thumbnailer
authoravl <alex.litovchenko@gmail.com>
Thu, 11 Sep 2008 00:42:13 +0000 (03:42 +0300)
committeravl <alex.litovchenko@gmail.com>
Thu, 11 Sep 2008 00:42:13 +0000 (03:42 +0300)
ipf.php
ipf/exception/image.php [new file with mode: 0644]
ipf/image/thumbnail.php [new file with mode: 0644]
ipf/orm/import/builder.php
ipf/orm/utils.php
ipf/utils.php

diff --git a/ipf.php b/ipf.php
index a1bdebf88201433e9ef9a56d532a155005a9bcc9..dbf2389a8f940f17cbec2c621c3e6dcf09984011 100644 (file)
--- a/ipf.php
+++ b/ipf.php
@@ -91,7 +91,13 @@ final class IPF{
         if (!isset(IPF::$settings['session_cookie_id'])){
             IPF::$settings['session_cookie_id'] = 'sessionid';
         }
-        
+
+        if (!isset(IPF::$settings['dir_permission']))
+            IPF::$settings['dir_permission'] = 0770;
+
+        if (!isset(IPF::$settings['file_permission']))
+            IPF::$settings['file_permission'] = 0660;
+
         //print_r(IPF::$settings);
     }
 
diff --git a/ipf/exception/image.php b/ipf/exception/image.php
new file mode 100644 (file)
index 0000000..907545c
--- /dev/null
@@ -0,0 +1,3 @@
+<?php
+
+class IPF_Exception_Image extends IPF_Exception{}
diff --git a/ipf/image/thumbnail.php b/ipf/image/thumbnail.php
new file mode 100644 (file)
index 0000000..bb99363
--- /dev/null
@@ -0,0 +1,111 @@
+<?
+
+class IPF_Image_Thumbnail {
+    
+    protected $Source, $Thumbnail;
+    protected $ThumbnailWidth, $ThumbnailHeight;
+    protected $SourceWidth, $SourceHeight, $SourceType;
+    protected $file_permission, $dir_permission;
+        
+    public function __construct($source, $thumnbail, $width=null, $height=null, $dir_permission=null, $file_permission=null){
+        $this->Source = $source;
+        $this->Thumbnail = $thumbnail;
+
+        if (($width==null) && ($height==null))
+            throw new IPF_Exception_Image(__('Please Specify width or height'));
+
+        $this->ThumbnailWidth = $width;
+        $this->ThumbnailHeight = $height;
+        
+        if ($dir_permission)
+            $this->dir_permission = $dir_permission;
+        else
+            $this->dir_permission = IPF::get('dir_permission');
+
+        if ($file_permission)
+            $this->file_permission = $file_permission;
+        else
+            $this->file_permission = IPF::get('file_permission');
+    }
+        
+    public function execute(){
+        $ImageInfo = getimagesize($this->Source);
+        if(!$ImageInfo)
+            throw new IPF_Exception_Image(sprintf(__('Cannot open %s image file'), $this->Source));
+            
+        $this->SourceWidth = $ImageInfo[0];
+        $this->SourceHeight = $ImageInfo[1];
+        $this->SourceType = $ImageInfo[2];
+    
+        if($this->SourceType==IMAGETYPE_JPEG)
+            $im = ImageCreateFromJPEG($this->Source);
+        else if($this->SourceType==IMAGETYPE_GIF)
+            $im = ImageCreateFromGIF($this->Source);
+        else if($this->SourceType==IMAGETYPE_PNG)
+            $im = ImageCreateFromPNG($this->Source);
+        else
+            throw new IPF_Exception_Image(sprintf(__('Unknown image format %s'), $this->Source));
+
+        if($this->ThumbnailWidth)
+            $c1 = $this->SourceWidth/abs($this->ThumbnailWidth);
+        else
+            $c1 = 0;
+      
+        if($this->ThumbnailHeight)
+            $c2 = $this->SourceHeight/abs($this->ThumbnailHeight);
+        else
+            $c2 = 0;
+      
+        $c = $c1>$c2 ? $c1 : $c2;
+      
+        if($c<=1){
+            $this->ThumbnailWidth = $this->SourceWidth;
+            $this->ThumbnailHeight = $this->SourceHeight;
+            if($this->Source<>$this->Thumbnail)
+                if (!@copy($this->Source, $this->Thumbnail))
+                    throw new IPF_Exception_Image(sprintf(__('Cannot copy %s to %s'), $this->Source, $this->Thumbnail));
+        }
+        else{
+            if($this->ThumbnailWidth<0 and $this->SourceWidth/$c<(-$this->ThumbnailWidth))
+              $c = $this->SourceWidth/(-$this->ThumbnailWidth);
+            if($this->ThumbnailHeight<0 and $this->SourceHeight/$c<(-$this->ThumbnailHeight))
+              $c = $this->SourceHeight/(-$this->ThumbnailHeight);
+            
+            $this->ThumbnailWidth = $this->SourceWidth/$c;
+            $this->ThumbnailHeight = $this->SourceHeight/$c;
+        
+            $tn = imagecreatetruecolor($this->ThumbnailWidth, $this->ThumbnailHeight);
+            imagecopyresampled(
+                $tn, $im, 0, 0, 0, 0, 
+                $this->ThumbnailWidth, $this->ThumbnailHeight, 
+                $this->SourceWidth, $this->SourceHeight
+            );
+
+            if (!@unlink($this->Thumbnail))
+                throw new IPF_Exception_Image(sprintf(__('Cannot delete %s'), $this->Thumbnail));
+
+            $dir_thumbnail = dirName($this->Thumbnail);
+            if (!IPF_Utils::makeDirectories(dirName($this->Thumbnail), $this->dir_permission))
+                throw new IPF_Exception_Image(sprintf(__('Cannot create path %s'), $dir_thumbnail));
+            
+            if($this->SourceType==IMAGETYPE_JPEG){
+                if (!ImageJPEG($tn, $this->Thumbnail))
+                    throw new IPF_Exception_Image(sprintf(__('Cannot create JPEG %s'), $this->Thumbnail));
+            }
+            else if($this->SourceType==IMAGETYPE_GIF){
+                if (!ImageGIF($tn, $this->Thumbnail))
+                    throw new IPF_Exception_Image(sprintf(__('Cannot create GIF %s'), $this->Thumbnail));
+            }
+            else if($this->SourceType==IMAGETYPE_PNG){
+                if (!ImagePNG($tn, $this->Thumbnail))
+                    throw new IPF_Exception_Image(sprintf(__('Cannot create PNG %s'), $this->Thumbnail));
+            }
+            else
+                throw new IPF_Exception_Image(sprintf(__('Unknown image format %s'), $this->Source));
+
+            if (!@chmod($this->Thumbnail, $this->file_permission))
+                throw new IPF_Exception_Image(sprintf(__('Cannot change permission %s'), $this->Thumbnail));
+        }
+    }
+}
+?>
index 542594e442a7464c8daa5110b72fea6d3d522984..9f72a894ce85a2eab9974942c056f746b4c74ad1 100644 (file)
@@ -661,7 +661,7 @@ class IPF_ORM_Import_Builder extends IPF_ORM_Builder
                                        null
                                        );
 
-        IPF_ORM_Utils::makeDirectories($path);
+        IPF_Utils::makeDirectories($path);
 
         $writePath = $path . DIRECTORY_SEPARATOR . $className . $this->_suffix;
 
@@ -716,12 +716,12 @@ class IPF_ORM_Import_Builder extends IPF_ORM_Builder
 
         // If we have a writePath from the if else conditionals above then use it
         if (isset($writePath)) {
-            IPF_ORM_Utils::makeDirectories($writePath);
+            IPF_Utils::makeDirectories($writePath);
 
             $writePath .= DIRECTORY_SEPARATOR . $fileName;
         // Otherwise none of the conditions were met and we aren't generating base classes
         } else {
-            IPF_ORM_Utils::makeDirectories($this->_path);
+            IPF_Utils::makeDirectories($this->_path);
 
             $writePath = $this->_path . DIRECTORY_SEPARATOR . $fileName;
         }
index 5664c70d1ce711743b28dba263e15a70c6a83fb5..5b2afc6e2d4b4c6262873320dfaef98b6fd3506b 100644 (file)
@@ -103,75 +103,6 @@ class IPF_ORM_Utils {
         }
     }        
 
-    public static function makeDirectories($path, $mode = 0777)
-    {
-        if ( ! $path) {
-          return false;
-        }
-
-        if (is_dir($path) || is_file($path)) {
-          return true;
-        }
-
-        return mkdir(trim($path), $mode, true);
-    }
-
-    public static function removeDirectories($folderPath)
-    {
-        if (is_dir($folderPath))
-        {
-            foreach (scandir($folderPath) as $value)
-            {
-                if ($value != '.' && $value != '..')
-                {
-                    $value = $folderPath . "/" . $value;
-
-                    if (is_dir($value)) {
-                        self::removeDirectories($value);
-                    } else if (is_file($value)) {
-                        unlink($value);
-                    }
-                }
-            }
-
-            return rmdir($folderPath);
-        } else {
-            return false;
-        }
-    }
-
-    public static function copyDirectory($source, $dest)
-    {
-        // Simple copy for a file
-        if (is_file($source)) {
-            return copy($source, $dest);
-        }
-
-        // Make destination directory
-        if ( ! is_dir($dest)) {
-            mkdir($dest);
-        }
-
-        // Loop through the folder
-        $dir = dir($source);
-        while (false !== $entry = $dir->read()) {
-            // Skip pointers
-            if ($entry == '.' || $entry == '..') {
-                continue;
-            }
-
-            // Deep copy directories
-            if ($dest !== "$source/$entry") {
-                self::copyDirectory("$source/$entry", "$dest/$entry");
-            }
-        }
-
-        // Clean up
-        $dir->close();
-
-        return true;
-    }
-
     public static function getTableAsString(IPF_ORM_Table $table)
     {
         $r[] = "<pre>";
index 94305ec0adee5c5c1e6dddf747a9ac08e2b80f90..12a0642158576946b2445349925a66a9f6b2885a 100644 (file)
@@ -118,5 +118,58 @@ class IPF_Utils {
         }
         return '';
     }
+    
+    public static function makeDirectories($path, $mode = 0777){
+        if ( ! $path) {
+            return false;
+        }
+        if (is_dir($path) || is_file($path)) {
+            return true;
+        }
+        return mkdir(trim($path), $mode, true);
+    }
+    
+    public static function removeDirectories($folderPath){
+        if (is_dir($folderPath)){
+            foreach (scandir($folderPath) as $value){
+                if ($value != '.' && $value != '..'){
+                    $value = $folderPath . "/" . $value;
+                    if (is_dir($value)) {
+                        self::removeDirectories($value);
+                    } else if (is_file($value)) {
+                        unlink($value);
+                    }
+                }
+            }
+            return rmdir($folderPath);
+        } else {
+            return false;
+        }
+    }
+
+    public static function copyDirectory($source, $dest){
+        // Simple copy for a file
+        if (is_file($source)) {
+            return copy($source, $dest);
+        }
+        // Make destination directory
+        if (!is_dir($dest)) {
+            mkdir($dest);
+        }
+        // Loop through the folder
+        $dir = dir($source);
+        while (false !== $entry = $dir->read()){
+            // Skip pointers
+            if ($entry == '.' || $entry == '..') {
+                continue;
+            }
+            // Deep copy directories
+            if ($dest !== "$source/$entry") {
+                self::copyDirectory("$source/$entry", "$dest/$entry");
+            }
+        }
+        $dir->close();
+        return true;
+    }
 }