Page 1 of 1

Images from text

Posted: Mon Aug 16, 2004 2:10 pm
by AMMullan
Hey all,

I'm trying to play around with making images using GD and libttf2.
I've got the following code but when I try to load the page I get:
The image “http://skorpion/gd.php” cannot be displayed, because it contains errors.

Code: Select all

<?php
                                                                                                                                               
//Create an empty array
$length_array = array();
                                                                                                                                               
//Dissect $text
$fragments = explode("\n",$text);
$fragments_count = count($fragments);
for ($i = 0; $i <=$fragments_count; $i++)
   &#123;
   array_push($length_array, strlen($fragments&#1111;$i]));
   &#125;
                                                                                                                                               
//Sort the array so the highest value is first
rsort($length_array);
                                                                                                                                               
//find out how many lines there are in the string.
$line_count = count($length_array);
                                                                                                                                               
//Image Parameters
$w = $length_array&#1111;0] * 10;
$h = $line_count * 25;
$size = 10;
$xpos = 5;
$ypos = 20;
                                                                                                                                               
$im = imagecreate($w, $h);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
                                                                                                                                               
imagettftext($im, $size, 0, $xpos, $ypos, $black, "quick4.ttf", $text);
                                                                                                                                               
header("Cache-control: private");
header("Content-Type: image/jpeg");
imagejpeg($im);
imagedestroy($im);
                                                                                                                                               
?>
Has anyone got any ideas why this is happening?

Posted: Mon Aug 16, 2004 2:42 pm
by markl999
Change:
$fragments_count = count($fragments);
to:
$fragments_count = count($fragments)-1;

Also i don't see where you set $text? Your code worked fine for me with the above change and i also added a test line at the top:
$text = "hello\nworld";

Usually doing a 'view source' on the page that says '....contains errors' will show you what's wrong.