$maxW ? $maxW : $w; $nh = (int)round($h * ($nw / $w)); $canvas = imagecreatetruecolor($nw, $nh); // latar putih untuk PNG transparan imagefill($canvas, 0, 0, imagecolorallocate($canvas, 255, 255, 255)); imagecopyresampled($canvas, $img, 0, 0, 0, 0, $nw, $nh, $w, $h); $ok = imagejpeg($canvas, $dst, $quality); imagedestroy($img); imagedestroy($canvas); return $ok; } switch ($method) { case 'GET': $wargaId = $_GET['warga_id'] ?? null; if (!$wargaId) sendError('warga_id wajib'); $stmt = $pdo->prepare("SELECT id, file_path FROM foto_rumah WHERE warga_id=? ORDER BY id"); $stmt->execute([$wargaId]); $out = []; while ($r = $stmt->fetch()) { $out[] = ['id' => (int)$r['id'], 'url' => app_url($r['file_path'])]; } sendSuccess($out, 'Foto rumah'); break; case 'POST': $wargaId = $_POST['warga_id'] ?? null; if (!$wargaId) sendError('warga_id wajib'); if (empty($_FILES['foto'])) sendError('Tidak ada file diunggah'); $existing = (int)$pdo->query("SELECT COUNT(*) FROM foto_rumah WHERE warga_id=" . (int)$wargaId)->fetchColumn(); // Normalisasi $_FILES['foto'] ke array $files = $_FILES['foto']; $names = is_array($files['name']) ? $files['name'] : [$files['name']]; $tmps = is_array($files['tmp_name']) ? $files['tmp_name'] : [$files['tmp_name']]; if (!is_dir(UPLOAD_DIR)) mkdir(UPLOAD_DIR, 0775, true); $saved = 0; foreach ($tmps as $i => $tmp) { if ($existing + $saved >= MAX_FOTO) break; if (!is_uploaded_file($tmp)) continue; $fname = 'w' . (int)$wargaId . '_' . uniqid() . '.jpg'; $dst = UPLOAD_DIR . $fname; if (compressImage($tmp, $dst)) { $rel = 'uploads/rumah/' . $fname; $pdo->prepare("INSERT INTO foto_rumah (warga_id, file_path) VALUES (?,?)") ->execute([$wargaId, $rel]); $saved++; } } if ($saved === 0) sendError('Gagal memproses foto (format harus JPG/PNG/WEBP)'); logActivity($pdo, 'create', 'foto_rumah', (int)$wargaId, "Upload {$saved} foto"); sendSuccess(['uploaded' => $saved], "{$saved} foto diunggah", 201); break; case 'DELETE': $id = $_GET['id'] ?? null; if (!$id) sendError('ID wajib'); $row = $pdo->prepare("SELECT file_path FROM foto_rumah WHERE id=?"); $row->execute([$id]); $fp = $row->fetchColumn(); if ($fp) { $abs = __DIR__ . '/../' . $fp; if (is_file($abs)) @unlink($abs); $pdo->prepare("DELETE FROM foto_rumah WHERE id=?")->execute([$id]); logActivity($pdo, 'delete', 'foto_rumah', (int)$id, 'Hapus foto'); } sendSuccess(null, 'Foto dihapus'); break; default: sendError('Method not allowed', 405); }