<?php
// Validate and sanitize inputs
$height = isset($_GET['height']) ? intval($_GET['height']) : 300;
$alt    = isset($_GET['alt']) ? intval($_GET['alt']) : 0;

$day1   = isset($_REQUEST['day1']) ? floatval($_REQUEST['day1']) : 30;
$day15  = isset($_REQUEST['day15']) ? floatval($_REQUEST['day15']) : 60;
$month1 = isset($_REQUEST['month1']) ? floatval($_REQUEST['month1']) : 90;
$month2 = isset($_REQUEST['month2']) ? floatval($_REQUEST['month2']) : 120;
$total  = isset($_REQUEST['total']) ? floatval($_REQUEST['total']) : 100;

// Ensure height is within a safe range
$height = max(100, min($height, 1000)); // Clamp between 100 and 1000
$center = intval($height / 2);
$radius = $center - 1;

// Create image
$image = imagecreate($height, $height);
imagealphablending($image, true);
imageantialias($image, true);

// Background color
$background = ($alt == 0)
    ? imagecolorallocate($image, 255, 255, 255)
    : imagecolorallocate($image, 247, 247, 222);

// Segment colors
$color_day1   = imagecolorallocate($image, 0xCC, 0x00, 0x33);
$color_day15  = imagecolorallocate($image, 0xFF, 0xCC, 0x00);
$color_month1 = imagecolorallocate($image, 0x33, 0x00, 0xFF);
$color_month2 = imagecolorallocate($image, 0x00, 0x99, 0x00);
$color_after  = imagecolorallocate($image, 0x00, 0xCC, 0xFF);

// Shade colors for 3D effect
$shade_day1   = imagecolorallocate($image, 0x99, 0x00, 0x00);
$shade_day15  = imagecolorallocate($image, 0xFF, 0xAA, 0x00);
$shade_month1 = imagecolorallocate($image, 0x33, 0x00, 0xCC);
$shade_month2 = imagecolorallocate($image, 0x33, 0x66, 0x33);
$shade_after  = imagecolorallocate($image, 0x00, 0x99, 0xFF);

// If total is zero, use grayed-out colors for 'after'
if ($total == 0) {
    $color_after = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);
    $shade_after = imagecolorallocate($image, 0x90, 0x90, 0x90);
}

// Convert percentage values to angles (0–360)
$angle_day1   = intval(($day1 / 100) * 360);
$angle_day15  = intval(($day15 / 100) * 360);
$angle_month1 = intval(($month1 / 100) * 360);
$angle_month2 = intval(($month2 / 100) * 360);

// Draw 3D pie with shades
$newcenter = $center - 15;
$radius_new = $radius - 25;

for ($i = $center; $i > $newcenter; $i--) {
    imagefilledarc($image, $center, $i, $radius, $radius_new, 0, $angle_day1, $shade_day1, IMG_ARC_PIE);
    imagefilledarc($image, $center, $i, $radius, $radius_new, $angle_day1, $angle_day15, $shade_day15, IMG_ARC_PIE);
    imagefilledarc($image, $center, $i, $radius, $radius_new, $angle_day15, $angle_month1, $shade_month1, IMG_ARC_PIE);
    imagefilledarc($image, $center, $i, $radius, $radius_new, $angle_month1, $angle_month2, $shade_month2, IMG_ARC_PIE);
    imagefilledarc($image, $center, $i, $radius, $radius_new, $angle_month2, 360, $shade_after, IMG_ARC_PIE);
}

// Top slice
imagefilledarc($image, $center, $newcenter, $radius, $radius_new, 0, $angle_day1, $color_day1, IMG_ARC_PIE);
imagefilledarc($image, $center, $newcenter, $radius, $radius_new, $angle_day1, $angle_day15, $color_day15, IMG_ARC_PIE);
imagefilledarc($image, $center, $newcenter, $radius, $radius_new, $angle_day15, $angle_month1, $color_month1, IMG_ARC_PIE);
imagefilledarc($image, $center, $newcenter, $radius, $radius_new, $angle_month1, $angle_month2, $color_month2, IMG_ARC_PIE);
imagefilledarc($image, $center, $newcenter, $radius, $radius_new, $angle_month2, 360, $color_after, IMG_ARC_PIE);

// Output image
header("Content-Type: image/png");
imagepng($image);
imagedestroy($image);
?>
