By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,890 Members | 1,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,890 IT Pros & Developers. It's quick & easy.

GD text justify

P: n/a
The following code works partially.
If Justify is set to 0, I see 3 separate lines of text left justified

but when I try to right or center justify I only get the first line.
It is
justified but out of vertical position as well it seems. Can someone
correct
this or point me to a working function. TIA
Jim
// $Justify: 0=left 1=right 2=center - watch word wrap
<?php
function imagettfJustifytext($text, $font="CENTURY.TTF", $Justify=0,
$Leading=0, $W=0, $H=0, $X=0, $Y=0, $fsize=12,
$color=array(0x0,0x0,0x0), $bgcolor=array(0xFF,0xFF,0xFF)){
$angle = 0;
$_bx = imageTTFBbox($fsize,0,$font,$text);
$s = split("[\n]+", $text); // Array of lines
$nL = count($s); // Number of lines
$W = ($W==0)?abs($_bx[2]-$_bx[0]):$W;
$H = ($H==0)?abs($_bx[5]-$_bx[3])+($nL>1?($nL*$Leading):0):$H;
$im = imagecreate($W, $H) or die("Cannot Initialize new GD image
stream");
$background_color = imagecolorallocate($im, $bgcolor[0],
$bgcolor[1], $bgcolor[2]);
$text_color = imagecolorallocate($im, $color[0], $color[1],
$color[2]);
if ($Justify == 0){ //Justify Left
imagettftext($im, $fsize, $angle, $X, $fsize, $text_color,
$font, $text);
}
else {
$_b = imageTTFBbox($fsize,0,$font,$text);
$_H = abs($_b[5]-$_b[3]);
$__H=0;
for ($i=0; $i<$nL; $i++) {
$_b = imageTTFBbox($fsize,0,$font,$s[$i]);
$_W = abs($_b[2]-$_b[0]);
if ($Justify == 1) $_X = $W-$_W; // Justify Right
else $_X = abs($W/2)-abs($_W/2); // Justify Center
$__H += $_H;
imagettftext($im, $fsize, $angle, $_X, $__H, $text_color,
$font, $s[$i]);
$__H += $Leading;
}
}
header("Content-type: image/png");
imagepng($im);
}
imagettfJustifytext("Hello there\nMy name is Ricky\nByee");
?>
Mar 2 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hi,

The vertical position exceeds the height, so you should be able to
replace

$__H += $_H;

with

$__H += abs($_b[5] - $_b[3]);

There's also an alignment function at:

http://us.php.net/manual/en/function...bbox.php#60678

HTH,

John Peters

On Mar 2, 2:32 am, "J.J.Cale" <ji...@012.net.ilwrote:
The following code works partially.
If Justify is set to 0, I see 3 separate lines of text left justified

but when I try to right or center justify I only get the first line.
It is
justified but out of vertical position as well it seems. Can someone
correct
this or point me to a working function. TIA
Jim
// $Justify: 0=left 1=right 2=center - watch word wrap
<?php
function imagettfJustifytext($text, $font="CENTURY.TTF", $Justify=0,
$Leading=0, $W=0, $H=0, $X=0, $Y=0, $fsize=12,
$color=array(0x0,0x0,0x0), $bgcolor=array(0xFF,0xFF,0xFF)){
$angle = 0;
$_bx = imageTTFBbox($fsize,0,$font,$text);
$s = split("[\n]+", $text); // Array of lines
$nL = count($s); // Number of lines
$W = ($W==0)?abs($_bx[2]-$_bx[0]):$W;
$H = ($H==0)?abs($_bx[5]-$_bx[3])+($nL>1?($nL*$Leading):0):$H;
$im = imagecreate($W, $H) or die("Cannot Initialize new GD image
stream");
$background_color = imagecolorallocate($im, $bgcolor[0],
$bgcolor[1], $bgcolor[2]);
$text_color = imagecolorallocate($im, $color[0], $color[1],
$color[2]);
if ($Justify == 0){ //Justify Left
imagettftext($im, $fsize, $angle, $X, $fsize, $text_color,
$font, $text);
}
else {
$_b = imageTTFBbox($fsize,0,$font,$text);
$_H = abs($_b[5]-$_b[3]);
$__H=0;
for ($i=0; $i<$nL; $i++) {
$_b = imageTTFBbox($fsize,0,$font,$s[$i]);
$_W = abs($_b[2]-$_b[0]);
if ($Justify == 1) $_X = $W-$_W; // Justify Right
else $_X = abs($W/2)-abs($_W/2); // Justify Center
$__H += $_H;
imagettftext($im, $fsize, $angle, $_X, $__H, $text_color,
$font, $s[$i]);
$__H += $Leading;
}
}
header("Content-type: image/png");
imagepng($im);}

imagettfJustifytext("Hello there\nMy name is Ricky\nByee");
?>
Mar 2 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.