User Tools

Site Tools


php:kompost:fontclasssingleentry

[PHP] Font Image Creator Single Entry

Dieses Script wendet die Classe FontImageCreator an um eine Textgrafik aus einem PHP-Link mit Parametern zusammenstellen kann.

Benötigte Module

Code

font.php

(php;1;font.php)
<?php
/**
* @copyright	© by ERB software
*       		All rights reserved
* @contact		stefan.erb@erb-software.com
* @date         10.02.2010
*
* @uses
* FontImageCreator.php  The Font-Class
* 
* @example
* Normal (mögliche Parameter siehe inhalt de Array $allowedParams):
* font.php?font=avalonQuest.ttf&size=20&text=Hallo Welt
*/

// Import the Font Class
include_once(dirname(__FILE__).'/FontImageCreator.php');

//definitions
define('C_COLOR_PATTERN', '/^#[[:xdigit:]]{6}$/');
//allowed get-params
$allowedParams = array( 'text', 'font', 'size', 'textColor', 'backgroundColor', 
						'border', 'transparent', 'angle', 'fontPath');
//create variableas from the allowed-get-params
extract(array_intersect_key($_GET, array_flip($allowedParams)));

try{
	// create Font Object
	$font = new FontImageCreator($_GET['text'], $_GET['font']);

	// set the params
	if (preg_match(C_COLOR_PATTERN, $color ='#'.$textColor)){
		$font->textColor = $color;
	}
	if (preg_match(C_COLOR_PATTERN, $color ='#'.$backgroundColor)){
		$font->backgroundColor = $color;
		$font->transparent = false;
	}
	if ($size)              $font->size         = $size;
	if ($border)            $font->border       = $border;
	if ($transparent)       $font->transparent  = $transparent;
	if ($angle)             $font->angle        = $angle;
	if ($fontPath)          $font->fontPath     = $fontPath;
	
	// create the image
	$font->createPng();
	// clean up
	$font=NULL;
	unset($font);
} catch (Exception $e){
	echo nl2br("<b>{$e->getMessage()}</b>\n");
	echo nl2br($e);
}
?>

Verwendung mit Pfadumleitung

Wenn man die Grafik in einem Forum oder so als Signatur verwenden will, kann man dies mittels Rewrite umgehen. Dazu müssen wir in der .htaccess-Datei ensprechende Regeln definieren. Mein Beispiel beinhalter 3 verchiedene Regeln

.htaccess
#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
# alles an meine Datei weiterleiten. Von da kann man dan machen was man will...
 
#Schema:    path/$font/[$textColor/[$backgroundColor/]]$text[/img.png]
#Beispiel:  path/arial.ttf/00FFFF/FFFF00/Hallo+Welt
#rewrite:   path/font.php?font=arial.ttf&textColor=00FFFF&backgroundColor=FFFF00&text=Hallo+Welt
#Beispiel:  path/arial.ttf/00FFFF/Hallo+Welt/img.png
#rewrite:   path/font.php?font=arial.ttf&textColor=00FFFF&text=Hallo+Welt
#Beispiel:  path/arial.ttf/Hallo+Welt/img.png
#rewrite:   path/font.php?font=arial.ttf&text=Hallo+Welt
 
	RewriteRule ^([^\/]+)\/(?:([[:xdigit:]]{6})\/|)(?:([[:xdigit:]]{6})\/|)(?:(.*(?=\/img.png))|(.*)) font.php?font=$1&textColor=$2&backgroundColor=$3&text=$4$5 [L,QSA]

Verlinkung auf Webseite

Um die Grafik auf einer Webseite anzuzeigen kann sie genaus verlinkt werden wie jede andere Grafik

<img src="font.php?font=avalonQuest.ttf&size=20&text=Hallo+Welt">
<img src="avalonQuest.ttf/Hallo+Welt">
php/kompost/fontclasssingleentry.txt · Last modified: 11.12.2013 11:03:28 (external edit)