Contents
Login Register Wishes Forum About
Language Reference » Drawing functions » drawText
Help topics
Latest updates
4704d  replaceImageMapTitle
4704d  replaceImageMapValues
4704d  createFunctionSerie
4704d  setAbsicssaPosition
4704d  negateValues
Toolbox
  Download as PDF
  Print this page
  Share this page
  Create an account
  Feed the updates!
Google AdSense
drawText - Write text

This function allows you to write text on your pictures. It is possible to tune the rendering by playing with the $Format array. To learn more about this please read the Format array guide. Lines like all simple drawing functions are supporting anti-alias and shadows.

This function is also used internally by some drawing functions.

This function was a bit tricky to write as the GD function imagettfbbox isn‘t accurate when rotating text. The workaround was to re-create it. You can use it calling the getTextBox function.


Calling this function
drawText($X,$Y,$Text,$Format="")
Where :

X,Y are the coordinate of the text (see the Align option).
Text is the text that will be written.
Format is an array containing the drawing parameters of the arrow.


This function returns an array or arrays with the coordinates of the surrounding box. This array can be accessed through the internal FontBox variable. You can access the points values with FontBox[0-3][0-1], the first value is the index of the point, the second is either 0 for the X coordinate and 1 of the Y one.
 /* Write some text */
 $myPicture->drawText(60,115,"10 degree text",$TextSettings);

 /* Print the coordinates of the top left corner of the written text */
 echo "X = ".$myPicture->FontBox[0][0].", Y = ".$myPicture->FontBox[0][1];

Customisation array - Tune up your line!

It is possible to customize the line rendering by playing with this array. Providing a detailled configuration is not mandatory, by default the text will be written black with no transparency. The text will be aligned using the bottom left corner. (TEXT_ALIGN_BOTTOMLEFT)

The line color can be set with R, G, B.
The alpha transparency factor can be set with Alpha.
The angle can be set with Angle.
The font name can be overridden with FontName.
The font size can be overridden with FontSize.
The text alignment can be set with Align. Depending of the value you‘ll put, the X,Y coordinates of the text will be translated to the red marker, you can see bellow all the alignment options :



The alignment is computed based on the text size and angle, this means that even if you rotate your text, the control points will be automatically re-computed.


It is possible to draw a filled rectangle under the text, following parameters can be tunned :

To enable this feature, set DrawBox to TRUE.
You can specify the box padding with BorderOffset. (default is 6)
The background color can be set with BoxR, BoxG, BoxB.
The background alpha can be set with BoxAlpha.
You can draw rounded corners setting BoxRounded to TRUE.
The corner radius can be set withRoundedRadius.
You can specify if the box will have a border setting DrawBoxBorder to TRUE.
You can specify a border surrounding factor with BoxSurrounding.
The border color can be set with BoxBorderR, BoxBorderG, BoxBorderB.
The border alpha can be set with BoxBorderAlpha.


Sample script



 /* pChart library inclusions */
 include("../class/pDraw.class.php");
 include("../class/pImage.class.php");

 /* Create the pChart object */
 $myPicture = new pImage(700,230);

 /* Draw the background */
 $Settings = array("R"=>170, "G"=>183, "B"=>87, "Dash"=>1, "DashR"=>190, "DashG"=>203, "DashB"=>107);
 $myPicture->drawFilledRectangle(0,0,700,230,$Settings);

 /* Overlay with a gradient */
 $Settings = array("StartR"=>219, "StartG"=>231, "StartB"=>139, "EndR"=>1, "EndG"=>138, "EndB"=>68, "Alpha"=>50);
 $myPicture->drawGradientArea(0,0,700,230,DIRECTION_VERTICAL,$Settings);
 $myPicture->drawGradientArea(0,0,700,20,DIRECTION_VERTICAL,array("StartR"=>0,"StartG"=>0,"StartB"=>0,"EndR"=>50,"EndG"=>50,"EndB"=>50,"Alpha"=>80));

 /* Add a border to the picture */
 $myPicture->drawRectangle(0,0,699,229,array("R"=>0,"G"=>0,"B"=>0));
 
 /* Write the picture title */ 
 $myPicture->setFontProperties(array("FontName"=>"../fonts/Silkscreen.ttf","FontSize"=>6));
 $myPicture->drawText(10,13,"drawText() - add some text to your charts",array("R"=>255,"G"=>255,"B"=>255));

 /* Enable shadow computing */ 
 $myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>20));

 /* Write some text */ 
 $myPicture->setFontProperties(array("FontName"=>"../fonts/advent_light.ttf","FontSize"=>20));
 $TextSettings = array("R"=>255,"G"=>255,"B"=>255,"Angle"=>10);
 $myPicture->drawText(60,115,"10 degree text",$TextSettings);

 /* Write some text */ 
 $TextSettings = array("R"=>0,"G"=>0,"B"=>0,"Angle"=>0,"FontSize"=>40);
 $myPicture->drawText(220,130,"Simple text",$TextSettings);

 /* Write some text */ 
 $TextSettings = array("R"=>200,"G"=>100,"B"=>0,"Angle"=>90,"FontSize"=>14);
 $myPicture->drawText(500,170,"Vertical Text",$TextSettings);

 /* Write some text */ 
 $myPicture->setFontProperties(array("FontName"=>"../fonts/Bedizen.ttf","FontSize"=>6));
 $TextSettings = array("DrawBox"=>TRUE,"BoxRounded"=>TRUE,"R"=>0,"G"=>0,"B"=>0,"Angle"=>0,"FontSize"=>10);
 $myPicture->drawText(220,160,"Encapsulated text",$TextSettings);

 /* Write some text */ 
 $myPicture->setFontProperties(array("FontName"=>"../fonts/Forgotte.ttf","FontSize"=>6));
 $TextSettings = array("DrawBox"=>TRUE,"R"=>0,"G"=>0,"B"=>0,"Angle"=>0,"FontSize"=>10);
 $myPicture->drawText(220,195,"Text in a box",$TextSettings);

 /* Render the picture (choose the best way) */
 $myPicture->autoOutput("pictures/example.drawText.png");
Last updated on 12/13/2010 
by Jean-Damien 
Linked resources
Community comments
Message from mykchan on 13/12/2010
pChart v1.2.7 allowed you to set X2 and Y2, creating a box for text. Setting X1Y1 and X2Y2 resulted in text of any pixel length perfectly centered between X1Y1 and X2Y2. can this centering ability be returned to pChart?
© Copyrights
Components used on this web site : Famfamfam icons has been made by Mark James, Rounded corners lite has been coded by Cameron Cooke and Tim Hutchison, SyntaxHighlighter has been written by Alex Gorbatchev. pChart and this web site have been created by Jean-Damien POGOLOTTI. This documentation contains 185 pages and 56 comments. 415 users have registered. This page has been rendered in 0,01 seconds. Wiki revision 1.37.