<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Write text to the image using fonts using FreeType 2</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.imageftbbox.html">imageftbbox</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.imagegammacorrect.html">imagegammacorrect</a></div> <div class="up"><a href="ref.image.html">GD and Image Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.imagefttext" class="refentry"> <div class="refnamediv"> <h1 class="refname">imagefttext</h1> <p class="verinfo">(PHP 4 >= 4.0.7, PHP 5)</p><p class="refpurpose"><span class="refname">imagefttext</span> — <span class="dc-title">Write text to the image using fonts using FreeType 2</span></p> </div> <div class="refsect1 description" id="refsect1-function.imagefttext-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">array</span> <span class="methodname"><strong>imagefttext</strong></span> ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$image</code></span> , <span class="methodparam"><span class="type">float</span> <code class="parameter">$size</code></span> , <span class="methodparam"><span class="type">float</span> <code class="parameter">$angle</code></span> , <span class="methodparam"><span class="type">int</span> <code class="parameter">$x</code></span> , <span class="methodparam"><span class="type">int</span> <code class="parameter">$y</code></span> , <span class="methodparam"><span class="type">int</span> <code class="parameter">$color</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$fontfile</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$text</code></span> [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$extrainfo</code></span> ] )</div> </div> <div class="refsect1 parameters" id="refsect1-function.imagefttext-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter"> image</code></em></span><dd> <p class="para">An image resource, returned by one of the image creation functions, such as <span class="function"><a href="function.imagecreatetruecolor.html" class="function">imagecreatetruecolor()</a></span>.</p></dd> </dt> <dt> <span class="term"><em><code class="parameter">size</code></em></span> <dd> <p class="para"> The font size to use in points. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">angle</code></em></span> <dd> <p class="para"> The angle in degrees, with 0 degrees being left-to-right reading text. Higher values represent a counter-clockwise rotation. For example, a value of 90 would result in bottom-to-top reading text. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">x</code></em></span> <dd> <p class="para"> The coordinates given by <em><code class="parameter">x</code></em> and <em><code class="parameter">y</code></em> will define the basepoint of the first character (roughly the lower-left corner of the character). This is different from the <span class="function"><a href="function.imagestring.html" class="function">imagestring()</a></span>, where <em><code class="parameter">x</code></em> and <em><code class="parameter">y</code></em> define the upper-left corner of the first character. For example, "top left" is 0, 0. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">y</code></em></span> <dd> <p class="para"> The y-ordinate. This sets the position of the fonts baseline, not the very bottom of the character. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">color</code></em></span> <dd> <p class="para"> The index of the desired color for the text, see <span class="function"><a href="function.imagecolorexact.html" class="function">imagecolorexact()</a></span>. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">fontfile</code></em></span> <dd> <p class="para"> The path to the TrueType font you wish to use. </p> <p class="para"> Depending on which version of the GD library PHP is using, <em class="emphasis">when <em><code class="parameter">fontfile</code></em> does not begin with a leading <em>/</em> then <em>.ttf</em> will be appended</em> to the filename and the library will attempt to search for that filename along a library-defined font path. </p> <p class="para"> When using versions of the GD library lower than 2.0.18, a <em>space</em> character, rather than a semicolon, was used as the 'path separator' for different font files. Unintentional use of this feature will result in the warning message: <em>Warning: Could not find/open font</em>. For these affected versions, the only solution is moving the font to a path which does not contain spaces. </p> <p class="para"> In many cases where a font resides in the same directory as the script using it the following trick will alleviate any include problems. <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// Set the enviroment variable for GD<br /></span><span style="color: #0000BB">putenv</span><span style="color: #007700">(</span><span style="color: #DD0000">'GDFONTPATH=' </span><span style="color: #007700">. </span><span style="color: #0000BB">realpath</span><span style="color: #007700">(</span><span style="color: #DD0000">'.'</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// Name the font to be used (note the lack of the .ttf extension)<br /></span><span style="color: #0000BB">$font </span><span style="color: #007700">= </span><span style="color: #DD0000">'SomeFont'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">text</code></em></span> <dd> <p class="para"> Text to be inserted into image. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">extrainfo</code></em></span> <dd> <p class="para"> <table class="doctable table"> <caption><strong>Possible array indexes for <em><code class="parameter">extrainfo</code></em></strong></caption> <thead> <tr> <th>Key</th> <th>Type</th> <th>Meaning</th> </tr> </thead> <tbody class="tbody"> <tr> <td><em>linespacing</em></td> <td><span class="type"><a href="language.types.float.html" class="type float">float</a></span></td> <td>Defines drawing linespacing</td> </tr> </tbody> </table> </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.imagefttext-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> This function returns an array defining the four points of the box, starting in the lower left and moving counter-clockwise: <table class="doctable informaltable"> <tbody class="tbody"> <tr> <td>0</td> <td>lower left x-coordinate</td> </tr> <tr> <td>1</td> <td>lower left y-coordinate</td> </tr> <tr> <td>2</td> <td>lower right x-coordinate</td> </tr> <tr> <td>3</td> <td>lower right y-coordinate</td> </tr> <tr> <td>4</td> <td>upper right x-coordinate</td> </tr> <tr> <td>5</td> <td>upper right y-coordinate</td> </tr> <tr> <td>6</td> <td>upper left x-coordinate</td> </tr> <tr> <td>7</td> <td>upper left y-coordinate</td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.imagefttext-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-3243"> <p><strong>Example #1 <span class="function"><strong>imagefttext()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// Create a 300x100 image<br /></span><span style="color: #0000BB">$im </span><span style="color: #007700">= </span><span style="color: #0000BB">imagecreatetruecolor</span><span style="color: #007700">(</span><span style="color: #0000BB">300</span><span style="color: #007700">, </span><span style="color: #0000BB">100</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$red </span><span style="color: #007700">= </span><span style="color: #0000BB">imagecolorallocate</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">, </span><span style="color: #0000BB">0xFF</span><span style="color: #007700">, </span><span style="color: #0000BB">0x00</span><span style="color: #007700">, </span><span style="color: #0000BB">0x00</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$black </span><span style="color: #007700">= </span><span style="color: #0000BB">imagecolorallocate</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">, </span><span style="color: #0000BB">0x00</span><span style="color: #007700">, </span><span style="color: #0000BB">0x00</span><span style="color: #007700">, </span><span style="color: #0000BB">0x00</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Make the background red<br /></span><span style="color: #0000BB">imagefilledrectangle</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">299</span><span style="color: #007700">, </span><span style="color: #0000BB">99</span><span style="color: #007700">, </span><span style="color: #0000BB">$red</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Path to our ttf font file<br /></span><span style="color: #0000BB">$font_file </span><span style="color: #007700">= </span><span style="color: #DD0000">'./arial.ttf'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Draw the text 'PHP Manual' using font size 13<br /></span><span style="color: #0000BB">imagefttext</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">, </span><span style="color: #0000BB">13</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">105</span><span style="color: #007700">, </span><span style="color: #0000BB">55</span><span style="color: #007700">, </span><span style="color: #0000BB">$black</span><span style="color: #007700">, </span><span style="color: #0000BB">$font_file</span><span style="color: #007700">, </span><span style="color: #DD0000">'PHP Manual'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Output image to the browser<br /></span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Content-Type: image/png'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">imagepng</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">imagedestroy</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.imagefttext-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">This function requires GD 2.0.1 or later (2.0.28 or later is recommended).</span></p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">This function is only available if PHP is compiled with freetype support (<strong class="option configure">--with-freetype-dir=DIR</strong> ) </span></p></blockquote> </div> <div class="refsect1 changelog" id="refsect1-function.imagefttext-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>4.3.5</td> <td> <em><code class="parameter">extrainfo</code></em> was made optional. </td> </tr> </tbody> </table> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.imageftbbox.html">imageftbbox</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.imagegammacorrect.html">imagegammacorrect</a></div> <div class="up"><a href="ref.image.html">GD and Image Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>