======[PHP] Font Image Creator Class======
FontImageCreator ist eine Klasse, mit der Textgrafiken direkt erstellt werden können
=====Mögliche Anwendung=====
* [[FontClassSingleEntry]]
* [[TextBoxClass]]
* [[FontClassSignatur]]
{{image class="left" url="images/icons/48/application-x-php.png"}}
=====Die Klasse FontImageCreator=====
---Die Klasse Font wandelt Strings in Grafiken mit ttf-Schriften um
(php;1;FontImageCreator.php)
text = $text;
$this->font = $font;
$this->fontPath = $fontPath;
$this->size = self::DEFAULT_SIZE;
$this->border = self::DEFAULT_BORDER;
$this->backgroundColor = self::DEFAULT_BACKGROUND_COLOR;
$this->textColor = self::DEFAULT_TEXT_COLOR;
$this->transparent = self::DEFAULT_TRANSPARENT;
$this->angle = self::DEFAULT_ANGLE;
} catch (Exception $e){
throw $e;
}
}
/**
* createPng
* Erstellt eine PNG-Grafik
*/
public function createPng($image=NULL){
$this->createImage(IMAGETYPE_PNG, self::PNG_CREATOR, $image);
}
/**
* createPng
* Erstellt eine Jpeg-Grafik
*/
public function createJpeg($image=NULL){
$this->createImage(IMAGETYPE_JPEG, self::JPEG_CREATOR, $image);
}
/**
* createGif
* Erstellt eine Jpeg-Grafik
*/
public function createGif($image=NULL){
$this->createImage(IMAGETYPE_GIF, self::GIF_CREATOR, $image);
}
/**
* @param image_type_to_mime_type image type
* @param String image creator function
*/
private function createImage($imageType, $imageCreatorFunction, $image=NULL){
try{
if (!$image) $this->getImage($image);
/** Header ausgeben */
header ('Content-type: '.image_type_to_mime_type($imageType));
/** Grafik ausgeben */
$imageCreatorFunction($image);
/** temporäre Grafik zerstören */
imagedestroy($image);
} catch (Exception $e){
throw $e;
}
}
/**
* gibt ein Image zurück
* @param $image
* @return $box => array(xSize, ySize)
*/
public function getImage(&$image){
/** Grafik erstellen */
try{
$box = $this->getTextBoxSize();
if (!$image) $image = imagecreate ($box['xSize'] , $box['ySize']);
/** Grundfarben definieren (rgb))*/
$backgroundColor = self::imageColor($image, $this->backgroundColor);
/** Textfarbe: */
$textColor = self::imageColor($image, $this->textColor);
/** Hintergrund mit Hintergrundfarbe füllen */
imagefill($image, 0, 0, $backgroundColor);
/** Hintergrundfarbe ggf als transparent definieren */
if($this->transparent) imagecolortransparent($image, $backgroundColor);
/** Text einfügen */
$xPos = abs($box[6]) + $this->border;
$yPos = abs($box[7]) + $this->border;
$textbox = ImageTTFText ($image, $this->size, $this->angle, $xPos, $yPos , $textColor, $this->absFontPath, $this->text);
} catch (Exception $e){
throw $e;
}
return $box;
}
/**
* Textboxgrösse ermitteln
* @return Array(xSize, ySize)
*/
public function getTextBoxSize(){
/** Schriftbildgrösse ermitteln */
$b = imagettfbbox($this->size, $this->angle, $this->absFontPath, $this->text);
/** Grafik erstellen */
$b['xSize'] = abs(max($b[2], $b[4]) - min($b[0], $b[6])) + (2*$this->border);
$b['ySize'] = abs(min($b[5], $b[7]) - max($b[1], $b[3])) + (2*$this->border);
return $b;
}
/**
* Farbe auf Grafik als transparent deklarieren
* @param $image
* @param $color
*/
public static function setTransparent(&$image, $color = self::DEFAULT_BACKGROUND_COLOR){
imagecolortransparent($image, self::imageColor($image, $color));
}
/**
* create a imageColor by a html #-Value (#FFFFFF, #00FFFF etc.)
* @param Image
* @param String color als Hexwert
* @return ImageColor
*/
public static function imageColor(&$image, $color){
//hexwert parsen
preg_match(self::HEX_COLOR_PATTERN, $color, $rgb);
//Gefundener Gesammtstring entfernen
array_shift($rgb);
//Hexwerte in Decimale Werte wandeln und array um eine Stufe reduzieren
array_walk($rgb, create_function('&$item, $key', '$item = hexdec($item);'));
return ImageColorAllocate($image, $rgb[0], $rgb[1], $rgb[2]);
}
/**
* Magic Function __get for all properties
* @access public
* @param String name
* @return Variant value
*/
public function __get($name){
switch ($name){
case 'absFontPath':
$absFontPath = dirname(__FILE__)."{$this->fontPath}/{$this->font}";
if(!is_file($absFontPath)) {
throw new Exception("Font not found :{$absFontPath}");
}
return $absFontPath;
default:
return $this->$name;
}
}
/**
* Magic Function __set for all properties
* @access public
* @param String name
* @param String value
*/
public function __set($name, $value){
switch ($name){
case 'font':
if ($value && !preg_match(self::FONT_NAME_PATTERN, $value)){
throw new Exception("invalid Fontname :{$value}");
}
case 'fontPath':
if ($fontPath && !is_dir($value)){
throw new Exception("invalid Directory :{$value}");
}
default:
$this->$name = $value;
}
}
public function __isset($name){$this->offsetExists($name);}
public function __unset($name){$this->offsetUnset($name);}
}
?>
----
{{image class="left" url="images/icons/48/bookmarks_list_add.png"}}
=====Links=====
---{{backlinks}}
----
{{image class="left" alt="ico" url="images/icons/48/xfce4-ui.png"}}
=====Kategorien=====
---[[CatPhp]]