From: avl Date: Thu, 11 Sep 2008 00:42:13 +0000 (+0300) Subject: Image Thumbnailer X-Git-Tag: 0.5~496 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=7f1bc42acb81f4fd814e86e3d535a2af223680fe;p=ipf.git Image Thumbnailer --- diff --git a/ipf.php b/ipf.php index a1bdebf..dbf2389 100644 --- 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 index 0000000..907545c --- /dev/null +++ b/ipf/exception/image.php @@ -0,0 +1,3 @@ +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)); + } + } +} +?> diff --git a/ipf/orm/import/builder.php b/ipf/orm/import/builder.php index 542594e..9f72a89 100644 --- a/ipf/orm/import/builder.php +++ b/ipf/orm/import/builder.php @@ -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; } diff --git a/ipf/orm/utils.php b/ipf/orm/utils.php index 5664c70..5b2afc6 100644 --- a/ipf/orm/utils.php +++ b/ipf/orm/utils.php @@ -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[] = "
";
diff --git a/ipf/utils.php b/ipf/utils.php
index 94305ec..12a0642 100644
--- a/ipf/utils.php
+++ b/ipf/utils.php
@@ -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;
+    }
 }