document.addEventListener('DOMContentLoaded', function() { const generateBtn = document.querySelector('button'); const imagePlaceholder = document.querySelector('.bg-gray-200'); const textarea = document.querySelector('textarea'); const styleSelect = document.querySelector('select'); const imageDetails = document.getElementById('image-details'); const detailText = document.getElementById('detail-text'); // Example prompts for different styles const styleExamples = { portrait: "A professional headshot of a software developer with glasses, modern office background, soft lighting", landscape: "Sunset over tropical beach with palm trees, vibrant colors, 4K resolution", abstract: "Geometric shapes in neon colors floating in dark space, digital art style", product: "Minimalist product photo of a smartphone on marble surface, studio lighting", fantasy: "Ancient dragon perched on castle tower, moonlit night, highly detailed", realistic: "Close-up of a chef preparing sushi in a restaurant kitchen, ultra-realistic", anime: "Anime-style character with pink hair wearing school uniform, cherry blossoms background", watercolor: "Watercolor painting of Paris streets in spring, loose brush strokes" }; styleSelect.addEventListener('change', function() { textarea.placeholder = `Example: "${styleExamples[this.value]}"`; }); generateBtn.addEventListener('click', async function() { const description = textarea.value.trim(); if (!description) { alert("Please enter an image description"); return; } // Show loading state imagePlaceholder.innerHTML = '
'; feather.replace(); try { // First translate non-English text to English for better results let processedDescription = description; // Detect if text is non-English (simple check for non-ASCII characters) if (/[^\x00-\x7F]/.test(description)) { const translationResponse = await fetch(`https://api.mymemory.translated.net/get?q=${encodeURIComponent(description)}&langpair=auto|en`); const translationData = await translationResponse.json(); if (translationData.responseData && translationData.responseData.translatedText) { processedDescription = translationData.responseData.translatedText; // Show translation feedback imagePlaceholder.innerHTML = `

Original: ${description}

Translated: ${processedDescription}

`; feather.replace(); } } // Generate random seed based on processed description for consistency const seed = hashCode(processedDescription); const style = styleSelect.value; const category = getCategoryForStyle(style); // Enhance prompt with style keywords const enhancedPrompt = `${processedDescription}, ${style} style, high quality, detailed`; const generatedImage = `http://static.photos/${category}/1024x576/${seed}`; imagePlaceholder.innerHTML = ` Generated image `; // Store image data for download imagePlaceholder.dataset.imageUrl = generatedImage; imagePlaceholder.dataset.description = enhancedPrompt; // Add download button const downloadBtn = document.createElement('button'); downloadBtn.className = 'mt-4 bg-green-600 hover:bg-green-700 text-white font-bold py-2 px-4 rounded-lg transition duration-200'; downloadBtn.textContent = 'Download Image'; downloadBtn.onclick = downloadImage; imagePlaceholder.parentNode.insertBefore(downloadBtn, imagePlaceholder.nextSibling); } catch (error) { console.error("Error generating image:", error); imagePlaceholder.innerHTML = '

Error generating image

'; feather.replace(); } }); // Helper function to generate hash code function hashCode(str) { let hash = 0; for (let i = 0; i < str.length; i++) { hash = str.charCodeAt(i) + ((hash << 5) - hash); } return Math.abs(hash % 1000); } // Map styles to photo categories function getCategoryForStyle(style) { const styleMap = { portrait: 'people', landscape: 'nature', abstract: 'abstract', product: 'workspace', fantasy: 'vintage', realistic: 'office', anime: 'education', watercolor: 'textures' }; return styleMap[style] || 'abstract'; } }); // Global functions for image hover details function showDetails(description, style) { const details = document.getElementById('image-details'); const text = document.getElementById('detail-text'); text.textContent = `${style} style: ${description.substring(0, 100)}${description.length > 100 ? '...' : ''}`; details.classList.remove('hidden'); } function hideDetails() { document.getElementById('image-details').classList.add('hidden'); } function downloadImage() { const placeholder = document.querySelector('.bg-gray-200'); const link = document.createElement('a'); link.href = placeholder.dataset.imageUrl; link.download = `ai-art-${Date.now()}.jpg`; document.body.appendChild(link); link.click(); document.body.removeChild(link); }