Forum Moderators: coopster

Message Too Old, No Replies

Justify text with GD

         

YorkshireSteve

2:56 pm on Aug 31, 2007 (gmt 0)

10+ Year Member



Hi Guys!

I was just wondering if anyone had a facility to fully justify an block of text in an image created with GD?

Steve

phparion

4:50 am on Sep 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This GoooOogling [google.com.pk] might help!

[edited by: dreamcatcher at 6:12 am (utc) on Sep. 1, 2007]
[edit reason] Tidied up url. [/edit]

YorkshireSteve

10:03 am on Sep 3, 2007 (gmt 0)

10+ Year Member



Unfortunately not... I've been down that route and haven't found much.

There is something in one of the user contributions on the PHP site which will allow me to justify text left, right and center, but nothing to allow me to create a block which lines up to the left and right egde in a block.

It may be a case of splitting each word into individual text boxes and working out how many can fit on a line, then adjusting the spacing between them accordingly... Fun! :o\

rainborick

1:35 pm on Sep 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have no experience with th GD library and very little with PHP, but when I've had to write software to do justification in the past, the process required determining the length of the drawn line of text and then embedding/sprinkling additional <space> characters between the words to achieve the desired length. A text measuring function is sometimes built into libraries like GD, but you'll have to check. Otherwise, you'll have to determine the length through other means, such as crawling the font file for character width information. A shortcut approach would be to rely on a mono-spaced font.

Gian04

3:19 pm on Sep 3, 2007 (gmt 0)

10+ Year Member



This one should work :

function textleftpos($fontsize, $text_to_display) {
$imagewidth = 60;
$text_left_position = ($imagewidth - (intval(((strlen($text_to_display) * imagefontwidth($fontsize)) / 2))));
return $text_left_position;
}

$imagename = "image_name.png";
$image1 = @imagecreatefrompng($imagename);
$text_top_pos = "150";
$maroon_text_color = imagecolorallocate($image1, 146, 2, 2);

imagestring($image1, 5, textleftpos(5, "Text to Display"), $text_top_pos, "Text to Display", $maroon_text_color);

header("Content-type: image/png");
imagepng($image1);
imagedestroy($image1);

YorkshireSteve

4:06 pm on Sep 5, 2007 (gmt 0)

10+ Year Member



The best approach would be to display the text as text, not as an image! Unfortunately though this isn't going to happen as the designers who want the site are insisting on this font etc etc!

Basically, the road I'm going down with this is as follows:

  1. Split the text string into words, then into individual characters. This will allow me to fine-tune the kerning (space between letters).
  2. I then work out the whole width of the word, and add these to an array of words for the current line. I also record this width into an array holding the words and their widths for later.
  3. These then go into an array of all lines, so I can later loop through these.
  4. Go through each line, and calculate the total width of all words, plus spaces in-between. The spacing should be padded out evenly based on how much space is left at the end of each line.
  5. Add the words to the canvas taking into account the additional spacing calculated above.

Once I've got it sorted I'll try and post some tidied-up code here for others.