Images from text

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
AMMullan
Forum Newbie
Posts: 1
Joined: Mon Aug 16, 2004 2:10 pm
Location: New Zealand
Contact:

Images from text

Post 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?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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.
Post Reply