Page 1 of 1

Create polygon and loop

Posted: Tue Dec 02, 2014 2:20 pm
by kumarrana
I am drawing polygon using following functions. Code works just fine but I would like to create multiple of them in one HTML page. I have tried looping and changing the $pageHeight every-time it loops. It did not work. Anyone have any idea how I can create multiple of similar polygon using sample script below? I am attaching sample source code and the result. Thank you for help in advance. (Note:this is sample only and would not work if you copy and paste)

Code: Select all

$image = imagecreatetruecolor($paneWidth, $pageHeight);
$background = imagecolorallocate($image, 255, 255, 255);        
imagefill($image, 0, 0, $background);
$textColor = imagecolorallocate($image, 0, 0, 0);    
drawVector($x_1, $y_1, $x_2, $y_2, $x_3, $y_3, $x_4, $y_4);  
imagefilledpolygon($image,$createVector, 4, $polygonFill); 
imagestringup($image, 3,($x_1 + $x_2)/2, $y_1-5,$CarrierLabel,$textColor); 
header('Content-type: image/png');
imagepng($image);  
imagedestroy($image);    

Re: Create polygon and loop

Posted: Tue Dec 02, 2014 2:53 pm
by requinix
Copy the code that draws the polygon.

Code: Select all

drawVector($x_1, $y_1, $x_2, $y_2, $x_3, $y_3, $x_4, $y_4);
imagefilledpolygon($image,$createVector, 4, $polygonFill);
Probably the part that writes the string too.

Code: Select all

imagestringup($image, 3,($x_1 + $x_2)/2, $y_1-5,$CarrierLabel,$textColor);
You'll have to adjust the numbers/variables as needed to put them where you want in the image, and of course you'll have to change the size of the image to fit however many more polygons you want to add to it.

Re: Create polygon and loop

Posted: Tue Dec 02, 2014 3:11 pm
by kumarrana
I am not sure I understood what you suggesting. Can you explain little more. I am not expert on PHP. I was hoping something keep imagecreatetruecolor inside the loop. I am attaching my intended result.

Re: Create polygon and loop

Posted: Tue Dec 02, 2014 3:49 pm
by requinix
If you want three separate images then you can't do this with just the one PHP script. You'll need an HTML page (or whatever you already have) to reference the three individually, then you can set up one script (or three) for the images.

But if you want one image with three shapes in it then it's like I said.
kumarrana wrote:I was hoping something keep imagecreatetruecolor inside the loop.
Assuming it's the "one image" choice, doing that doesn't make sense. imagecreatetruecolor() creates an image of whatever dimensions. You only do that once. What you repeat is the part that draws the polygon and writes the text.

Re: Create polygon and loop

Posted: Tue Dec 02, 2014 4:13 pm
by kumarrana
Thanks! It makes sense