Don Alexander Eckford's Medallion Reward System
Your Medallion:
Code for Goal Achievement FOLLOWS
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$percentage = $_POST['percentage'];
// Determine the number of sides
if ($percentage < 10)
{
$sides = 3;
}
elseif ($percentage <= 20)
{
$sides = 3;
}
elseif ($percentage <= 30)
{
$sides = 4;
}
elseif ($percentage <= 40)
{
$sides = 4;
}
elseif ($percentage <= 50)
{
$sides = 5;
}
elseif ($percentage <= 60)
{
$sides = 6;
}
elseif ($percentage <= 70)
{
$sides = 7;
}
elseif ($percentage <= 80)
{
$sides = 8;
}
elseif ($percentage <= 90)
{
$sides = 9;
}
else
{
$sides = 10;
}
// Redirect to the drawing script with the calculated sides
header("Location: draw_medallion.php?sides=$sides");
exit();
}
?>
SLAVE CODE for drawing medallion FOLLOWS
<?php
// Check if the number of sides is provided in the query string
if (!isset($_GET['sides']))
{
die("Error: No sides parameter provided."); // Stop execution if 'sides' is not set
}
// Get the number of sides from the query string
$sides = (int)$_GET['sides'];
// Define the image dimensions
$width = 200; // Image width
$height = 200; // Image height
// Create a blank image with the specified width and height
$image = imagecreate($width, $height);
// Allocate colors
$background = imagecolorallocate($image, 238, 238, 238); // Light gray background
$color = imagecolorallocate($image, 0, 0, 255); // Blue color for the polygon
// Initialize an empty array to store polygon points
$points = [];
$radius = 70; // Radius of the polygon
$centerX = $width / 2; // Center X-coordinate of the polygon
$centerY = $height / 2; // Center Y-coordinate of the polygon
// Calculate the coordinates for each vertex of the polygon
for ($i = 0; $i < $sides; $i++)
{
$angle = 2 * pi() * $i / $sides; // Calculate the angle for this vertex
$x = $centerX + $radius * cos($angle); // X-coordinate
$y = $centerY + $radius * sin($angle); // Y-coordinate
$points[] = $x; // Add X-coordinate to the points array
$points[] = $y; // Add Y-coordinate to the points array
}
// Draw the outline of the polygon
imagepolygon($image, $points, $sides, $color);
// Draw a filled polygon with the same points
imagefilledpolygon($image, $points, $sides, $color);
// Optional: Create a red frame around the image
// Define the coordinates for the frame
$frame = [
0, 0, // Top-left corner
$width - 1, 0, // Top-right corner
$width - 1, $height - 1, // Bottom-right corner
0, $height - 1, // Bottom-left corner
0, 0 // Close the frame back at the top-left corner
];
// Set the color for the frame (red)
$frameColor = imagecolorallocate($image, 255, 0, 0);
// Draw the frame
imagepolygon($image, $frame, count($frame) / 2, $frameColor);
// Directory to save the image
$imageDir = "generated_images/";
if (!is_dir($imageDir))
{
mkdir($imageDir, 0777, true); // Create directory if it doesn't exist
}
// Unique filename for the image
$imageFile = $imageDir . "medallion_" . $sides . "_" . time() . ".png";
// Save the image as a file
imagepng($image, $imageFile);
// Free memory
imagedestroy($image);
// Redirect back to the main page with the image file path
header("Location: index_medallion.php?image=" . urlencode($imageFile));
exit();
?>
// Output the image as a PNG
//header("Content-Type: image/png");
//imagepng($image);
// Free memory used by the image
//imagedestroy($image);
//?>