VB icon

Dynamic Font Generation

Email
Submitted on: 1/1/2015 7:22:00 PM
By: Daniel M. Hendricks (from psc cd)  
Level: Intermediate
User Rating: By 2 Users
Compatibility: PHP 4.0
Views: 1706
 
     Create text graphics on the fly using GD and freetype libraries. Supports any TrueType font and allows size, color, and bgcolor specification. Also has built in support for date formatting. Examples can be found at http://www.danhendricks.com/site.content/howto/php/dynafont/index.html
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
// for :Dynamic Font Generation
//**************************************
The IMAGE SUPPORT FUNCTIONS lifted from code by another author, but I made the other routines to make it useful and easier to use.
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Dynamic Font Generation
// Description:Create text graphics on the fly using GD and freetype libraries. Supports any TrueType font and allows size, color, and bgcolor specification. Also has built in support for date formatting. Examples can be found at http://www.danhendricks.com/site.content/howto/php/dynafont/index.html
// By: Daniel M. Hendricks (from psc cd)
//
// Inputs:The only required inputs are the 'text', but it is recommended you specify the font face and size as well.
//**************************************

<?
// SETTINGS
$fontbase = $DOCUMENT_ROOT."/howto/php/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);
?>
<?
// IMAGE SUPPORT FUNCTIONS
function GetImageSizeForTextBlock($rotateDegrees, $text, $fontLocation, $fontSize){
 ##-- This will add a small margin area around the text block.--##
 ##-- The margin grows with the font size. --##
 $margin = intval(0.25 * $fontSize);
 ##-- Convert the angle so it is always a number between 0 and 359 --##
 if($rotateDegrees > 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);
}
?>


Other 8 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.