Daniel M. Hendricks

Dynamic Font Generation

Description

This code allows you to create text as PNG images dynamically.

Requirements

  • PHP compiled --with-gd and --with-png
  • TrueType font(s) to use as typeface

Examples

<img src="dynafont.php?size=10&text=Simple%20Example">

<img src="dynafont.php?size=15&text=Hello%20World!&font=georgia.ttf">

<img src="dynafont.php?size=15&color=FFFFC0&bgcolor=7AAED2&text=I%20Want%20My%20MTV&font=arial.ttf">

<img src="dynafont.php?size=13&font=sylfaen.ttf&date=l,+F+j,+Y">

Date formatting codes can be found here.

Source Code

// SETTINGS
$fontbase = $DOCUMENT_ROOT."/includes/dynafont/";
$rotateDegrees = 0;
$text = trim(stripslashes($_GET["text"]));

// SET FONT PROPERTIES
$font = $_GET["font"];
if (!$font) { $font = "arial.ttf"; }
$fontfile = $fontbase.$font;

$size = $_GET["size"];
if (!$size) { $size = 5; }

$textalign = $_GET["textalign"];
if (!$textalign) { $textalign = "left"; }

$color = $_GET["color"];
if (!$color) { $color = "000000"; }
//$foreground = GetRGBvalues("0x".$color, false);

$bgcolor = $_GET["bgcolor"];
if (!$bgcolor) { $bgcolor = "FFFFFF"; }
//$background = GetRGBvalues("0x".$bgcolor, false);

if($_GET["date"]) {
    $text = date($_GET["date"]);
}

$ImageSizeInfoArr = GetImageSizeForTextBlock($rotateDegrees, $text, $fontfile, $size);

// CREATE IMAGE
$im = imagecreate ($ImageSizeInfoArr["WIDTH"]+5, $ImageSizeInfoArr["HEIGHT"]);
$background = ImageColorAllocate ($im, hexdec(substr($bgcolor, 0, 2)), hexdec(substr($bgcolor, 2, 2)), hexdec(substr($bgcolor, 4, 2)));
$foreground = ImageColorAllocate ($im, hexdec(substr($color, 0, 2)), hexdec(substr($color, 2, 2)), hexdec(substr($color, 4, 2)));
ImageTTFText ($im, $size, 0, $ImageSizeInfoArr["X_POS"], $ImageSizeInfoArr["Y_POS"], $foreground, $fontfile, $text);

// SEND IMAGE TO BROWSER
Header ("Content-type: image/PNG");
ImagePNG ($im, "", 100);
ImageDestroy ($im);
?>

 359 || $rotateDegrees < -359){
        $rotateDegrees = intval($rotateDegrees % 360);
    }
    if($rotateDegrees < 0){
        $rotateDegrees = 360 + $rotateDegrees;
    }

    #-- Clean out all of the "new line" characters. --#
    #-- The following function expects the special code of "-br-" for line breaks. --#
    $text = preg_replace("/(\n|\r)/", "", $text);

    ##-- Get the total size of the text block so that we know how big to create the Image --##
    $TextBoxSize = ImageTTFBBox ($fontSize, $rotateDegrees, $fontLocation, preg_replace("/-br-/", "\n\r", $text));

    ##-- Put the variables into reader-friendly variables --##
    $TxtBx_Lwr_L_x = $TextBoxSize[0];
    $TxtBx_Lwr_L_y = $TextBoxSize[1];
    $TxtBx_Lwr_R_x = $TextBoxSize[2];
    $TxtBx_Lwr_R_y = $TextBoxSize[3];
    $TxtBx_Upr_R_x = $TextBoxSize[4];
    $TxtBx_Upr_R_y = $TextBoxSize[5];
    $TxtBx_Upr_L_x = $TextBoxSize[6];
    $TxtBx_Upr_L_y = $TextBoxSize[7];

    ##-- The Text Box coordinates are relative to the font, regardless of the angle         --##
    ##-- We need to figure out the height and width of the text box accounting for the rotation     --##
    if($rotateDegrees <= 90 || $rotateDegrees >= 270 ){
        $TextBox_width = max($TxtBx_Lwr_R_x, $TxtBx_Upr_R_x) - min($TxtBx_Lwr_L_x, $TxtBx_Upr_L_x);
        $TextBox_height = max($TxtBx_Lwr_L_y, $TxtBx_Lwr_R_y) - min($TxtBx_Upr_R_y, $TxtBx_Upr_L_y);

        ##-- This figures out where the coordinates of the first letter in the text block starts    --##
        ##-- It is roughly the lower left-hand corner letter                     --##
        $Start_Text_Coord_x = -(min($TxtBx_Upr_L_x, $TxtBx_Lwr_L_x));
        $Start_Text_Coord_y = -(min($TxtBx_Upr_R_y, $TxtBx_Upr_L_y));
    }
    else{
        $TextBox_width = max($TxtBx_Lwr_L_x, $TxtBx_Upr_L_x) - min($TxtBx_Lwr_R_x, $TxtBx_Upr_R_x);
        $TextBox_height = max($TxtBx_Upr_R_y, $TxtBx_Upr_L_y) - min($TxtBx_Lwr_L_y, $TxtBx_Lwr_R_y);

        $Start_Text_Coord_x = -(min($TxtBx_Lwr_R_x,$TxtBx_Upr_R_x));
        $Start_Text_Coord_y = -(min($TxtBx_Lwr_L_y, $TxtBx_Lwr_R_y));
    }

    ##-- We need to add our margin to the coordinates of the first letter --##
    $Start_Text_Coord_x += $margin;
    $Start_Text_Coord_y += $margin - 2; //Don't forget to account for the '0th' pixel at Y-coord 0

    ##-- We are going to make the image just big enough to hold our text block... accounting for the rotation and font size. --##
    ##-- We times the Margin by 2 so that there is a margin on all 4 sides     --##
    $TotalImageWidth = $TextBox_width + $margin *2;
    $TotalImageHeight = $TextBox_height + $margin *2;

    ##-- Send back a hash with our calculations --#
    return array("WIDTH"=>$TotalImageWidth, "HEIGHT"=>$TotalImageHeight, "X_POS"=>$Start_Text_Coord_x, "Y_POS"=>$Start_Text_Coord_y);
}
?>

This code will bind an object to record set named $result and display the values therein. Be sure to replace “localhost”, “username”, and “password” with the appropriate values for your database server.

Post a Comment

You must be logged in to post a comment.

Tip: Sign up for a free Gravatar to have a photo next to your comment! Your gravatar will follow you around when you post to blogs that support it, based on the e-mail address you use to post.