trf_certest/public/userarea/remove_background.php
2025-08-29 16:30:29 +02:00

56 lines
1.7 KiB
PHP

<?php
header('Content-Type: application/json');
$photo_path = $_POST['photo_path'] ?? '';
$iddatadb = $_POST['iddatadb'] ?? '';
if (empty($photo_path) || empty($iddatadb)) {
echo json_encode(['success' => false, 'message' => 'Parametri mancanti']);
exit;
}
// Verifica che il file esista
$full_path = realpath($photo_path);
if (!$full_path || !file_exists($full_path)) {
echo json_encode(['success' => false, 'message' => 'File non trovato']);
exit;
}
// Configurazione Remove.bg API
$api_key = 'YOUR_API_KEY'; // Inserisci la tua chiave API di Remove.bg
$api_url = 'https://api.remove.bg/v1.0/removebg';
// Preparazione della richiesta
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
'image_file' => new CURLFile($full_path),
'size' => 'auto',
]);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'X-Api-Key: ' . $api_key
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Esecuzione della richiesta
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code !== 200) {
echo json_encode(['success' => false, 'message' => 'Errore API Remove.bg']);
exit;
}
// Salva l'immagine senza sfondo
$timestamp = date('YmdHis');
$new_filename = "no_bg_photo_{$iddatadb}_{$timestamp}.png";
$new_filepath = "uploads/{$new_filename}"; // Assumi una directory Uploads/
file_put_contents($new_filepath, $response);
// Aggiorna il database o il sistema di file con il nuovo percorso
// Qui potresti aggiungere logica per aggiornare il database con il nuovo percorso
echo json_encode(['success' => true, 'new_photo_path' => $new_filepath]);