<?php
// Returns a JSON list of images currently present in /img
header('Content-Type: application/json; charset=utf-8');

$imgDir = realpath(__DIR__ . '/../img');
if ($imgDir === false || !is_dir($imgDir)) {
  http_response_code(500);
  echo json_encode(['ok' => false, 'error' => 'img directory not found']);
  exit;
}

$allowedExt = ['jpg','jpeg','png','gif','webp'];
$images = [];

$files = scandir($imgDir);
if ($files === false) $files = [];

foreach ($files as $f) {
  if ($f === '.' || $f === '..') continue;
  $path = $imgDir . DIRECTORY_SEPARATOR . $f;
  if (!is_file($path)) continue;
  $ext = strtolower(pathinfo($f, PATHINFO_EXTENSION));
  if (!in_array($ext, $allowedExt, true)) continue;
  $images[] = $f;
}

// stable order
natcasesort($images);
$images = array_values($images);

echo json_encode(['ok' => true, 'images' => $images], JSON_UNESCAPED_UNICODE);


