======[PHP] Thumb======
----
{{image class="left" url="images/icons/48/gtk-go-back-rtl.png"}}
=====Ausgangslage=====
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.
----
{{image class="left" url="images/icons/48/application-x-php.png"}}
=====Voraussetzungen=====
Benötigte Komponenten (Sind alle am Ende im Zip enthalten)
* [[PhpFilterInputDefaults]]
* [[PhpErrorsAsExceptions]]
* [[PhpCreatePath]]
* [[PhpEnums]]
----
{{image class="left" url="images/icons/48/application-x-php.png"}}
=====Code=====
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);
}
}
}
?>
----
{{image class="left" url="images/icons/48/application-x-php.png"}}
=====ReWrite=====
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]
----
{{image class="left" alt="ico" url="images/icons/48/zip.png"}}
=====Files=====
--- {{files}}
----
{{image class="left" alt="ico" url="images/icons/48/xfce4-ui.png"}}
=====Kategorien=====
[[CatPhp]] | [[CatLibrary]]