PDA

View Full Version : Font generator - php



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();

Jahova
27-05-2009, 09:34 AM
Not being a pain, but a working demo would be nice.

Protege
27-05-2009, 09:41 AM
http://www.safeupload.com/Public/generate_text.php?Text=Hello|%20James%20Roz%E9e

Jahova
27-05-2009, 11:30 AM
Quite nice, if I add a p though, the bottom gets cut off.

Protege
27-05-2009, 11:32 AM
dear god, i'll fix the math when I get home, been so tired.

Brixsta
27-05-2009, 05:14 PM
dear god, i'll fix the math when I get home, been so tired.
Very well done sir
you noa have permizionz to rule teh interwebz.

Protege
27-05-2009, 08:18 PM
yea i do, suck my ****./

iDenning
28-05-2009, 12:52 AM
Lol ^

Nice of you to release, should come in handy to someone.

Protege
28-05-2009, 07:33 AM
Fixed it, I believe anyway
http://www.safeupload.com/Public/generate_text.php?Text=james|%20rozee%20is%20queer (http://www.safeupload.com/Public/generate_text.php?Text=james%7C%20rozee%20is%20que er)



<?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 ] );

$height = abs( $arr[ 5 ] - $arr[ 1 ] );

if( $this->height < $height )
{
$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 );

}
}


generate_text.php


<?php
require_once 'generate.class.php';

$fontGenerator = new fontGenerator( 'SEGOEUII.TTF', 24, 10 );

$txt = $_GET[ 'Text' ];
$e_txt = explode( '|', $txt );

$fontGenerator->addText( $e_txt[ 0 ], '147,215,255' );
$fontGenerator->addText( $e_txt[ 1 ], '99,99,99' );
$fontGenerator->generateText();

Verrou
28-05-2009, 10:29 AM
http://www.safeupload.com/Public/generate_text.php?Text=f|UUUUUUUUUUUUUUUUUUUUUUUUU U (http://www.safeupload.com/Public/generate_text.php?Text=f%7CUUUUUUUUUUUUUUUUUUUUUUU UUU)

The f is cut off at the top :) Cool though :)

Want to hide these adverts? Register an account for free!