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";

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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);



?>

Link to comment
Share on other sites

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
            );

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.