'POST required']); exit; } header('Content-Type: application/json'); $file = $_FILES['image'] ?? null; if (!$file || $file['error'] !== UPLOAD_ERR_OK) { echo json_encode(['error' => 'Upload failed — error code ' . ($file['error'] ?? 'none')]); exit; } $allowed_types = ['image/jpeg', 'image/png', 'image/gif', 'image/webp']; $mime = mime_content_type($file['tmp_name']); if (!in_array($mime, $allowed_types, true)) { echo json_encode(['error' => 'Only JPEG, PNG, GIF, and WebP images are allowed']); exit; } if ($file['size'] > 10 * 1024 * 1024) { echo json_encode(['error' => 'Image must be under 10 MB']); exit; } $ext = match ($mime) { 'image/jpeg' => 'jpg', 'image/png' => 'png', 'image/gif' => 'gif', 'image/webp' => 'webp', default => 'jpg', }; $filename = time() . '_' . bin2hex(random_bytes(6)) . '.' . $ext; $dest = UPLOAD_DIR . '/' . $filename; if (!move_uploaded_file($file['tmp_name'], $dest)) { echo json_encode(['error' => 'Failed to save image']); exit; } echo json_encode([ 'ok' => true, 'path' => $filename, 'url' => '/api/social_upload?action=serve&path=' . rawurlencode($filename), ]);