false, 'error' => 'Invalid request method']); exit; } $type = $_POST['type'] ?? 'photo'; $prompt = $_POST['prompt'] ?? ''; $style = $_POST['style'] ?? ''; if (empty($prompt)) { echo json_encode(['success' => false, 'error' => 'Запрос не может быть пустым']); exit; } $width = 1024; $height = 1024; $enhanced_prompt = $prompt; if (!empty($style)) { $style_map = [ 'anime' => 'in anime style, high quality, vibrant colors', 'realism' => 'photorealistic, highly detailed, 8k, masterpiece', 'cyberpunk' => 'cyberpunk style, neon lights, futuristic, highly detailed', '3d-render' => '3d render, soft aesthetic, cinematic lighting, high quality', 'minimalism' => 'minimalist style, clean lines, simple, elegant', 'cinematic' => 'cinematic shot, dramatic lighting, movie still' ]; if (isset($style_map[$style])) { $enhanced_prompt .= ", " . $style_map[$style]; } } try { $result_url = ''; $is_ai = false; $provider_name = ''; $message = ''; if ($type === 'photo') { $is_ai = true; $provider_name = 'Pollinations Flux'; $seed = rand(1000, 99999); $encoded_prompt = urlencode($enhanced_prompt); $api_url = "https://image.pollinations.ai/prompt/{$encoded_prompt}?width={$width}&height={$height}&seed={$seed}&model=flux&nologo=true"; $filename = 'assets/images/pexels/gen_' . md5($enhanced_prompt . $seed) . '.jpg'; $target = __DIR__ . '/../' . $filename; $ch = curl_init($api_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_TIMEOUT, 30); $image_data = curl_exec($ch); $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($http_code === 200 && strpos($content_type, 'image') !== false && $image_data) { file_put_contents($target, $image_data); $result_url = $filename; } else { $result_url = $api_url; } } else { // Video always from Pexels $is_ai = false; $provider_name = 'Pexels Stock'; $url = 'https://api.pexels.com/videos/search?query=' . urlencode($prompt) . '&per_page=1&page=1'; $data = pexels_get($url); if ($data && !empty($data['videos'])) { foreach ($data['videos'][0]['video_files'] as $file) { if ($file['quality'] === 'hd' || $file['quality'] === 'sd') { $result_url = $file['link']; break; } } } if (empty($result_url)) { $result_url = 'https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.mp4'; $message = 'Видео не найдено по вашему запросу. Показываем пример.'; } } $history_prompt = $prompt . ($style ? " ($style)" : ""); $stmt = db()->prepare("INSERT INTO media_history (type, prompt, result_url) VALUES (?, ?, ?)"); $stmt->execute([$type, $history_prompt, $result_url]); echo json_encode([ 'success' => true, 'type' => $type, 'url' => $result_url, 'prompt' => $history_prompt, 'is_ai' => $is_ai, 'provider' => $provider_name, 'message' => $message ]); } catch (Exception $e) { echo json_encode(['success' => false, 'error' => $e->getMessage()]); }