AN INSTRUMENTAL EXPERIMENTAL

Files

PHP View: MyDirectoryIterator.php
<?php
// http://layer.php5.sk/?Q=mdi

class MyDirectoryIterator extends DirectoryIterator {
    
    public 
$imageInfo;
    public 
$thumbCache;// chmod 777
    
public $thumbRes;// image resolution of thumbnails
    
public $thumbQuality;// quality parameter of jpeg
    
public $log = array();
    
    public function 
__construct($path$thumbCache './cache/'$thumbRes = array('width' => 128,'height' => 96), $thumbQuality 50) {
        if (
extension_loaded("gd")) {
            
parent::__construct($path);
            
$this->thumbCache $thumbCache;
            
$this->thumbRes $thumbRes;
            
$this->thumbQuality $thumbQuality;
        } else {
            
$this->log[] = 'Extension "gd" not loaded. MyDirectoryIterator failed.';
            exit;
        }
    }
    
    public function 
getFormatSize(){
        
$size $this->getSize();
        if(
$size == 0){ return("0 Bytes"); }
        
$acronym = array(" B "," KB"," MB"," GB"," TB"," PB"," EB"," ZB"," YB");
        return 
round($size/pow(1024,($i=floor(log($size,1024)))),2).$acronym[$i];
    }
    
    public function 
getOctalPerms(){
        return 
substr(sprintf('%o'$this->getPerms()), -4);
    }
    
    public function 
getFullPerms(){
        
$perms $this->getPerms();
        if ((
$perms 0xC000) == 0xC000) {
            
$info 's'// Socket
        
} elseif (($perms 0xA000) == 0xA000) {
            
$info 'l'// Symbolic Link
        
} elseif (($perms 0x8000) == 0x8000) {
            
$info '-'// Regular
        
} elseif (($perms 0x6000) == 0x6000) {
            
$info 'b'// Block special
        
} elseif (($perms 0x4000) == 0x4000) {
            
$info 'd'// Directory
        
} elseif (($perms 0x2000) == 0x2000) {
            
$info 'c'// Character special
        
} elseif (($perms 0x1000) == 0x1000) {
            
$info 'p'// FIFO pipe
        
} else {
            
$info 'u'// Unknown
        
}
        
// Owner
        
$info .= (($perms 0x0100) ? 'r' '-');
        
$info .= (($perms 0x0080) ? 'w' '-');
        
$info .= (($perms 0x0040) ? (($perms 0x0800) ? 's' 'x' ) : (($perms 0x0800) ? 'S' '-'));
        
// Group
        
$info .= (($perms 0x0020) ? 'r' '-');
        
$info .= (($perms 0x0010) ? 'w' '-');
        
$info .= (($perms 0x0008) ? (($perms 0x0400) ? 's' 'x' ) : (($perms 0x0400) ? 'S' '-'));
        
// World
        
$info .= (($perms 0x0004) ? 'r' '-');
        
$info .= (($perms 0x0002) ? 'w' '-');
        
$info .= (($perms 0x0001) ? (($perms 0x0200) ? 't' 'x' ) : (($perms 0x0200) ? 'T' '-'));
        
        return 
$info;
    }
    
    public function 
getLog() {
        
$Dump "<pre>\n";
        
$Dump.= print_r($this->logTRUE);
        
$Dump.= "</pre>\n";
        return 
$Dump;
    }
    
    public function 
isImage(){
        if (
$this->imageInfo getimagesize($this->getPathname())) {
            return 
true;
        } else {
            return 
false;
        } 
    }
    
    
    public function 
getThumbnail(){
        
$thumbPathname $this->thumbCache.md5($this->current());
        if (
file_exists($thumbPathname)) {
            
$this->log[$this->getBasename()] = $this->getFullPerms().' '.$this->getOctalPerms().' Thumbnail already exist.';
            return 
$thumbPathname;
        } else {
            if (
$this->imageInfo) {
                
//$this->log[$this->getBasename()] = $this->imageInfo;
                
switch($this->imageInfo[2]){
                    case
'1'$imageResource imagecreatefromgif($this->getPathname()); break;
                    case
'2'$imageResource imagecreatefromjpeg($this->getPathname()); break;
                    case
'3'$imageResource imagecreatefrompng($this->getPathname()); break;
                    case
'15'$imageResource imagecreatefromwbmp($this->getPathname()); break;
                    case
'16'$imageResource imagecreatefromxbm($this->getPathname()); break;
                    default: 
$this->log[] = 'Create ImageResource failed.';
                        return 
false;
                }
                if (
$this->imageInfo[0] > $this->imageInfo[1]) {
                    
$Coefficient $this->thumbRes['width'] / $this->imageInfo[0];
                } else {
                    
$Coefficient $this->thumbRes['height'] / $this->imageInfo[1];
                }
                
$thumbWidth round($this->imageInfo[0] * $Coefficient);
                
$thumbHeight round($this->imageInfo[1] * $Coefficient);
                
$thumbResource imagecreatetruecolor($thumbWidth$thumbHeight);
                
imagecopyresampled($thumbResource$imageResource0000$thumbWidth$thumbHeight$this->imageInfo[0], $this->imageInfo[1]);
                if(
imagejpeg($thumbResource$thumbPathname$this->thumbQuality)) {
                    
$this->log[$this->getBasename()] = 'Thumbnail Succeed.';
                    return 
$thumbPathname;
                } else {
                    
$this->log[] = 'Create Imagejpeg failed. No space left on the device or insufficient privileges?';
                    return 
false;
                }
            } else {
                
$this->log[] = 'ImageInfo failed.';
                return 
false;
            }
        }
    }
}

$iterator = new MyDirectoryIterator('./files/StarTrek/');
print 
'<div class="article"><h2>Directory: '.$iterator->getPath().'</h2>'."\n";
foreach (
$iterator as $item) {
    if(
$item->isImage()){
        print 
'<a href="'.$item->getPathname().'"><img src="'.$item->getThumbnail().'" alt="'.$item->current().'" title="'.$item->current().' '.$item->getFormatSize().'" /></a>'."\n";
    }
}
print 
$iterator->getLog().'</div>';
?>