GD text justify

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • J.J.Cale

    #1

    GD text justify

    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 imagettfJustify text($text, $font="CENTURY. TTF", $Justify=0,
    $Leading=0, $W=0, $H=0, $X=0, $Y=0, $fsize=12,
    $color=array(0x 0,0x0,0x0), $bgcolor=array( 0xFF,0xFF,0xFF) ){
    $angle = 0;
    $_bx = imageTTFBbox($f size,0,$font,$t ext);
    $s = split("[\n]+", $text); // Array of lines
    $nL = count($s); // Number of lines
    $W = ($W==0)?abs($_b x[2]-$_bx[0]):$W;
    $H = ($H==0)?abs($_b x[5]-$_bx[3])+($nL>1?($nL*$ Leading):0):$H;
    $im = imagecreate($W, $H) or die("Cannot Initialize new GD image
    stream");
    $background_col or = imagecoloralloc ate($im, $bgcolor[0],
    $bgcolor[1], $bgcolor[2]);
    $text_color = imagecoloralloc ate($im, $color[0], $color[1],
    $color[2]);
    if ($Justify == 0){ //Justify Left
    imagettftext($i m, $fsize, $angle, $X, $fsize, $text_color,
    $font, $text);
    }
    else {
    $_b = imageTTFBbox($f size,0,$font,$t ext);
    $_H = abs($_b[5]-$_b[3]);
    $__H=0;
    for ($i=0; $i<$nL; $i++) {
    $_b = imageTTFBbox($f size,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($i m, $fsize, $angle, $_X, $__H, $text_color,
    $font, $s[$i]);
    $__H += $Leading;
    }
    }
    header("Content-type: image/png");
    imagepng($im);
    }
    imagettfJustify text("Hello there\nMy name is Ricky\nByee");
    ?>
  • petersprc

    #2
    Re: GD text justify

    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:



    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 imagettfJustify text($text, $font="CENTURY. TTF", $Justify=0,
    $Leading=0, $W=0, $H=0, $X=0, $Y=0, $fsize=12,
    $color=array(0x 0,0x0,0x0), $bgcolor=array( 0xFF,0xFF,0xFF) ){
    $angle = 0;
    $_bx = imageTTFBbox($f size,0,$font,$t ext);
    $s = split("[\n]+", $text); // Array of lines
    $nL = count($s); // Number of lines
    $W = ($W==0)?abs($_b x[2]-$_bx[0]):$W;
    $H = ($H==0)?abs($_b x[5]-$_bx[3])+($nL>1?($nL*$ Leading):0):$H;
    $im = imagecreate($W, $H) or die("Cannot Initialize new GD image
    stream");
    $background_col or = imagecoloralloc ate($im, $bgcolor[0],
    $bgcolor[1], $bgcolor[2]);
    $text_color = imagecoloralloc ate($im, $color[0], $color[1],
    $color[2]);
    if ($Justify == 0){ //Justify Left
    imagettftext($i m, $fsize, $angle, $X, $fsize, $text_color,
    $font, $text);
    }
    else {
    $_b = imageTTFBbox($f size,0,$font,$t ext);
    $_H = abs($_b[5]-$_b[3]);
    $__H=0;
    for ($i=0; $i<$nL; $i++) {
    $_b = imageTTFBbox($f size,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($i m, $fsize, $angle, $_X, $__H, $text_color,
    $font, $s[$i]);
    $__H += $Leading;
    }
    }
    header("Content-type: image/png");
    imagepng($im);}
    >
    imagettfJustify text("Hello there\nMy name is Ricky\nByee");
    ?>

    Comment

    Working...