Using function containing for loop within a foreach loop goes terribly wrong

I use this function to create transparent images with text written on them. There’s a for loop inside this function.
[php]
function createheader($string) {
$fontsize = 8;
$fontspace = 0.5;
$fontname = ‘Big-Bold’;
$filename_string = strtolower(substr(preg_replace("/[^a-zA-Z]+/", “”, $string),0,30));
$filename = ‘./images/headers/’ . $filename_string . ‘-’ . $fontname . $fontsize . ‘.png’;
//
if (!file_exists($filename)) {
$im = @imagecreatetruecolor(600, 12);
imagealphablending($im, true);
imagesavealpha($im, true);
imagefill($im,0,0,0x7fff0000);
$color_text = imagecolorallocate($im, 51, 51, 51);
$x = 0;
$y = 11;
for ($i = 0; $i < strlen($string); $i++){
$arr = imagettftext ($im, $fontsize, 0, $x, $y, $color_text, “include/” . $fontname . “.ttf”, strtoupper($string{$i}));
$x = $arr[4] + $fontspace;
}
imagepng($im,$filename);
imagedestroy($im);
}
return $filename;
}
[/php]
When I call this function in the situation below, the result is not what I expect. An image is created, but something goes wrong. When the text string is i.e. ‘robot’, the output is ‘robotrobotrobotrobotrobot’ without letter spacing. Instead of letter spacing, the whole string is repeated for each character.
[php]$xml = simplexml_load_file(‘data/nieuwsberichten.xml’);
foreach ($xml->children() as $second_gen) {
$header = createheader($second_gen[‘title’]);
//
echo “

<img src=”" . $header . “” height=“12” width=“600” alt="" . $second_gen[‘title’] . “”>

\n

\n";
foreach ($second_gen->children() as $third_gen) {
echo “<span class=“label_3”><a title=”" . $third_gen[‘description’] . “” href="?p=3&a=" . $third_gen[‘folder’] . “”>" . $third_gen[‘title’] . " <span class=“label_2”>" . $third_gen[‘description’] . “
\n”;
}
echo “

\n”;
}[/php]
I cant figure out how to alter the function to make it work like it should within a foreach loop.
Sponsor our Newsletter | Privacy Policy | Terms of Service