Jump to content

[SOLVED] Creating a star using the GD Library


Recommended Posts

hey there, for some extra marks in a class i'm trying to use the gd library to create a start for the purpose of a 5 star rating instead of just using png's stored in a folder.  i believe i had the code correct but it is not working for some reason.  any help?  thanks!

	
	//set up array for point in star
	echo "star start";
	$values = array(
				array(0,5),
				array(5,5),
				array(7.5,0),
				array(10,5),
				array(15,5),
				array(10,9),
				array(5,9),
				array(7.5,10),
				array(2,15),
				array(13,15),
				);

	//create image
	$img = imagecreatetruecolor(15,15);

	//colors
	$bg = imagecolorallocate($img,255,255,255);
	$black = imagecolorallocate($img,0,0,0);

	//draw star
	imagefilledpolygon($img,$values,5,$black);

	//flush image


	echo "star end";

I don't really get an error per say just this displays....see img attatched.   

 

the stars you see are just called png's but at the very top is where i'm trying to draw a star.

 

[attachment deleted by admin]

Ok, there are two problems. One, bluesky mentioned. You need a function to output the image to the browser.

 

The second problem is that, you cannot directly output it to the page like you are doing. Because the result of which would be showing the "binary" that represents that image! To do this properly, you'd typically have a seperate php file, with:

 


header('Content-type: image/jpeg');

//create image
$img = imagecreatetruecolor(15,15);

//colors
$bg = imagecolorallocate($img,255,255,255);
$black = imagecolorallocate($img,0,0,0);

//draw star
imagefilledpolygon($img,$values,5,$black);
imagejpeg($img);

 

And create a <img src="otherScript.php" /> tag in the page where you want the image to display.

 

As far as I know, there's no way to directly convert the "binary" result into a visible image (and avoidig the dual script approach).

ok so that deffinately worked, or at least made some progress.  it is now printing a 15x15 square instead of the points specified.  inside of starimage.php i have:

<?php

header('content-type: image.png');

//set up array for point in star
	$values = array(
				array(0,5),
				array(5,5),
				array(7.5,0),
				array(10,5),
				array(15,5),
				array(10,9),
				array(5,9),
				array(7.5,10),
				array(2,15),
				array(13,15),
				);

	//create image
	$img = imagecreatetruecolor(15,15);

	//colors
	$bg = imagecolorallocate($img,255,255,255);
	$black = imagecolorallocate($img,0,0,0);

	//draw star
	imagefilledpolygon($img,$values,5,$black);

	//flush image
	imagepng($img);



?>

 

 

i'm thinking that it might be working but my backgournd is black and the star is black....soo what values should i put in to make tha background white for now(or clear if possible)  and the star black.  thanks

 

attatched picture shows new result

 

[attachment deleted by admin]

k so that now replaces the black square with a white one.  not too sure now if the star just isn't printing and therefore not showing or if the white square is ontop of the star

<?php

header('content-type: image.png');

//set up array for point in star
	$values = array(
				array(0,5),
				array(5,5),
				array(7.5,0),
				array(10,5),
				array(15,5),
				array(10,9),
				array(5,9),
				array(7.5,10),
				array(2,15),
				array(13,15),
				);

	//create image
	$img = imagecreatetruecolor(15,15);

	//colors
	$bg = imagecolorallocate($img,255,255,255);
	$black = imagecolorallocate($img,0,0,0);

	imagefill($img,0,0,$bg);
	//draw star
	imagefilledpolygon($img,$values,5,$black);

	//flush image
	imagepng($img);



?>

change this

		$values = array(
				array(0,5),
				array(5,5),
				array(7.5,0),
				array(10,5),
				array(15,5),
				array(10,9),
				array(5,9),
				array(7.5,10),
				array(2,15),
				array(13,15),
				);

To

$values = array(
				0,5,
				5,5,
				7.5,0,
				10,5,
				15,5,
				10,9,
				5,9,
				7.5,10,
				2,15,
				13,15
            );

ok that's great.  i think it's done.  i just have to change the order of the values in the array so that it draws properly.  i've messed with it a bit and it just needs some tweaking.  thanks so much for you help!!  once again this site and you guys have been an excellent resource!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.