quick question related to imagestring.

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Post Reply
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

quick question related to imagestring.

Post by Flamie »

So if I have a rectangle:

Code: Select all

$TopLeftX = whatever;
$TopLeftY = whatever2;
$width = we;
$height = we2;
and I have a string $message, is there an easy way for me to make it fit in that box (like having it go to the line when it reaches the end of the rectangle?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Pretty easy .. you'd need to work out the maximum length of text that will fit in the box (with imagefontwidth() ), then use wordwrap() .. or write your own wrapping function if wordwrap() isn't up to it (depends, wordwrap() isn't all that clever). That's if you're using PHP's builtin fonts. If you're using TTF, PS, etc fonts then you'll need to use the bounding box functions (imagettfbbox() for example), and loop through your text adding one letter at a time until you reach the maximum width, then draw the line, and move down to the next. Tedious, but it'll work.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Re: quick question related to imagestring.

Post by bokehman »

Flamie wrote:is there an easy way for me to make it fit in that box
Check this thread. I used precisely the method Onion2k describes.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

More...

Image

Code: Select all

<?php

function OutputTextAsImage($text, $image_width = 300, $colour = array(0,0,0), $background = array(255,255,159), $padding = NULL, $destination = NULL)
{
	$font = 5; // that's the biggest
	$line_height = ImageFontheight($font) + 5;
	$padding = (is_numeric($padding)) ? $padding : 10;
	$quality = 75; // 0 = worst, 100 = best
	$text = wordwrap($text, (($image_width - (1.5 * $padding))/ImageFontWidth($font)));
	$lines = explode("\n", $text);
	$image = imagecreate($image_width,((count($lines) * $line_height)) + ($padding * 2));
	$background = imagecolorallocate($image, $background[0], $background[1], $background[2]);
	$colour = imagecolorallocate($image,$colour[0],$colour[1],$colour[2]);
	imagefill($image, 0, 0, $background);
	$i = $padding;
	foreach($lines as $line){
		imagestring($image, $font, $padding, $i, trim($line), $colour);
		$i += $line_height;
	}
	$destination or header('Content-Type: image/jpeg');
	imagejpeg($image, null, $quality);
	imagedestroy($image);
	$destination or die;
}

$text = <<<END
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer commodo consequat lacus. 

Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
 
Vivamus eu neque. Maecenas ut erat in turpis ornare rutrum. Duis id leo. Proin elit tortor, porta a, elementum nec, ultrices sed, ante. 
END;

OutputTextAsImage($text)

?>
Post Reply