<?PHP
// Define GD fonts.
define('MADGD_FIRSTFONT', 1);
define('MADGD_SECONDFONT', 2);
define('MADGD_THIRDFONT', 3);
define('MADGD_FOURTHFONT', 4);
define('MADGD_FIFTHFONT', 5);
define('MADGD_STACK', 'stackIt');
// Predefined positions.
define('AMULET_X', 340);
define('AMULET_Y', 18);
define('HELMET_X', 377);
define('HELMET_Y', 4);
define('BACKPACK_X', 415);
define('BACKPACK_Y', 18);
define('LEFTHAND_X', 340);
define('LEFTHAND_Y', 55);
define('ARMOR_X', 377);
define('ARMOR_Y', 41);
define('RIGHTHAND_X', 414);
define('RIGHTHAND_Y', 55);
define('RING_X', 340);
define('RING_Y', 92);
define('LEGS_X', 377);
define('LEGS_Y', 78);
define('AMMO_X', 414);
define('AMMO_Y', 92);
define('BOOTS_X', 377);
define('BOOTS_Y', 115);
$GDPreviousPositions = array('x' => 0, 'y' => 0, 'l' => 0);
// Create a class to handle icons.
class MadGDIcon
{
// Define private variables.
private $object, $icon;
// Create the constructor.
public function __construct($object, $icon)
{
// Set the private variable values.
$this->object = $object;
$this->icon = $icon;
}
// Create a public function to set the positions.
public function setPosition($x, $y = false)
{
// Check if the icon exists.
if(file_exists($this->icon))
{
global $GDPreviousPositions;
// Check if x is set to GDCONFIG_STACK.
if($x == MADGD_STACK)
{
$this->position['x'] = $GDPreviousPositions['x'] + $GDPreviousPositions['l'] + ($y == false ? 5 : $y);
$this->position['y'] = $GDPreviousPositions['y'];
}
else
{
$this->position['x'] = $x;
$this->position['y'] = $y;
}
// Get icon size.
$size = getimagesize($this->icon);
$GDPreviousPositions['x'] = $this->position['x'];
$GDPreviousPositions['y'] = $this->position['y'];
$GDPreviousPositions['l'] = $size[0];
// Check for the file extension.
$object = pathinfo($this->icon);
// Run several checks of the extension until a match is found.
switch(strtolower($object['extension']))
{
case 'png':
// Set the GD type to PNG.
$return = imagecreatefrompng($this->icon);
break;
case 'gif':
// Set the GD type to GIF.
$return = imagecreatefromgif($this->icon);
break;
default:
// Set the GD type to JPG.
$return = imagecreatefromjpeg($this->icon);
break;
}
return imagecopy($this->object, $return, $this->position['x'], $this->position['y'], 0, 0, $size[0], $size[1]);
}
else
{
imagestring($this->object, 'fonts/arial.ttf', $this->position['x']+1, $this->position['y']+1, 'Icon: not found', imagecolorallocate($this->object, 0, 0, 0));
imagestring($this->object, 'fonts/arial.ttf', $this->position['x'], $this->position['y'], 'Icon: not found', imagecolorallocate($this->object, 230, 0, 0));
}
}
}
// Create a class to handle text strings.
class MadGDText
{
// Define private variables.
private $object, $text, $font, $color, $size, $position = array('x', 'y'), $shadow;
// Create the constructor.
public function __construct($object, $text, $color, $font, $shadow)
{
// Check if the color is false.
if($color == false)
{
$color = array(255, 255, 255);
}
// Set the private variable values.
$this->object = $object;
$this->text = $text;
$this->font = $font[0];
$this->color = $color;
$this->size = (isset($font[1]) ? $font[1] : 10 );
$this->shadow = $shadow;
}
// Create a public function to set the positions.
public function setPosition($x, $y = false)
{
global $GDPreviousPositions;
// Check if x is set to GDCONFIG_STACK.
if($x == MADGD_STACK)
{
$this->position['x'] = $GDPreviousPositions['x'] + $GDPreviousPositions['l'] + ($y == false ? 5 : $y);
$this->position['y'] = $GDPreviousPositions['y'];
}
else
{
$this->position['x'] = $x;
$this->position['y'] = $y;
}
$textsize = (is_int($this->font) ? null : imagettfbbox($this->size, 0, $this->font, $this->text));
$GDPreviousPositions['x'] = $this->position['x'];
$GDPreviousPositions['y'] = $this->position['y'];
$GDPreviousPositions['l'] = (is_int($this->font) ? imagefontwidth($this->font) * strlen($this->text) : $textsize[2]);
// Return the text string.
if(is_int($this->font))
{
if($this->shadow)
{
imagestring($this->object, $this->font, $this->position['x']+1, $this->position['y']+1, $this->text, imagecolorallocate($this->object, 0, 0, 0));
}
imagestring($this->object, $this->font, $this->position['x'], $this->position['y'], $this->text, imagecolorallocate($this->object, $this->color[0], $this->color[1], $this->color[2]));
}
else
{
if($this->shadow)
{
imagettftext($this->object, $this->size, 0, $this->position['x']+1, $this->position['y']+11, imagecolorallocate($this->object, 0, 0, 0), $this->font, $this->text);
}
imagettftext($this->object, $this->size, 0, $this->position['x'], $this->position['y']+10, imagecolorallocate($this->object, $this->color[0], $this->color[1], $this->color[2]), $this->font, $this->text);
}
}
}
// Create a class to handle GD images.
class MadGD
{
// Define private variables.
protected $background, $object;
// Define public variables.
public $testMode = false;
// Create a public function to set the background image.
public function setBackground($background)
{
// Set the private variable values.
$this->background = $background;
// Check for the file extension.
$object = pathinfo($this->background);
// Run several checks of the extension until a match is found.
switch(strtolower($object['extension']))
{
case 'png':
// Set the GD type to PNG.
if(!$this->testMode) header('Content-Type: image/png');
$this->object = imagecreatefrompng($this->background);
break;
case 'gif':
// Set the GD type to GIF.
if(!$this->testMode) header('Content-Type: image/gif');
$this->object = imagecreatefromgif($this->background);
break;
default:
// Set the GD type to JPG.
if(!$this->testMode) header('Content-Type: image/jpeg');
$this->object = imagecreatefromjpeg($this->background);
break;
}
// Return the GD object.
return $this->object;
}
// Create a public function to add icons.
public function addIcon($icon)
{
return new MadGDIcon($this->object, $icon);
}
// Create a public function to add text strings.
public function addText($text, $config = array())
{
// Set the default configuration.
$defaultConfig = array('font' => MADGD_SECONDFONT, 'size' => 9, 'color' => '#FFFFFF', 'shadow' => true);
// Check if all of the keys exists in the new config.
foreach($defaultConfig as $defaultKey => $defaultValue)
{
if(!array_key_exists($defaultKey, $config))
{
$config[$defaultKey] = $defaultValue;
}
}
// Return a new GDTextString object.
return new MadGDText($this->object, $text, $this->HexToRGB($config['color']), array($config['font'], $config['size']), $config['shadow']);
}
// Create a private function to transform any HEX values into RGB format.
private function HexToRGB($hex)
{
// Check if it is HEX.
if(!is_array($hex))
{
// Remove any # character.
$hex = ereg_replace("#", "", $hex);
$color = array();
// Check whether it is 6 characters long.
if(strlen($hex) == 6)
{
// Set the new values.
$color[0] = hexdec(substr($hex, 0, 2));
$color[1] = hexdec(substr($hex, 2, 2));
$color[2] = hexdec(substr($hex, 4, 2));
}
}
// Return the color.
return (!is_array($hex) ? $color : $hex);
}
// Create a public function to display the image as PNG and unset/destroy it after it is completed.
public function display()
{
imagepng($this->object);
imagedestroy($this->object);
}
public function unnn()
{
unset($this);
}
}
$image = new MadGD;
$image->addText('hello world')->setPosition(10, 10);
$image->setBackground('Signature.png');
$image->display();
$image->unnn();