Skip to content

Instantly share code, notes, and snippets.

@captbaritone
Created November 17, 2012 03:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save captbaritone/4093015 to your computer and use it in GitHub Desktop.
Save captbaritone/4093015 to your computer and use it in GitHub Desktop.
Drawing mandalas with PHP
<?php
// Prints a mondala
$points = $_GET['points'];
$size = $_GET['size'];
// Test the size
if($size < 1 || $size > 1024){
echo "Please use a better size";
// Test the number of points
}elseif($points < 1 || $points > 100){
echo "Please use a more reasonable number or points";
}else{
// Set the radius to half of the size of the image, so it fills the image
$radius = $size/2;
// Find the number of degrees between points (in radians)
$degrees = deg2rad(360/$points);
// The offset used to push the whole drawing into the first quadrant (because our math would
// normally put it centered on 0,0
$offset = $size/2;
// Create the image
$im = imagecreatetruecolor($size, $size);
// Set the background color white
$background = imagecolorallocate($im, 255, 255, 255);
imagefill($im, 0, 0, $background);
// Set the color of the lines black
$lines = imagecolorallocate($im, 0, 0, 0);
// Until we have made our way around the circle
while($angle < deg2rad(360)){
// Increase the angle to point to the next point
$angle = $angle + $degrees;
// Find the new points
$x = $radius * cos($angle) + $offset;
$y = $radius * sin($angle) + $offset;
// Add those points to the array of points
$pointsArr[] = array($x, $y);
}
// For each point
while($from = array_pop($pointsArr)){
// For each point that has not yet been plotted
foreach($pointsArr as $to){
// Draw a line
imageline($im, $from[0], $from[1], $to[0], $to[1], $lines);
}
}
// Output the image
header ("Content-type: image/png");
imagepng($im);
// Clear the image form memory
imagedestroy($im);
}
?>
@realizare
Copy link

Fatal error: Uncaught Error: Call to undefined function imagecreatetruecolor() in D:\xampp\htdocs\MANDALA\mandala.php:21 Stack trace: #0 {main} thrown in D:\xampp\htdocs\MANDALA\mandala.php on line 21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment