GD text justify new problem

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

Threaded View
This works if I pass the "quoted" string. But when I try to pass it
from the GET
it doesn't work. I assume the culprit is the call to split() but I
can't make it work.
I've tried variations on the split but no luck. I've also tried
tostring and strval. Can
someone show me the correct syntax for this or another solution? TIA
$str = isset($_GET['txt']) ? $_GET['txt']: "Sorry";
header("Content-type: image/png");
$img = imagecreate(400, 300) or die("Cannot Initialize new GD image
//$str = "Hello there\nMy name is Ricky\nByee";
imagettfJustifytext($img, $str, "CENTURY.TTF", 0);

function imagettfJustifytext($im, $text, $font, $Justify, $Leading=0,
$W=0, $H=0, $X=20, $Y=20, $fsize=18, $color=array(0x0,0x0,0x0),
$bgcolor=array(0xAA,0xAA,0xAA)) {
    $background_color = imagecolorallocate($im, $bgcolor[0], $bgcolor[1],
    $text_color = imagecolorallocate($im, $color[0], $color[1],
    $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;
    if ($Justify == 0){ //Justify Left
        imagettftext($im, $fsize, $angle, $X, $fsize+$Y, $text_color, $font,
    else {
        $_b = imageTTFBbox($fsize,0,$font,$text);
        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 += abs($_b[5]-$_b[3]);
            imagettftext($im, $fsize, $angle, $_X+$X, $__H+$Y,
$text_color, $font, $s[$i]);
            $__H += $Leading;

Re: GD text justify new problem

Greetings, J.J.Cale.
In reply to Your message dated Tuesday, March 4, 2008, 19:21:45,

Quoted text here. Click to load it

Found no errors. It's working for me.


Re: GD text justify new problem

Quoted text here. Click to load it

"it doesn't work" is a totally useless description of an error. It
doesn't tell us anything about precisely what way it "doesn't work"

If split works OK with the literal assigment, then that suggests that
the call to split() is correct, but that the difference is in the

What DO you get with the submitted data? What is in $_GET['txt']?

Try to at least make an effort to help people help you.

Site Timeline