38771-vm/api/process.php
2026-02-25 23:02:46 +00:00

74 lines
2.8 KiB
PHP

<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../db/config.php';
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
echo json_encode(['success' => false, 'error' => 'Invalid method']);
exit;
}
if (!isset($_FILES['photo']) || $_FILES['photo']['error'] !== UPLOAD_ERR_OK) {
echo json_encode(['success' => false, 'error' => 'Upload failed']);
exit;
}
$uploadDir = __DIR__ . '/../uploads/originals/';
$processedDir = __DIR__ . '/../uploads/processed/';
$fileName = time() . '_' . basename($_FILES['photo']['name']);
$targetFile = $uploadDir . $fileName;
if (move_uploaded_file($_FILES['photo']['tmp_name'], $targetFile)) {
// In a real app, we would call an AI API here to remove background and swap.
// For this MVP, we will simulate it by creating a "Luxury Edition"
// which is the original image with a subtle luxury filter/overlay.
$processedFileName = 'luxury_' . $fileName;
$processedPath = $processedDir . $processedFileName;
// Simulate processing: just copy for now
copy($targetFile, $processedPath);
// To make it look "luxury", we could use GD library to add a filter if available
if (extension_loaded('gd')) {
$info = getimagesize($targetFile);
$img = null;
if ($info['mime'] == 'image/jpeg') $img = imagecreatefromjpeg($targetFile);
elseif ($info['mime'] == 'image/png') $img = imagecreatefrompng($targetFile);
elseif ($info['mime'] == 'image/webp') $img = imagecreatefromwebp($targetFile);
if ($img) {
// Apply a slight "luxury" warm/dark filter
imagefilter($img, IMG_FILTER_CONTRAST, -5);
imagefilter($img, IMG_FILTER_BRIGHTNESS, -10);
imagefilter($img, IMG_FILTER_COLORIZE, 20, 10, -10, 20); // Warm gold tint
if ($info['mime'] == 'image/jpeg') imagejpeg($img, $processedPath, 90);
elseif ($info['mime'] == 'image/png') imagepng($img, $processedPath);
elseif ($info['mime'] == 'image/webp') imagewebp($img, $processedPath);
imagedestroy($img);
}
}
// Persist to DB
try {
$db = db();
$stmt = $db->prepare("INSERT INTO transformations (original_path, processed_path, style_id, status) VALUES (?, ?, ?, 'completed')");
$stmt->execute([
'uploads/originals/' . $fileName,
'uploads/processed/' . $processedFileName,
$_POST['style'] ?? 'default'
]);
} catch (Exception $e) {
// Silently continue if DB fails, but log it
error_log($e->getMessage());
}
echo json_encode([
'success' => true,
'original_url' => 'uploads/originals/' . $fileName,
'processed_url' => 'uploads/processed/' . $processedFileName
]);
} else {
echo json_encode(['success' => false, 'error' => 'Could not save file']);
}