<?php
session_start();

// Buat angka acak 4 digit
$captcha_code = strval(rand(1000, 9999));
$_SESSION['captcha_code'] = $captcha_code;

// Ukuran gambar
$width = 155;
$height = 70;
$image = imagecreatetruecolor($width, $height);

// Warna background (hitam)
$background_color = imagecolorallocate($image, 0, 0, 0);
imagefilledrectangle($image, 0, 0, $width, $height, $background_color);

// Warna teks (emas / kuning cerah)
$text_color = imagecolorallocate($image, 255, 215, 0);

// Path font TTF (pastikan file ini ada!)
$font_path = __DIR__ . '/../css/Teko-SemiBold.ttf'; // sesuaikan dengan posisi file font kamu

// Ukuran font
$font_size = 50;

// Hitung posisi teks biar rata tengah
$bbox = imagettfbbox($font_size, 0, $font_path, $captcha_code);
$text_width = $bbox[2] - $bbox[0];
$text_height = $bbox[1] - $bbox[7];
$x = ($width - $text_width) / 2;
$y = ($height + $text_height) / 2;

// Tambahkan teks captcha ke gambar
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font_path, $captcha_code);

// (Opsional) tambahkan garis acak untuk efek noise ringan
// for ($i = 0; $i < 3; $i++) {
//     $noise_color = imagecolorallocate($image, rand(100, 200), rand(100, 200), 0);
//     imageline($image, 0, rand(0, $height), $width, rand(0, $height), $noise_color);
// }

// Output gambar ke browser
header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
?>
