Protege
27-05-2009, 09:21 AM
Made this up, thought i'd release it to you guis. Probably loads on teh internets.
<?php
class fontGenerator
{
private $face;
private $size;
private $width;
private $height;
private $text = array();
function __construct( $face, $size, $lSpacer = 0 )
{
header( 'Content-Type: image/png' );
$this->face = $face;
$this->size = $size;
$this->width = $lSpacer;
return true;
}
public function addText( $str, $colour )
{
$this->text[] = array( $str, $colour, $this->width );
$arr = imagettfbbox( $this->size, 0, $this->face, $str );
$this->width = abs( $this->width + $arr[ 4 ] );
$this->height = abs( $arr[ 5 ] - $arr[ 1 ] );
return true;
}
public function generateText()
{
$holder = imagecreatetruecolor( $this->width, $this->height );
imagesavealpha( $holder, true );
imagefill( $holder, 0, 0, imagecolorallocatealpha( $holder, 0, 0, 0, 127 ) );
foreach( $this->text as $text )
{
$e_colour = explode( ',', $text[ 1 ] );
$colour = imagecolorallocate( $holder, $e_colour[ 0 ], $e_colour[ 1 ], $e_colour[ 2 ] );
imagettftext( $holder, $this->size, 0, $text[ 2 ], $this->size - ( $this->size / 20 ), $colour, $this->face, $text[ 0 ] );
}
imagepng( $holder );
imagedestroy( $holder );
}
}
usage:
require 'fontgenerator.class.php';
$fontGenerator = new fontGenerator( 'ARIALNI.TTF', 24 );
/* fontGenerator( string font, int size, int leftspacer (optional) ); */
$fontGenerator->addText( 'welcome ', '147,215,255' );
/* addText( string text, string colour(RGB) ); */
$fontGenerator->addText( 'james', '99,99,99' );
$fontGenerator->generateText();
<?php
class fontGenerator
{
private $face;
private $size;
private $width;
private $height;
private $text = array();
function __construct( $face, $size, $lSpacer = 0 )
{
header( 'Content-Type: image/png' );
$this->face = $face;
$this->size = $size;
$this->width = $lSpacer;
return true;
}
public function addText( $str, $colour )
{
$this->text[] = array( $str, $colour, $this->width );
$arr = imagettfbbox( $this->size, 0, $this->face, $str );
$this->width = abs( $this->width + $arr[ 4 ] );
$this->height = abs( $arr[ 5 ] - $arr[ 1 ] );
return true;
}
public function generateText()
{
$holder = imagecreatetruecolor( $this->width, $this->height );
imagesavealpha( $holder, true );
imagefill( $holder, 0, 0, imagecolorallocatealpha( $holder, 0, 0, 0, 127 ) );
foreach( $this->text as $text )
{
$e_colour = explode( ',', $text[ 1 ] );
$colour = imagecolorallocate( $holder, $e_colour[ 0 ], $e_colour[ 1 ], $e_colour[ 2 ] );
imagettftext( $holder, $this->size, 0, $text[ 2 ], $this->size - ( $this->size / 20 ), $colour, $this->face, $text[ 0 ] );
}
imagepng( $holder );
imagedestroy( $holder );
}
}
usage:
require 'fontgenerator.class.php';
$fontGenerator = new fontGenerator( 'ARIALNI.TTF', 24 );
/* fontGenerator( string font, int size, int leftspacer (optional) ); */
$fontGenerator->addText( 'welcome ', '147,215,255' );
/* addText( string text, string colour(RGB) ); */
$fontGenerator->addText( 'james', '99,99,99' );
$fontGenerator->generateText();