Ich wollte ein PHP-Script haben, das einfach Thumnails erstellen kann. Jedes mal alles neu programmieren war mir zu mühsam. Heraus kam dieses kurze Script.
Benötigte Komponenten (Sind alle am Ende im Zip enthalten)
<?php /** * mpl by ERB software * @author stefan.erb(at)erb-software.com */ /** * @example * Um ein bild nur als Thumb anzuzeigen, kann dieses Script folgendermassen aufgerufen werden * Mit den Standartwerten * http://domain.com/thumb.php?file=/images/meinBild.jpg * Mit einer anderen Grösse * http://domain.com/thumb.php?file=/images/meinBild.jpg&size=150 * * Man kann aber das Thumb auch speichern. Dazu dient der Parameter destfolder * http://domain.com/thumb.php?file=/images/meinBild.jpg&destfolder=/images/thumbs */ /** * Notwendige Packages einbinden */ $thumb_path = dirname(__FILE__); // Sauberes Exception-Handling require "{$thumb_path}/packages/functions/exceptionErrorHandler.php"; // Hilfsfunktion um Pfade zu erstellen require "{$thumb_path}/packages/functions/createPath.php"; // Erweiterte Funktion filter_input_array um 'default' require "{$thumb_path}/packages/functions/filterInputDefaults.php"; // Enum-Klasse require "{$thumb_path}/packages/enum/include.php"; /** * Definitionen */ //Art wie das Bild 'geschnitten' wird class TYPES extends Enum{ public static function FIT(){return new self('fit');} //Proportinen beibehalten. Die längere Seite wird auf die THUMB_SIZE verkleinert public static function STRETCH(){return new self('stretch');} //Das Bild wird auf das Format THUMB_SIZE x THUMB_SIZE verzerrt //public static function CUT(){return new self('cut');} //Noch nicht richtig definiert } //Bildtyp des Thumbs class IMG_TYPES extends enum{ protected $paramNames = array('constant', 'function'); public static function PNG(){return new self('png', IMAGETYPE_PNG, 'imagepng');} public static function JPG(){return new self('jepg', IMAGETYPE_JPEG, 'imagejpeg');} public static function GIF(){return new self('gif', IMAGETYPE_GIF, 'imagegif');} } //Konstanten define('C_THUMB_MIN_SIZE', 20); define('C_THUMB_MAX_SIZE', 200); define('C_THUMB_FILENAME_SUFFIX', '_thumb'); //Standarts defnieren define('C_THUMB_DEFAULT_TYPE', TYPES::FIT()); define('C_THUMB_DEFAULT_SIZE', 100); define('C_THUMB_DEFAULT_IMG_TYPE', IMG_TYPES::PNG()); //GET-Definitionen $C_THUMB_GET_DEFINITION = array( 'file' => 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<!-- Error Trace\n".$e->getTraceAsString()."\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); } } } ?>
Und für alle die gerne eine Weiterleitung mittels .htaccess haben wollen..
#Rewrite Engine einschalten RewriteEngine on #nur Pfade umleiten die keine Datei sind RewriteCond %{REQUEST_FILENAME} !-f #nur Pfade umleiten die kein Ordner sind RewriteCond %{REQUEST_FILENAME} !-d #Schema path/thumb/[$filepath] #Beispiel path/thumb//images/meinBild.jpg #rewrite http://domain.com/thumb.php?file=/images/meinBild.jpg RewriteRule ^thumb\/(.*)$ thumb.php?file=$1 [L,QSA] #Schema path/size/[$size]/thumb/[$filepath] #Beispiel path/size/200/thumb//images/meinBild.jpg #rewrite http://domain.com/thumb.php?file=/images/meinBild.jpg&size=200 RewriteRule ^size\/([[:digit:]]*)\/thumb\/(.*)$ thumb.php?file=$2&size=$1 [L,QSA]