AI Image Generator
Select One
Select One
Abstract
Cartoon
Cubism
Cyberpunk
Expressionism
Fantasy
Futuristic
Gothic
Illustration
Impressionism
Magical Realism
Minimalist
Modernism
Photo-realistic
Pointillism
Pop Art
Realism
Retro
Sci-Fi
Steampunk
Surreal
Surrealism
Vintage
document.addEventListener(‘DOMContentLoaded’, function() {
const generateImageBtn = document.getElementById(‘generateImageBtn’);
const descriptionTextarea = document.getElementById(‘description’);
const styleSelect = document.getElementById(‘image-style’); // Added style select
const generatedImageContainer = document.getElementById(‘generatedImageContainer’);
async function generateImage() {
const description = descriptionTextarea.value.trim();
const style = styleSelect.value; // Get the selected style
let prompt = description;
if (!description) {
alert(‘Please enter a description.’);
return;
}
if (style) {
prompt += `, Style: ${style}`; // Append style to prompt if selected
}
generatedImageContainer.innerHTML = ‘Loading image…
‘; try { const response = await fetch(‘https://api.openai.com/v1/images/generations’, { method: ‘POST’, headers: { ‘Content-Type’: ‘application/json’, ‘Authorization’: ‘Bearer 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’ }, body: JSON.stringify({ prompt: prompt, model: ‘dall-e-3’, n: 1, quality: ‘hd’, response_format: ‘url’ }) }); if (!response.ok) { throw new Error(‘Error generating image’); } const data = await response.json(); const imageUrl = data.data[0].url; generatedImageContainer.innerHTML = ‘‘; const regenerateBtn = document.createElement(‘button’); regenerateBtn.textContent = ‘Regenerate’; regenerateBtn.onclick = generateImage; generatedImageContainer.appendChild(regenerateBtn); const downloadLink = document.createElement(‘a’); downloadLink.href = imageUrl; downloadLink.download = ‘generated-image.png’; downloadLink.textContent = ‘Download’; generatedImageContainer.appendChild(downloadLink); } catch (error) { console.error(‘Error generating image:’, error); alert(‘Error generating image. Please try again later.’); } } generateImageBtn.addEventListener(‘click’, generateImage); });