...

View Full Version : circle in php



gemguy
11-05-2007, 02:23 PM
Hi,

I need to create a circle in php with specified width and height. Can it be done using the createimage for gif and png formats.

Also i like to have some ideas on venn diagrams using php.

mlse
11-05-2007, 03:00 PM
Hi there, the following code should give you the circle that you require. I've not tested the code so not sure if it works! I just bashed it out quickly. Check out the image<whatever> functions at www.php.net to understand what they do.



<?php
$diameter = $_REQUEST["diameter"];

if ($im = imagecreate($diameter, $diameter))
{
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);

imagefill($im, 0, 0, $white);

imagearc($im, $diameter/2, $diameter/2, $diameter, $diameter, 0, 360, $black);

header("content-type:image/png"); //This tells the web browser to expect a PNG image.
imagepng($im);

imagecolordeallocate($im, $white);
imagecolordeallocate($im, $black);
imagedestroy($im);
}
else echo "whoops!";
?>


if you put that code as it is into a file called "circle.php" on your webserver then you can generate cirles of whatever diameter you want by adjusting the diameter get var.

For example: http:// ... /circle.php?diameter=101 will give you a 101x101 pixel circle (don't use an even number for a diameter or else you'll loose the rightmost and bottom-most pixels).

Note that I've only coded for the single argument "diameter" rather than for separate width and height because a circle with width not equal to height is not a circle ... ;)

I hope that helps and I hope the code works! (as I said, I;ve not tested the code, so there may be the odd bug in it!). If it works for you, you can easily modify that code to make venn diagrams (just move the locus of each circle that you draw).

Note that you need the GD2 library included in your PHP build for many of the image functions to work.

Regards,

Mike.

mlseim
11-05-2007, 03:02 PM
A lot of information on GD graphs and charts:
http://www.google.com/search?hl=en&safe=off&q=php+gd+graphs&btnG=Search

Not so much about Venn diagrams, but perhaps someone has an example?

gemguy
11-06-2007, 05:39 AM
Hi mlse,mlseim,

Thanks for your answers. Im just going through it and let me get back with the best code out of it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum