<?PHP
error_reporting(E_ALL);
class tumb_dir{
	var $geg = array();
	function tumb_dir($path, $add = 'tumb_', $extentions = array('jpg', 'gif', 'png'), $size = 100, $recursive = true){
		$ret = '';
		if(is_dir($path) && !empty($add) && preg_match('#^\/(.*)\/$#is', $path) && is_array($extentions) && is_numeric($size) && is_bool($recursive)){
			$this->geg['path']		= $path;
			$this->geg['max_size']	= $size;
			$this->geg['recursive']	= $recursive;
			$this->geg['add']		= $add;
			foreach($extentions as $value){
				$this->geg['extentions'][]	= strtolower($value);
			}
			foreach(tumb_dir::__dir_array() as $value){
				if(!preg_match('#'.$this->geg['add'].'#i', $value)){
					list($width, $height) = getimagesize($value);
					$width_old	= $width;
					$height_old	= $height;
					if($width > $this->geg['max_size'] || $height > $this->geg['max_size']){
						if($width == $height){
							$width	= $this->geg['max_size'];
							$height	= $this->geg['max_size'];
						}
						elseif($width > $height){
							$height	= ($height/($width/$this->geg['max_size']));
							$width	= $this->geg['max_size'];
						}
						else{
							$width	= ($width/($height/$this->geg['max_size']));
							$height	= $this->geg['max_size'];
						}	
					}
					$new_path	= explode('/', $value);
					$new_path[(count($new_path)-1)] = $this->geg['add'].$new_path[(count($new_path)-1)];
					reset($new_path);
					$new_path	= implode('/', $new_path);
					$img_new	= imagecreatetruecolor($width, $height);
					$img_old	= imagecreatefromjpeg($value);
					imagecopyresampled($img_new, $img_old, 0, 0, 0, 0, $width, $height, $width_old, $height_old);
					imagejpeg($img_new, $new_path, 100);
					imagedestroy($img_new);
					imagedestroy($img_old);
					$ret .= $new_path.' is succesvol aangemaakt!<br />';
				}
			}
		}
		else{
			$ret .= 'De dir ('.$path.') is niet gevonden, kijk of de dir bestaat en dat je een begin en eind \'/\' hebt ingegeven.';
		}
		echo $ret;
	}
	function __dir_array($path = ''){
		if($path == ''){
			$path = $this->geg['path'];
		}
		else{
			$path .= '/';
		}
		$dir = array();
		$dir['array'] = array();
		if(is_dir($path) && $dir['open'] = opendir($path)){
			while(($dir['read'] = readdir($dir['open'])) !== false){
				if($dir['read'] != '.' && $dir['read'] != '..'){
					$dir['extention'] = explode('.', $dir['read']);
					$dir['extention'] = end($dir['extention']);
					if(is_dir($path.$dir['read']) && $this->geg['recursive'] == true){
						$dir['array'] = array_merge($dir['array'], tumb_dir::__dir_array($path.$dir['read']));
					}
					elseif(in_array(strtolower($dir['extention']), $this->geg['extentions'])){
						$dir['array'][] = $path.$dir['read'];
					}
				}
			}
			closedir($dir['open']);
			return $dir['array'];
		}
		else{
			return array();
		}
	}
}
?>
<form method="POST">
  <input type="text" name="dir" size="70" value="<?PHP echo ((isset($_POST['dir'])) ? $_POST['dir'] : $_SERVER['DOCUMENT_ROOT'].'/'); ?>"> (met begin en eind '/')<br />
  <input type="submit" name="tmbn" value="Tumbnailen">
</form>
<hr />
<?PHP
if(isset($_POST['tmbn'])){
	if(!empty($_POST['dir'])){
		$dir = new tumb_dir($_POST['dir']);
	}
	else{
		echo 'Je moet een dir ingeven!';
	}
}
?>