Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    May 2003
    Location
    in a small damp cupboard
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    aligning text using imagettftext() under php4.3.2/GDLIB2/FreeType2

    Hey,

    here's my problem:

    I want to create an image with some user-submitted text but the text needs to be aligned to the right. On php.net i found the following code that looks like it should do the trick but i can't get my head around how to use it:

    PHP Code:
    function ImageTextLength(&$bbox)
    {
     
    $deltax $bbox[2] - $bbox[0];
    $deltay $bbox[3] - $bbox[1];
     return 
    sqrt(pow($deltax,2) + pow($deltay,2));
    }

    function 
    ImageTextHeight(&$bbox)
    {
    $deltax $bbox[6] - $bbox[0];
     
    $deltay $bbox[7] - $bbox[1];
    return 
    sqrt(pow($deltax,2) + pow($deltay,2));
    }

    function 
    ImageTtfTextAlign(&$image$font_size$angle_degrees$x$y$colour$font$text$length$align)
    {  
     
    $align strtoupper($align);
       
     if (
    strcmp($align"LEFT"))
     {
       
    $angle_radians $angle_degrees pi() / 180;
       
    $bbox imagettfbbox($font_size$angle_degrees$font$text);
       
    $text_length ImageTextLength($bbox);
     
       if (!
    strcmp($align"CENTRE"))
         
    $adjustment = ($length $text_length) / 2;
       elseif (!
    strcmp($align"RIGHT"))
         
    $adjustment $length $text_length;
       else
         
    $adjustment 0;
     
     
    $x += (int)round($adjustment cos($angle_radians));
       
    $y -= (int)round($adjustment sin($angle_radians));
     }
    imagettftext($image$font_size$angle_degrees$x$y$colour$font$text);
    }   

    function 
    ImageAddBbox($bbox1$bbox2$angle_degrees)
    {
     
    $angle_radians $angle_degrees pi() / 180;

     
    $new_length max(ImageTextLength($bbox1), ImageTextLength($bbox2));
     
    $new_height ImageTextHeight($bbox1) + ImageTextHeight($bbox2);
     
     
    $bbox[0] = 0;
     
    $bbox[1] = 0;
    $bbox[2] = (int)ceil($new_length cos($angle_radians));
     
    $bbox[3] = (int)ceil($new_length sin($angle_radians));
     
    // add 5 pixels to space lines out
     
    $bbox[6] = (int)ceil(- ($new_height 5) * sin($angle_radians));
     
    $bbox[7] = (int)ceil(- ($new_height 5) * cos($angle_radians));
     
    $bbox[4] = $bbox[2] + $bbox[6];
     
    $bbox[5] = $bbox[3] + $bbox[7];
       
     return 
    $bbox;
    }

    function 
    ImageTtfBboxWrap($font_size$angle$font$text$max_length)
    {
     if (
    strlen($text) == 0)
     {
       for (
    $i 0$i 8$i++)
         
    $bbox[$i] = 0;
       return 
    $bbox;
    }

     
    $subtext $text;

     do
     {
       
    $bbox imagettfbbox($font_size$angle$font$subtext);

       
    $text_length ImageTextLength($bbox);

       if (
    $text_length $max_length)  
       {
         
    $text_cutoff = (int)(($max_length $text_length) * strlen($subtext));
         
    $subtext substr($subtext0strrpos(substr($subtext0$text_cutoff), " "));
       }
    } while (
    $text_length $max_length);

     
    $bbox_rest ImageTtfBboxWrap($font_size$angle$fontsubstr($textstrlen($subtext) + 1), $max_length);
     return 
    ImageAddBbox($bbox$bbox_rest$angle);
    }

    function 
    ImageTtfTextWrap(&$image$font_size$angle_degrees$x$y$colour$font$text$max_length)
    {
    ImageTtfTextWrapAndAlign($image$font_size$angle_degrees$x$y$colour$font$text$max_length"LEFT");
    }

    function 
    ImageTtfTextWrapAndAlign(&$image$font_size$angle_degrees$x$y$colour$font$text$max_length$align)
    {
     if (
    strlen($text) == 0)
     {
       for (
    $i 0$i 8$i++)
         
    $bbox[$i] = 0;

       return 
    $bbox;
     }

     
    $subtext $text;

    do
     {
       
    $bbox imagettfbbox($font_size$angle_degrees$font$subtext);

       
    $text_length ImageTextLength($bbox);

       if (
    $text_length $max_length)    
       {
         
    // The following assumes a fixed width font
         
    $text_cutoff = (int)(($max_length $text_length) * strlen($subtext));
         
    $subtext substr($subtext0strrpos(substr($subtext0$text_cutoff), " "));
       }
    } while (
    $text_length $max_length);
       
     
    $text_height ImageTextHeight($bbox);
     
    $angle_radians $angle_degrees pi() / 180;
     
    $bbox_rest ImageTtfTextWrapAndAlign($image$font_size$angle_degrees$x$y$colour$fontsubstr($textstrlen($subtext) + 1), $max_length$align);
     
    $text_height ImageTextHeight($bbox_rest);

     
    imagettftextalign($image$font_size$angle_degrees$x $text_height sin($angle_radians), $y $text_height cos($angle_radians), $colour$font$subtext$max_length$align);  

     return 
    ImageAddBbox($bbox$bbox_rest$angle_degrees);

    i'm probably being dense but help would be appreciated.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you should check imagettfbbox in the manual for explainations of the returned indices - the rest is pretty simple co-ordinate mathematics.

    the func you have there is probably more than you really need as it includes text angles (not enough time to check the trig so I presume it is correct)

    you might also want to look at

    imagefttext and
    imageftbbox

    if you have a version of GD above about 2.0.4 as the fttext function includes a last parameter array for defining line height for linebreaks in input.

    Subnote: I am not going to write your code for you, I hope to only put you on the right track for learning.
    Ökii - formerly pootergeist
    teckis - take your time and it'll save you time.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •