90 lines
3.5 KiB
PHP
90 lines
3.5 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
require_once __DIR__ . '/../db/config.php';
|
|
require_once __DIR__ . '/../ai/LocalAIApi.php';
|
|
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (!$data) {
|
|
echo json_encode(['success' => false, 'error' => 'Invalid request data']);
|
|
exit;
|
|
}
|
|
|
|
$action = $data['action'] ?? 'magic';
|
|
$original_prompt = $data['original_prompt'] ?? '';
|
|
$edit_prompt = $data['edit_prompt'] ?? '';
|
|
$image_url = $data['image_url'] ?? '';
|
|
|
|
if (empty($original_prompt) && empty($edit_prompt)) {
|
|
echo json_encode(['success' => false, 'error' => 'Prompt is required']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$new_prompt = '';
|
|
|
|
// Step 1: Use AI to merge prompts or handle actions
|
|
if ($action === 'magic') {
|
|
$ai_payload = [
|
|
'input' => [
|
|
['role' => 'system', 'content' => 'You are an AI Image Prompt Engineer. Your task is to combine an original image description with a new edit instruction into a single, highly detailed, and cohesive new image prompt. Maintain the core subject and style of the original but incorporate the changes naturally. Output ONLY the new prompt, no explanation.'],
|
|
['role' => 'user', 'content' => "Original description: {$original_prompt}\nEdit instruction: {$edit_prompt}\nNew Prompt:"]
|
|
]
|
|
];
|
|
|
|
$ai_response = LocalAIApi::createResponse($ai_payload);
|
|
if (!empty($ai_response['success'])) {
|
|
$new_prompt = LocalAIApi::extractText($ai_response);
|
|
} else {
|
|
// Fallback: simple concatenation
|
|
$new_prompt = $original_prompt . ", " . $edit_prompt;
|
|
}
|
|
} else if ($action === 'remove_bg') {
|
|
$new_prompt = "{$original_prompt}, isolated on a pure white background, studio lighting, professional product photography";
|
|
} else if ($action === 'upscale') {
|
|
$new_prompt = "{$original_prompt}, extremely detailed, 8k resolution, masterpiece, sharp focus, hyperrealistic";
|
|
}
|
|
|
|
if (empty($new_prompt)) {
|
|
$new_prompt = $original_prompt . " " . $edit_prompt;
|
|
}
|
|
|
|
// Step 2: Generate new image using Pollinations (Flux)
|
|
$seed = rand(1000, 99999);
|
|
$encoded_prompt = urlencode($new_prompt);
|
|
$api_url = "https://image.pollinations.ai/prompt/{$encoded_prompt}?width=1024&height=1024&seed={$seed}&model=flux&nologo=true";
|
|
|
|
$filename = 'assets/images/pexels/gen_edit_' . md5($new_prompt . $seed) . '.jpg';
|
|
$target = __DIR__ . '/../' . $filename;
|
|
|
|
// Download the image
|
|
$ch = curl_init($api_url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
|
|
$image_data = curl_exec($ch);
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
if ($http_code === 200 && $image_data) {
|
|
if (!is_dir(__DIR__ . '/../assets/images/pexels/')) {
|
|
mkdir(__DIR__ . '/../assets/images/pexels/', 0775, true);
|
|
}
|
|
file_put_contents($target, $image_data);
|
|
|
|
// Save to history
|
|
$stmt = db()->prepare("INSERT INTO media_history (type, prompt, result_url) VALUES (?, ?, ?)");
|
|
$stmt->execute(['photo', "[Edit] " . $new_prompt, $filename]);
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'url' => $filename,
|
|
'new_prompt' => $new_prompt
|
|
]);
|
|
} else {
|
|
echo json_encode(['success' => false, 'error' => 'Failed to generate image from AI: HTTP ' . $http_code]);
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
|
|
} |