centering text in an image

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I'm using PHP 4.4.4.  I have GD and freetype libraries.  How do I
center text within an image if that text contains new lines?
According to the imagettftext docs, the x and y specified represent
the coordinates of where the first character begins.  But if there are
new lines under it, I need to adjust that x and y by a certain

What to do? - Dave

Re: centering text in an image wrote:
Quoted text here. Click to load it

It isn't easy. You get the overall size of the multi-line text's bounding
box with imagettfbbox(). You calculate the upper left corner to center that
box within your image. But if you pass a multi-line text string to
ImageTTFText(), you need to give it the position of the lower left of the
first line of text. So you then need to get the height of the first line
of the text, so you can adjust your box position.

Re: centering text in an image

On Mar 21, 11:14=A0pm, ""
Quoted text here. Click to load it

Quoted text here. Click to load it
The code below works ok. The source is imagettftext manual, users
contributions.(correction from John Peters) I have messed with it a
If you pass from a textarea it is pretty straight formward. I
personally am
reproducing the textarea.value in a divide. In order to do that one
strip the "'\n" chars , Windows uses "\r" (so the lines wrap without
white space added by the browser).Then I can call the function below
from a
"POST", "GET", or <img src=3Dmyscript.php?myquerystring>
!!IMPORTANT the calling HTML has to use encodeURI (myquerystring) or
encodeURIComponent(myquerystring)(thanks to SrSilveira)  for this to
be handled property by a NIX
box. Good luck. HTH. n.b. I posted this a few days ago from my news
server but it hasn't appeared in Google groups yet so I'm reposting
via Google.
error_reporting(E_ALL | E_STRICT);
$str = isset($_GET["txt"]) ? $_GET["txt"]: "Sorry";
//$str = isset($_POST["txt"]) ? $_POST["txt"]: "Sorry";
header("Content-type: image/png");
$img = imagecreate(400, 300) or die("Cannot Initialize new GD image
//$background_color = imagecolorallocate($img, 0xAA, 0xAA, 0xFF);
//$str = "Hello there\nMy name is Charlie\nByee";
imagettfJustifytext($img, $str, "CENTURY.TTF", 2);

function imagettfJustifytext($im, $text, $font, $Justify, $Leading=3D3,
$H=3D0, $X=3D10, $Y=3D10, $fsize=3D18, $color=3Darray(0xFF,0x00,0x00),
$bgcolor=3Darray(0xAA,0xAA,0xFF)) {
 $background_color = imagecolorallocate($im, $bgcolor[0],
 $text_color = imagecolorallocate($im, $color[0], $color[1],
 $angle = 0;
 $_bx = imageTTFBbox($fsize,0,$font,$text);
    $text = str_replace ("\r", "\n", $text); //Replace windows line-
 $s = split("[\n]+", $text);  // Array of lines
 $W = abs($_bx[2]-$_bx[0]);
    $nL = count($s);  // Number of lines
 $H = abs($_bx[5]-$_bx[3]);
 $_b = imageTTFBbox($fsize,0,$font,$text);
    for ($i=3D0; $i<$nL; $i++) {
     $_b = imageTTFBbox($fsize,0,$font,$s[$i]);
  $_W = abs($_b[2]-$_b[0]);
  if ($Justify =3D 1) $_X = $W - $_W;  // Justify Right
  else {
   if ($Justify =3D 2) $_X = abs($W/2)-abs($_W/2);  // Justify Center
   else $_X = $X;
  $__H +=3D abs($_b[5]-$_b[3]);
     imagettftext($im, $fsize, $angle, $_X+$X, $__H+$Y, $text_color,
     $__H +=3D $Leading;
 " ">
<HEAD><TITLE>Text Justify test</TITLE>
<script type=3D"text/javascript">
function getImg(str) {
 document.getElementById('pic').src =

// POST to test the textarea
// str = document.getElementById('txtArea').value;
// document.forms[0]['txt'].value = str;

//  POST to test contents of <div><pre> after stripping "\n";
// document.forms[0]['txt'].value = encodeURIComponent(str);
// document.forms[0].submit();
function writeTxt(str) {
 var txt = "";
 for (var i = 0; i < str.length; i++) {
  if (str.charAt(i) =3D '\n') {
  txt +=3D str.charAt(i);
 document.getElementById('oDiv').firstChild.nodeValue = txt;
<div onclick=3DgetImg(this.firstChild.firstChild.nodeValue);><pre
id=3D"oDiv">Hello there\nMy name is Charlie\nByee</pre></div>
<textarea id=3D"txtArea" cols=3D40 rows=3D4
<img id=3D"pic" src=3D"">
<form name=3D"myForm" method=3D"POST" action=3D"justifytxt.php">
<input type=3D"hidden" name=3D"txt"></form>

Site Timeline