bluebutterflyofyourmind Posted November 4, 2007 Share Posted November 4, 2007 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 https://forums.phpfreaks.com/topic/75963-solved-creating-a-star-using-the-gd-library/ Share on other sites More sharing options...
BlueSkyIS Posted November 4, 2007 Share Posted November 4, 2007 so what's not working? errors messages? Link to comment https://forums.phpfreaks.com/topic/75963-solved-creating-a-star-using-the-gd-library/#findComment-384533 Share on other sites More sharing options...
bluebutterflyofyourmind Posted November 4, 2007 Author Share Posted November 4, 2007 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] Link to comment https://forums.phpfreaks.com/topic/75963-solved-creating-a-star-using-the-gd-library/#findComment-384535 Share on other sites More sharing options...
BlueSkyIS Posted November 4, 2007 Share Posted November 4, 2007 yeah, you just drew an image with imagefilledpolygon() but you haven't sent anything to the browser. you need to send your image to the browser or a file using imagejpeg($img) or similar. Link to comment https://forums.phpfreaks.com/topic/75963-solved-creating-a-star-using-the-gd-library/#findComment-384537 Share on other sites More sharing options...
Guest Posted November 4, 2007 Share Posted November 4, 2007 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 https://forums.phpfreaks.com/topic/75963-solved-creating-a-star-using-the-gd-library/#findComment-384538 Share on other sites More sharing options...
BlueSkyIS Posted November 4, 2007 Share Posted November 4, 2007 good point on the second script. i assumed it was, but not apparently given the echo's. or if it is a second script, the echo's will certainly hose the image. afaik, no way to avoid the dual script approach. Link to comment https://forums.phpfreaks.com/topic/75963-solved-creating-a-star-using-the-gd-library/#findComment-384541 Share on other sites More sharing options...
bluebutterflyofyourmind Posted November 4, 2007 Author Share Posted November 4, 2007 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 https://forums.phpfreaks.com/topic/75963-solved-creating-a-star-using-the-gd-library/#findComment-384543 Share on other sites More sharing options...
BlueSkyIS Posted November 4, 2007 Share Posted November 4, 2007 you may need to use imagefill() for a background after creating the image. Link to comment https://forums.phpfreaks.com/topic/75963-solved-creating-a-star-using-the-gd-library/#findComment-384552 Share on other sites More sharing options...
bluebutterflyofyourmind Posted November 4, 2007 Author Share Posted November 4, 2007 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 https://forums.phpfreaks.com/topic/75963-solved-creating-a-star-using-the-gd-library/#findComment-384555 Share on other sites More sharing options...
marcus Posted November 4, 2007 Share Posted November 4, 2007 header('content-type: image.png'); should be header('content-type: image/png'); Link to comment https://forums.phpfreaks.com/topic/75963-solved-creating-a-star-using-the-gd-library/#findComment-384556 Share on other sites More sharing options...
bluebutterflyofyourmind Posted November 4, 2007 Author Share Posted November 4, 2007 valid point but still same results Link to comment https://forums.phpfreaks.com/topic/75963-solved-creating-a-star-using-the-gd-library/#findComment-384558 Share on other sites More sharing options...
BlueSkyIS Posted November 4, 2007 Share Posted November 4, 2007 is 7.5 a valid coordinate? Link to comment https://forums.phpfreaks.com/topic/75963-solved-creating-a-star-using-the-gd-library/#findComment-384561 Share on other sites More sharing options...
Daukan Posted November 4, 2007 Share Posted November 4, 2007 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 https://forums.phpfreaks.com/topic/75963-solved-creating-a-star-using-the-gd-library/#findComment-384563 Share on other sites More sharing options...
Daukan Posted November 4, 2007 Share Posted November 4, 2007 oops and this imagefilledpolygon($img,$values,5,$black); TO imagefilledpolygon($img,$values,10,$black); Link to comment https://forums.phpfreaks.com/topic/75963-solved-creating-a-star-using-the-gd-library/#findComment-384564 Share on other sites More sharing options...
BlueSkyIS Posted November 4, 2007 Share Posted November 4, 2007 ah, someone with imagefilledpolygon experience. Link to comment https://forums.phpfreaks.com/topic/75963-solved-creating-a-star-using-the-gd-library/#findComment-384565 Share on other sites More sharing options...
bluebutterflyofyourmind Posted November 4, 2007 Author Share Posted November 4, 2007 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! Link to comment https://forums.phpfreaks.com/topic/75963-solved-creating-a-star-using-the-gd-library/#findComment-384569 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.