Swan Posted March 20, 2008 Share Posted March 20, 2008 I have a function which creates a polygon of blue color. I call this function inside a loop of 128 iterations. For every iteration, i change the value of x and y so that my polygon is rendered on new location. This goes fine and i get 128 new polygons on my image after running the script. But if change the number to 129, the 129th polygon is transparent or what i cannot guess.It just does not display or render at all. Is this a bug or some mistake with my code? ??? I am using php version 5.1.4 that came with WAMP5 Version 1.6.3 my code is - <?php $im = imagecreate(600, 400); $x = 20; $y = 350; for($i=1;$i<=128;$i++) //create a polygon 128 times { drawPoly($x,$y,$im); $x = $x+2; $y = $y-2; } header('Content-type: image/png'); imagepng($im); imagedestroy($im); function drawPoly($xCoord,$yCoord,$im) { $x1 = $x2 = $xCoord; $x3 = $x4 = $xCoord + 70; $y1 = $y4 = $yCoord; $y2 = $y3 = $yCoord + 30; $Poly = array(0 => $x1,1 => $y1,2 => $x2,3 => $y2,4 => $x3,5 => $y3,6 => $x4,7 => $y4); $bg = imagecolorallocate($im, 150, 150, 150); $blue = imagecolorallocate($im, 0, 0, 255); imagefilledpolygon($im, $Poly, 4, $blue); } ?> Please help me Link to comment https://forums.phpfreaks.com/topic/97124-filled-polygon-is-not-renders-129th-time-in-same-image/ Share on other sites More sharing options...
mraiur Posted March 20, 2008 Share Posted March 20, 2008 I have a question . Why do you create 128 poligons when u can create 1 ? I mean you have the first point get the top ending points after the loop and then draw the poligon ... Link to comment https://forums.phpfreaks.com/topic/97124-filled-polygon-is-not-renders-129th-time-in-same-image/#findComment-497014 Share on other sites More sharing options...
Swan Posted March 21, 2008 Author Share Posted March 21, 2008 well i am working on something where i need to create number of polygons depending on the array passed by the user. If the array count is 5, then 5 polygons should be created, if array count is 129, 129 polygons should be created. Then in each polygone, i am displaying the value of the array. Link to comment https://forums.phpfreaks.com/topic/97124-filled-polygon-is-not-renders-129th-time-in-same-image/#findComment-497295 Share on other sites More sharing options...
Swan Posted March 21, 2008 Author Share Posted March 21, 2008 The issue is solved. It was the problem with the code. i took this line $bg = imagecolorallocate($im, 150, 150, 150); outside the function drawPoly(), and everything worked as expected. the new updated code is - <?php $im = imagecreate(600, 400); $bg = imagecolorallocate($im, 150, 150, 150); $x = 20; $y = 350; for($i=1;$i<=150;$i++) //create a polygon 128 times { drawPoly($x,$y,$im); $x = $x+2; $y = $y-2; } header('Content-type: image/png'); imagepng($im); imagedestroy($im); function drawPoly($xCoord,$yCoord,$im) { $x1 = $x2 = $xCoord; $x3 = $x4 = $xCoord + 70; $y1 = $y4 = $yCoord; $y2 = $y3 = $yCoord + 30; $Poly = array(0 => $x1,1 => $y1,2 => $x2,3 => $y2,4 => $x3,5 => $y3,6 => $x4,7 => $y4); $blue = imagecolorallocate($im, 0, 0, 255); imagefilledpolygon($im, $Poly, 4, $blue); } ?> But i still want to know why this happened and why it worked when i took that line outside the function? Link to comment https://forums.phpfreaks.com/topic/97124-filled-polygon-is-not-renders-129th-time-in-same-image/#findComment-497332 Share on other sites More sharing options...
Barand Posted March 21, 2008 Share Posted March 21, 2008 When you create an image with imagecreate() it creates a paletted image with a 256 color pallette. If each time you you allocate a background and foregraound color you run out after 128. Define the the colors once, instead of defining blue 128 times, and use them for each polygon and you can have as many as you want. Link to comment https://forums.phpfreaks.com/topic/97124-filled-polygon-is-not-renders-129th-time-in-same-image/#findComment-497393 Share on other sites More sharing options...
mraiur Posted March 21, 2008 Share Posted March 21, 2008 and still my question stays ? I mean when the direction of the polygon is hard-cored inside the the function $x+2 and $y-2 to alocate 128 polygons when u can make the loop to get the last points so u can draw 1 polygon ? Link to comment https://forums.phpfreaks.com/topic/97124-filled-polygon-is-not-renders-129th-time-in-same-image/#findComment-497515 Share on other sites More sharing options...
rhodesa Posted March 21, 2008 Share Posted March 21, 2008 and still my question stays ? I mean when the direction of the polygon is hard-cored inside the the function $x+2 and $y-2 to alocate 128 polygons when u can make the loop to get the last points so u can draw 1 polygon ? What are you talking about? They want to draw 129 polygons on an image, not 1. Link to comment https://forums.phpfreaks.com/topic/97124-filled-polygon-is-not-renders-129th-time-in-same-image/#findComment-497518 Share on other sites More sharing options...
mraiur Posted March 21, 2008 Share Posted March 21, 2008 ok i will explain even more . whit that function whit hard-cored increment it doest mather if you draw 128 polygons ... You need 4 point to draw the polygon so you have the 2 points from the start where you havent started the loop and you cant get the other two needed after looping and saving the end points . example: whit 128 polygons : - - - - result ----- and drawing 1 polygon after the loop ----- i didn thied it but this s more logical... Link to comment https://forums.phpfreaks.com/topic/97124-filled-polygon-is-not-renders-129th-time-in-same-image/#findComment-497631 Share on other sites More sharing options...
rhodesa Posted March 21, 2008 Share Posted March 21, 2008 ok i will explain even more . whit that function whit hard-cored increment it doest mather if you draw 128 polygons ... You need 4 point to draw the polygon so you have the 2 points from the start where you havent started the loop and you cant get the other two needed after looping and saving the end points . example: whit 128 polygons : - - - - result ----- and drawing 1 polygon after the loop ----- i didn thied it but this s more logical... He is using a function to draw the polygons inside the loop. You may just be a little confused because his function comes after his loop. Link to comment https://forums.phpfreaks.com/topic/97124-filled-polygon-is-not-renders-129th-time-in-same-image/#findComment-497647 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.