<?php
// Set PNG header
header("Content-type: image/png");

// Configuration
define('IMAGE_WIDTH_BASE', 500);
define('IMAGE_HEIGHT', 440);
define('LEFT_MARGIN', 100);
define('RIGHT_MARGIN', 100);
define('FONT_PATH', '../fonts/nimbus_roman.pfb'); // Ensure this font exists!

// Input sanitization
$rows     = max(1, intval($_REQUEST['rows'] ?? 1));
$maxComm  = floatval($_REQUEST['maxComm'] ?? 20);
$type     = htmlspecialchars($_REQUEST['type'] ?? 'Clicks');

$clicks   = explode(",", $_REQUEST['click'] ?? '');
$names    = explode(",", $_REQUEST['name'] ?? '');
$comm     = explode(",", $_REQUEST['comm'] ?? '');

$totalValue = ($maxComm > 0) ? $maxComm : 20;
$leftValue  = $totalValue / 2;

$width  = IMAGE_WIDTH_BASE + $totalValue;
$height = IMAGE_HEIGHT;

$start = $leftValue + LEFT_MARGIN;
$end   = 400 + $leftValue + LEFT_MARGIN;
$top_start = 10;
$top_end   = 410;
$row_height = ($top_end - $top_start) / max(1, $rows);

// Create image
$im = imagecreatetruecolor($width, $height);
imageantialias($im, true);

// Colors
$black = imagecolorallocate($im, 0, 0, 0);
$gray  = imagecolorallocate($im, 192, 192, 192);
$white = imagecolorallocate($im, 255, 255, 255);
$aqua  = imagecolorallocate($im, 239, 239, 255);
$blue  = imagecolorallocate($im, 0, 0, 255);

// Fill background
imagefilltoborder($im, 0, 0, $blue, $aqua);

// Draw border
imageline($im, $start, $top_start, $start, $top_end, $gray);
imageline($im, $start, $top_end, $end, $top_end, $gray);
imageline($im, $start, $top_start, $end, $top_start, $gray);
imageline($im, $end, $top_start, $end, $top_end, $gray);

// Vertical grid
for ($i = $start + 40; $i < $end; $i += 40) {
    imageline($im, $i, $top_start, $i, $top_end, $gray);
}

// Horizontal grid
for ($i = $top_start + $row_height; $i < $top_end; $i += $row_height) {
    imageline($im, $start, $i, $end, $i, $gray);
}

// Check if font file exists
$useFont = is_readable(FONT_PATH);

// Draw bubbles
$center_y = $row_height / 2 + 10;
$count = min(count($clicks), count($names), count($comm));

for ($x = 0; $x < $count; $x++) {
    $clickVal = floatval($clicks[$x]);
    $commVal  = floatval($comm[$x]);
    $name     = htmlspecialchars($names[$x]);

    $diameter = ($row_height * $commVal) / 100;
    if ($diameter < 1) $diameter = 1; // avoid invisible bubble

    $center_x = (($width - RIGHT_MARGIN) * $clickVal / 100) - ($diameter / 2) + LEFT_MARGIN;
    imageellipse($im, $center_x, $center_y, $diameter, $diameter, $black);

    if ($useFont) {
        imagettftext($im, 10, 0, 5, $center_y + 4, $black, FONT_PATH, $name);
    } else {
        imagestring($im, 2, 5, $center_y - 6, $name, $black);
    }

    $center_y += $row_height;
}

// Axis labels
if ($useFont) {
    imagettftext($im, 10, 0, $start, $height - 10, $black, FONT_PATH, $type);
    imagettftext($im, 10, 0, 10, 15, $black, FONT_PATH, "Affiliates");
} else {
    imagestring($im, 2, $start, $height - 20, $type, $black);
    imagestring($im, 2, 10, 5, "Affiliates", $black);
}

// Output image
imagepng($im);
imagedestroy($im);
