FILTER_SANITIZE_STRING, 'size' => array( 'filter' => FILTER_VALIDATE_INT, 'options' => array( 'min_range' => C_THUMB_MIN_SIZE, 'max_range' => C_THUMB_MAX_SIZE), 'default' => C_THUMB_DEFAULT_SIZE), 'type' => array( 'filter' => FILTER_VALIDATE_REGEXP, 'options' => array( 'regexp' => '/^(?:'.TYPES::implode('|').')$/'), 'default' => C_THUMB_DEFAULT_TYPE), 'imgtype' => array( 'filter' => FILTER_VALIDATE_REGEXP, 'options' => array( 'regexp' => '/^(?:'.IMG_TYPES::implode('|').')$/'), 'default' => C_THUMB_DEFAULT_IMG_TYPE), 'destfolder' => array( 'filter' => FILTER_SANITIZE_STRING, 'default' => FALSE) ); /** * Ausführender Code */ try{ Thumb::createThumb(); }catch(Exception $e){ echo nl2br($e->getMessage()); echo "\n"; } /** * Klasse Thumb */ class Thumb{ /** * Singel-Entry * @static */ public static function createThumb(IMG_TYPES $imgType = NULL){ $thumb = new self(); $thumb->createImage($imgType); } /** * Konstrukteur */ public function __construct(){ //$_GET auswerten $params = (object) filterInputDefaults(INPUT_GET, $GLOBALS['C_THUMB_GET_DEFINITION'] ); //Detailierte Überprüfungen der Parameter //if(!file_exists($params->file)) throw new Exception("File '{$params->file}' dosnt exist"); if($params->size==FALSE) throw new Exception("Size not between ".C_THUMB_MIN_SIZE." AND ".C_THUMB_MAX_SIZE); if($params->type==FALSE) throw new Exception("invalid Type '{$params->type}'. Valid Types are ".TYPES::implode(', ')); //Klassenvariablen bescrheiben $this->imgType = IMG_TYPES::getEnum($params->imgtype); $this->destFolder = $params->destfolder; //Grössen und Faktoren berechnen $info = getimagesize($this->path = $params->file); $this->srcH = $info[1]; $this->srcW = $info[0]; switch ($params->type){ case TYPES::FIT(): $this->factorH = $this->factorW = $params->size/max($this->srcH, $this->srcW); $this->h = $this->srcH * $this->factorH; $this->w = $this->srcW * $this->factorW; break; case TYPES::STRETCH(): $this->h = $this->w =$params->size; $this->factorH = $this->srcH/$this->h; $this->factorW = $this->srcW/$this->w; break; case TYPES::CUT(): $this->factorH = $this->factorW = $params->size/min($this->srcH, $this->srcW); $this->h = $this->w =$params->size; break; } } /** * Erstellt das Thumb-image * @param $imgType */ public function createImage(IMG_TYPES $imgType = NULL, $destFolder = FALSE){ //Parameters überprüfen und ggf aus de Klasse laden if(is_null($imgType)) $imgType = $this->imgType; if($destFolder == FALSE) $destFolder = $this->destFolder; //Thumb-Image erstellen $image = $this->getImage(); //imgecreate-Funktion ermitteln $function = $imgType->function; //Thumb-Dateiname ermitteln $fileName = $this->getThumbFileName(); //Falls ein Destinationsordner angegen wurde, die Datei speichern if($this->destFolder != FALSE){ $function($image, createPath($this->destFolder, $fileName)); //Anderfalls an den Browser ausgeben }else{ header ('Content-type: '.image_type_to_mime_type($imgType->constant)); header("Content-disposition: inline; filename=\"{$fileName}\); $function($image); } //Aufräumen imagedestroy($image); } /** * kopiert das Image in das Thumb-Image */ public function getImage(){ $srcImg = &$this->getSource(); $image = imagecreatetruecolor($this->w, $this->h); imagecopyresized($image, $srcImg, 0, 0, 0, 0, $this->w, $this->h, $this->srcW, $this->srcH); imagedestroy($srcImg); return $image; } /** * erstellt den Namen der Thumb-Datei */ protected function getThumbFileName(){ $info = pathinfo($this->path); $pattern = "/\.{$info['extension']}$/"; return preg_replace($pattern, '', $info['basename']) . C_THUMB_FILENAME_SUFFIX .image_type_to_extension($imgType->constant); } /** * öffnet die Quellgrafik und gibt die Ressource zurück */ protected function getSource(){ //Auswertung des Imagetypes -> http://ch.php.net/manual/de/function.image-type-to-mime-type.php switch(exif_imagetype($this->path)){ case IMAGETYPE_PNG: return imagecreatefrompng($this->path); case IMAGETYPE_GIF: return imagecreatefromgif($this->path); case IMAGETYPE_JPEG: return imagecreatefromjpeg($this->path); } } } ?>