Page 1 of 1

Creating numbered list in an image

Posted: Sat Nov 06, 2010 8:21 am
by InGale
Hi!
I'm trying to create an image which will have a numbered list, while the numbers are going down from 1 to 20. Something like that:
[text]
1
2
3
4
etc...[/text]
The image is created ok, until I try this code to make the list:

Code: Select all

$font_number = 5;
for($place = 1; $place = 20; $place++)
{
	for($y_position=40; $y_position < ((($y_position + imagefontheight($font_number))+5)*20); $y_position += 10)
	{
		imagestring($image, $font_number, 40, $y_position, $place, $draw_color);
	}
}
What am I doing wrong and how can I make it work?

Thanks!

Re: Creating numbered list in an image

Posted: Sat Nov 06, 2010 10:02 am
by requinix
For those of us who don't want to write a test script to see the problem in action, how about you describe it to us?

What's "not ok" about it?

Re: Creating numbered list in an image

Posted: Sat Nov 06, 2010 11:09 am
by InGale
Well, the entire code is this:

Code: Select all

//Creating base image
$image_width = 1000;
$image_height = 400;
$image = imageCreate($image_width, $image_height);

//Stating line thickness and colors
imagesetthickness($image, 2);
$back_color = imageColorAllocate($image, 56, 141, 192); //BG color of the image
$draw_color = imageColorAllocate($image, 0, 0, 0); //lines and text color
$rect_color = imageColorAllocate($image, 43, 109, 149); //image border color

//Creating rectangular border for the image
imageRectangle($image, 0, 0, imageSX($image) - 1, imageSY($image) - 1, $rect_color);
	
$font_number = 5;
$text = "Placing chart";
$x_position = ($image_width - (strlen($text) * imagefontwidth($font_number))) / 2;
imagestring($image, $font_number, $x_position, 20, $text, $draw_color);

for($place = 1; $place = 20; $place++)
{
	for($y_position=40; $y_position < ((($y_position + imagefontheight($font_number))+5)*20); $y_position += 10)
	{
		imagestring($image, $font_number, 40, $y_position, $place, $draw_color);
	}
}
		

//Stating image format (options: jpeg, gif, png, wbmp). In this case - PNG.
header('Content-Type: image/png');
imagepng($image);
imageDestroy($image);
I save the file as "php_image.php" and then embed that in my default.php file: [text]<img src = "php_image.php">[/text]
It creates a 1000*400 pixels image. If I remove both loops, it just shows the "Placing chart" text in the middle of the image (which is good). Now I need to create the numbered list on the left side, about 20 pixels on the X axis and 40 on the Y.
Of course I can do it manually, but it's to many lines of code and not an elegant solution. So I'm trying to make it with a loop, but it doesn't work so far...

Re: Creating numbered list in an image

Posted: Sat Nov 06, 2010 12:58 pm
by requinix
You still haven't explained what "doesn't work" means. When I mentioned a test script I wasn't asking for the actual code instead of a description.

Code: Select all

$place = 20

Code: Select all

$y_position < ((($y_position + imagefontheight($font_number))+5)*20)
You created an infinite loop. Twice.

I think all what you're aiming for is more like

Code: Select all

for($place = 1; $place <= 20; $place++)
{
		$y_position = 30 + $place * 10;
		imagestring($image, $font_number, 40, $y_position, $place, $draw_color);
}
If the 10 is supposed to be vertical spacing (grabbed from the $y_position+=10 earlier) I suggest imagefontheight() + a couple pixels instead.

Re: Creating numbered list in an image

Posted: Sun Nov 07, 2010 5:25 am
by InGale
Yes, thank you! That was exactly what I needed!

Sorry I misunderstood you with the "doesn't working" part... The problem was that the image just wasn't displaying anymore. Probably because of the infinite loop... Everything is working now, thanks to you!

Cheers! :D