Skip to content

Instantly share code, notes, and snippets.

@souhailkaoussi-pro
Created May 24, 2025 12:08
Show Gist options
  • Save souhailkaoussi-pro/809bb20960df81ebb524163c1c8e92b3 to your computer and use it in GitHub Desktop.
Save souhailkaoussi-pro/809bb20960df81ebb524163c1c8e92b3 to your computer and use it in GitHub Desktop.
// app/api/generate-image/route.ts
import { NextResponse } from 'next/server';
import OpenAI from 'openai';
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
export async function POST(request: Request) {
try {
const { prompt, style } = await request.json();
// Create an enhanced prompt with style guidelines in JSON format
const enhancedPrompt = createEnhancedPrompt(prompt, style);
const response = await openai.images.generate({
model: 'dall-e-3',
prompt: enhancedPrompt,
n: 1,
size: '1024x1024',
});
const imageUrl = response.data?.[0]?.url;
return NextResponse.json({ imageUrl });
} catch (error) {
console.error('Error generating image:', error);
return NextResponse.json({ error: 'Failed to generate image' }, { status: 500 });
}
}
function createEnhancedPrompt(userPrompt: string, style: string = 'modern') {
// Style guide templates
const styleGuides = {
modern: {
visualStyle: "clean, minimalist, high contrast",
lighting: "soft, ambient, natural",
colorPalette: "muted, complementary colors",
composition: "rule of thirds, balanced",
mood: "calm, sophisticated"
},
vintage: {
visualStyle: "retro, nostalgic, textured",
lighting: "warm, golden hour, film-like",
colorPalette: "faded, sepia tones, desaturated",
composition: "centered, symmetrical",
mood: "nostalgic, dreamy"
},
futuristic: {
visualStyle: "high-tech, sleek, digital",
lighting: "neon, dramatic, high contrast",
colorPalette: "blues, purples, electric highlights",
composition: "dynamic, asymmetrical",
mood: "mysterious, technological"
},
fantasy: {
visualStyle: "magical, detailed, ethereal",
lighting: "dramatic, mystical, glowing",
colorPalette: "rich, vibrant, otherworldly",
composition: "epic, storytelling focus",
mood: "wonder, awe, magical"
}
};
// Select the appropriate style guide
const selectedStyle = styleGuides[style as keyof typeof styleGuides] || styleGuides.modern;
// Create the enhanced prompt with JSON style guide
return `Create an image based on this prompt: "${userPrompt}".
Apply these style guidelines:
{
"visualStyle": "${selectedStyle.visualStyle}",
"lighting": "${selectedStyle.lighting}",
"colorPalette": "${selectedStyle.colorPalette}",
"composition": "${selectedStyle.composition}",
"mood": "${selectedStyle.mood}",
"quality": "highly detailed, professional, masterpiece",
"render": "8k resolution, sharp focus"
}
Please strictly follow both the prompt content and these style specifications.`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment