<?php
class FontException extends Exception {}
class Font {
private static $fonts = array();
private $name = '';
private $weight = '';
private $directory = '';
private $letters = array();
public $format = 'png';
public static function get($name, $weight = null, $directory = null) {
if($weight === null) {
$weight = 'normal';
}
if(self::$fonts === array()) {
if($directory === null) {
throw new FontException('directory required');
}
self::$fonts[] = new Font($name, $weight, $directory);
return end(self::$fonts);
} else {
foreach(self::$fonts as $font) {
if(($font->name === $name) && ($font->weight === $weight)) {
return $font;
}
}
if($directory === null) {
throw new FontException('directory required');
}
self::$fonts[] = new Font($name, $weight, $directory);
return end(self::$fonts);
}
}
private function __construct($name, $weight, $directory) {
if(!is_string($name) || !is_string($name) || !is_string($directory) || empty($name) || empty($weight) || empty($directory)) {
throw new FontException('invalid arguments');
} elseif(!file_exists($directory) || !is_dir($directory)) {
throw new FontException('directory >>' . $directory . '<< does not exist');
}
$this->name = strtolower($name);
$this->weight = strtolower($weight);
$this->directory = $directory;
}
public function letter($letter) {
if(isset($this->letters[$letter])) {
return $this->letters[$letter];
} elseif(file_exists($this->__toString() . $letter . '.' . $this->format) || ($size = getimagesize($this->__toString() . $letter . '.' . $this->format))) {
switch($this->format) {
case 'png':
$this->letters[$letter] = array(
imagecreatefrompng($this->__toString() . $letter . '.' . $this->format),
$size,
);
break;
case 'jpeg':
case 'jpg':
$this->letters[$letter] = array(
imagecreatefromjpeg($this->__toString() . $letter . '.' . $this->format),
$size,
);
break;
case 'gif':
$this->letters[$letter] = array(
imagecreatefromgif($this->__toString() . $letter . '.' . $this->format),
$size,
);
break;
default:
throw new FontException('invalid image format');
}
} else {
throw new FontException('could not find the file for the character "' . $letter . '"');
}
}
public function __toString() {
return $this->directory . '/' . ucfirst($this->name) . '_Font_' . ucfirst($this->weight);
}
}
if(isset($_POST['images']['submit'])) {
try {
if(($length = strlen($input = trim($_POST['images']['text']))) === 0) {
throw new Exception('invalid input');
}
if($length < 5) {
$font = Font::get('arial', null, './images/sig');
} else {
$font = Font::get('arial', 'bold', './images/sig');
}
# set format of the character files
$font->format = 'png';
$letters = str_split($input);
$size = array(0, 0);
foreach($letters as $i => $letter) {
$letters[$i] = $letter = $font->letter($letter);
$size[0] += $letter[0][0];
$size[1] = ($letter[0][1] > $size[1]) ? $letter[0][1] : $size[1];
}
$image = imagecreatetruecolor($size[0], $size[1]);
$position_x = 0;
$imend; # hast du irgendwo definiert
foreach($letters as $letter) {
imagecopyresampled($imend, $letter[0], $position_x, 0, 0, 0, $letter[0][0], $letter[0][1], $letter[0][0], $letter[0][1]);
$position_x += $letter[0][0];
}
header('Content-Type: image/png');
imagepng($image);
} catch(Exception $e) {
?>
<!DOCTYPE html>
<html>
<head>
<title>Fehler aufgetreten</title>
</head>
<body>
<div style="border:3px solid red"><?php echo $e->getMessage(); ?></div>
</body>
<?php
}
} else {
?>
<!DOCTYPE html>
<html>
<head>
<title>Bild erstellen</title>
</head>
<body>
<form id="images" method="post" action="">
<label for="images-text"><input id="images-text" name="images[text]" type="text" /><br />
<input id="images-submit" name="images[submit]" type="submit" value="erstellen" />
</form>
</body>
<?php
}
?>