Zend - The PHP Company




Graphics

Add Code


full "text on image" function. Center and wrap text.  

Type: code fragment
Added by: phpfarm
Entered: 24/05/2001
Last modified: 05/12/2000
Rating: - (fewer than 3 votes)
Views: 9008
ok, well as you may have noticed, imagepstext, and imagettftext do not word wrap. I ended up writing THIS code to combat that, and also to help in ALL my sites. With very little work it can be changed to do lots of cool things with text. This SHOULD work, i dont THINK it is an older copy...


<?php
function draw_text($boundry_x_size$boundry_y_size$font$font_location$font_size$text$bg_color$text_color$space$tight$angle$alias)
{
// was having probs with this so:
    
settype($font_size"integer");
    
settype($space"integer");
    
settype($tight"integer");
    
settype($alias"integer");
//when the imagepstext encounters a n, it goes CRAZY so:
$text ereg_replace("rn","n",$text)


//create the image, fonts, colors... (the nice thing about the colors is that the function converts them from HTML to RGB for you!!!
$img imagecreate($boundry_x_size$boundry_y_size);
$font imagepsloadfont($font_location);
$bg imagecolorallocate($imghexdec(substr($bg_color02)), hexdec(substr($bg_color22)), hexdec(substr($bg_color42)));
$color imagecolorallocate($imghexdec(substr($text_color02)), hexdec(substr($text_color22)), hexdec(substr($text_color42)));

//set starting variables
$place_holder=0;
$place=0;
$last_offset 0;
//find the middle of the image (for later so we can center it)
$halfx imagesx($img)/2;
//if you set this to 0 then the text apears ON TOP of the graphic (meaning it doesnt show).
$space_between_lines $font_size;

//i forget WHY i do this exactly :(
trim($text);
$text .= " ";


//make an array containing everything UP UNTIL a newline and go through the array
$returns explode("n"$text);
while(list(
$k$text) = each($returns))
{
    while (
$offset strpos($text" "$place)) // find the next space starting from the $place we are now
    
{
    
$line=imagepsbbox(substr($text$place_holder$offset $place_holder), $font$font_size$space$tight$angle); //find the size of the text if it were to be added to the current
    
$size $line[2]-$line[0];
//if its smaller than the boundry size, then keep going until it is too big
        
if ($size <= $boundry_x_size)
        {
        
$place $offset+1// make $place where we are currently plus 1
        
$last_offset $offset;
        }
        else
        {
//if it is too big, we are going to go BACK to the LAST space char and print THAT on the image, and then keep going
//find the size of the text if it were to be added    
$line=imagepsbbox(substr($text$place_holder$last_offset $place_holder), $font$font_size$space$tight$angle);
//get the place where the text should start so it will be centered (ie so it stops the same distance from the end as it starts)
        
$centerx $halfx - (($line[2]-$line[0])/2);
//was having some trouble with this so:        
settype($centerx"integer");
//make a variable that will hold the text
        
$insert_text substr($text$place_holder$last_offset $place_holder);
//just some error checking i commented out
//echo $insert_text. "<br>";

//print the text on the image
        
imagepstext($img$insert_text$font$font_size$color$bg$centerx$space_between_lines$space$tight$angle$alias);

//set placeholder to the LAST offset of the text, plus the space so that next time it will start at the next set of words
        
$place_holder=$last_offset+1;
//make sure next time, the text is written under the current text
        
$space_between_lines += $font_size 1;
        }
    }
//now do this to the text that is left (which will be one line or less)(truthfully i dont know why i put this here, im not sure if it even REALLY does something.... but hey- doesnt hurt, and im too lazy to think about it now

$line imagepsbbox(substr($text$place_holder), $font$font_size$space$tight$angle);
$centerx $halfx - (($line[2]-$line[0])/2);
settype($centerx"integer");
$insert_text substr($text$place_holder);
ltrim($insert_text);

//echo $insert_text. "<br>";

imagepstext($img$insert_text$font$font_size$color$bg$centerx$space_between_lines$space$tight$angle$alias);

$space_between_lines += $font_size 1;
$place_holder=0;
$place=0;
$last_offset 0;
}



imageJPEG($img);
ImageDestroy($img); 
}
?>


Usage Example




Rate This Script





Search



This Category All Categories