<?php
// Ukuran gambar
$width = 800;
$height = 600;

// Buat gambar baru
$image = imagecreatetruecolor($width, $height);

// Warna
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
$lightgrey = imagecolorallocate($image, 230, 230, 230);
$lightblue = imagecolorallocate($image, 173, 216, 230);
$darkblue = imagecolorallocate($image, 0, 0, 139);
$green = imagecolorallocate($image, 0, 128, 0);
$red = imagecolorallocate($image, 255, 0, 0);

// Isi latar belakang dengan putih
imagefill($image, 0, 0, $white);

// Definisikan posisi kotak
$positions = [
    "Start" => ["x" => 400, "y" => 50],
    "R&D" => ["x" => 400, "y" => 150],
    "Product Design" => ["x" => 400, "y" => 250],
    "Procurement" => ["x" => 400, "y" => 350],
    "Production" => ["x" => 400, "y" => 450],
    "Quality Assurance" => ["x" => 400, "y" => 550],
    "Marketing & Sales" => ["x" => 400, "y" => 650],
    "Customer Service" => ["x" => 400, "y" => 750],
    "End" => ["x" => 400, "y" => 850]
];

// Gambar kotak dan tulisan
foreach ($positions as $key => $pos) {
    if ($key == "Start" || $key == "End") {
        // Gambar oval untuk Start dan End
        imagefilledellipse($image, $pos["x"], $pos["y"], 100, 50, $lightgrey);
        imageellipse($image, $pos["x"], $pos["y"], 100, 50, $black);
        // Tulisan di tengah oval
        imagestring($image, 5, $pos["x"] - 40, $pos["y"] - 10, $key, $black);
    } else {
        // Gambar kotak untuk proses lainnya
        imagerectangle($image, $pos["x"] - 50, $pos["y"] - 25, $pos["x"] + 50, $pos["y"] + 25, $black);
        imagefilledrectangle($image, $pos["x"] - 50, $pos["y"] - 25, $pos["x"] + 50, $pos["y"] + 25, $lightblue);
        // Tulisan di tengah kotak
        imagestring($image, 5, $pos["x"] - 40, $pos["y"] - 10, $key, $black);
    }
}

// Gambar panah antara kotak
$arrow_size = 10;
foreach ($positions as $i => $key) {
    if ($i < count($positions) - 1) {
        $start_pos = $positions[$key];
        $end_key = array_keys($positions)[$i + 1];
        $end_pos = $positions[$end_key];
        
        // Gambar garis
        imageline($image, $start_pos["x"], $start_pos["y"] + 25, $end_pos["x"], $end_pos["y"] - 25, $black);
        
        // Gambar panah
        $angle = atan2($end_pos["y"] - $start_pos["y"], $end_pos["x"] - $start_pos["x"]);
        $x1 = $end_pos["x"] - $arrow_size * cos($angle - pi() / 6);
        $y1 = $end_pos["y"] - $arrow_size * sin($angle - pi() / 6);
        $x2 = $end_pos["x"] - $arrow_size * cos($angle + pi() / 6);
        $y2 = $end_pos["y"] - $arrow_size * sin($angle + pi() / 6);
        imageline($image, $end_pos["x"], $end_pos["y"], $x1, $y1, $black);
        imageline($image, $end_pos["x"], $end_pos["y"], $x2, $y2, $black);
    }
}

// Output gambar
header("Content-Type: image/png");
imagepng($image);

// Hapus gambar dari memori
imagedestroy($image);
?>
