feat(galerias): upload several images at once, clear errors and JPEG XL placeholders

This commit is contained in:
andre committed 2026-10-26 13:48:02 +00:00
1 parent 6abc486630
commit 6ac5213624
3 files changed
+55 -17

No files matched your search

+16 -5
View File
@@ -50,19 +50,30 @@ function db_gallery_images(PDO $pdo, int $gallery_id): array {
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
function db_gallery_image_upload(PDO $pdo, int $gallery_id, array $file): ?string {
if ($file['error'] !== UPLOAD_ERR_OK) return null;
// Limite por ficheiro: o menor entre 16 MB e o upload_max_filesize do PHP.
function gallery_upload_max_bytes(): int {
$ini = trim((string) ini_get('upload_max_filesize'));
$n = (int) $ini;
$n *= match (strtolower(substr($ini, -1))) { 'g' => 1 << 30, 'm' => 1 << 20, 'k' => 1 << 10, default => 1 };
return $n > 0 ? min($n, 16 * 1024 * 1024) : 16 * 1024 * 1024;
}
if (($file['size'] ?? 0) > 16 * 1024 * 1024) return null;
// $error recebe o motivo (para a mensagem ao utilizador) quando devolve null.
function db_gallery_image_upload(PDO $pdo, int $gallery_id, array $file, ?string &$error = null): ?string {
$max_mb = round(gallery_upload_max_bytes() / 1048576);
if ($file['error'] === UPLOAD_ERR_INI_SIZE || $file['error'] === UPLOAD_ERR_FORM_SIZE) { $error = "maior que {$max_mb} MB"; return null; }
if ($file['error'] !== UPLOAD_ERR_OK) { $error = 'o envio falhou'; return null; }
if (($file['size'] ?? 0) > gallery_upload_max_bytes()) { $error = "maior que {$max_mb} MB"; return null; }
// Tipo decidido pelo conteúdo, não pelo nome (config/functions_images.php).
$ext = upload_image_ext($file, ['jpg', 'jpeg', 'png', 'gif', 'webp', 'jxl']);
if ($ext === null) return null;
if ($ext === null) { $error = 'formato não suportado'; return null; }
$upload_dir = __DIR__ . '/../uploads/gallery/' . $gallery_id . '/';
if (!is_dir($upload_dir)) @mkdir($upload_dir, 0775, true);
$filename = bin2hex(random_bytes(16)) . '.' . $ext;
if (!move_uploaded_file($file['tmp_name'], $upload_dir . $filename)) return null;
if (!move_uploaded_file($file['tmp_name'], $upload_dir . $filename)) { $error = 'não foi possível gravar'; return null; }
img_optimize($upload_dir . $filename, IMG_MAX_GALLERY);
$stmt = $pdo->prepare("INSERT INTO gallery (i_file_name, i_file_type, i_project_id) VALUES (?, ?, ?)");