View Full Version : GD Library Phantom Line Break Issue

12-08-2008, 10:34 AM
Hi everyone.

I am told that somebody here might be able to offer some advice on an problem I have with some GD Library code.

I have some php code that generates two names, which I then reference in the gd script to output the two names onto an image.

The problem is that there is a line break magically appearing after the first name.

The code for the names is this:

$name = "$first_array[$arnd] $second_array[$nrnd]";

I then insert these into the gd library code as this:

imagettftext ($im, 20, 0, 150, 150, $color, $font, $name);

From experiments, it seems to be something to do with the [$arnd] part immediately following the $first_array which is causing the line break.

Is there any way to force the text onto one single line, or another way to get around the problem?

Best regards.

12-08-2008, 11:20 AM
trim it first:

$name = trim($first_array[$arnd]) . ' ' . trim($second_array[$nrnd]);

I'm assuming the problem is that the $arr[$arnd] or $arr[$nrnd] keys contain newlines. I'd need to see those to determine that, but I doubt that the GD is adding a newline. Its possible that somewhere you're line wrapping, but the GD doesn't do that by default.

12-08-2008, 11:39 AM
The two files they read from are each a list of names in this format


and so on...

When I run the script without GD Library and just echo the result i.e.

$name = "$first_array[$arnd] $second_array[$nrnd]";

echo "$name";

it all appears on the same line.

12-08-2008, 11:55 AM
Browsers ignore whitespace. Check the source code, if it shows each on its own item then you've got the newlines.

File handling with the file() function will preserve the newline of each item, unless you've got a new version of PHP and are adding FILE_IGNORE_NEW_LINES. I'd say it sounds pretty good like thats where you're problem is coming from.

12-08-2008, 12:45 PM
Thanks for your help on this.

Apologies, I'm fairly new to PHP.

Would you be able to show an example of how I'd implement that function into the code ?

12-08-2008, 01:41 PM
By using file?

$aFileLines = file('/path/to/file.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

This would populate an array with each line of a file representing each element of an array. It will strip off the newline and skip any empty lines. To do this old school, you'd do:

$aFileLines = file('/path/to/file.txt');
array_map($aFileLines, 'trim');

Doesn't delete empty items though.

12-08-2008, 03:14 PM
Thanks very much Fou-Lu

That was a great help