wear-fun / api /mockup-generator.js
dodey917's picture
good use the OpenAI鈥檚 DALL路E / gpt-image-1
6159e33 verified
// This is a Node.js backend file for secure API handling
// Place this in your backend server, not in the frontend
const express = require('express');
const { Configuration, OpenAIApi } = require('openai');
const multer = require('multer');
const fs = require('fs');
const path = require('path');
const router = express.Router();
// Configure OpenAI - IMPORTANT: Store this in environment variables
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY, // Never expose API keys in frontend
});
const openai = new OpenAIApi(configuration);
// Configure multer for file uploads
const upload = multer({
dest: 'uploads/',
limits: {
fileSize: 10 * 1024 * 1024, // 10MB limit
},
fileFilter: (req, file, cb) => {
if (file.mimetype.startsWith('image/')) {
cb(null, true);
} else {
cb(new Error('Only image files are allowed'));
}
}
});
// Generate realistic T-shirt mockup endpoint
router.post('/generate-mockup', upload.single('image'), async (req, res) => {
try {
const { caption, shirtColor = 'white' } = req.body;
const imagePath = req.file ? req.file.path : null;
if (!imagePath && !req.body.image) {
return res.status(400).json({
success: false,
error: 'No image provided'
});
}
let base64Image = req.body.image;
// If file was uploaded, convert to base64
if (imagePath) {
const imageBuffer = fs.readFileSync(imagePath);
base64Image = imageBuffer.toString('base64');
// Clean up uploaded file
fs.unlinkSync(imagePath);
}
// Construct the detailed prompt for DALL-E
const prompt = `
Generate a high-quality, realistic photograph of a plain unisex T-shirt on a clean, neutral background.
The T-shirt must:
- Be ${shirtColor} colored
- Have soft, natural lighting with visible fabric texture
- Show natural folds and wrinkles in the fabric
- Have the following design seamlessly printed on it: "${caption}"
- The printed design should follow the shirt's wrinkles, lighting, and shadows naturally
- Blend the design so it appears part of the fabric, not pasted on
- Preserve realistic shadows, highlights, and depth
- Style: photorealistic, commercial product photography, high detail
`;
// Call DALL-E API
const response = await openai.createImage({
model: "dall-e-3",
prompt: prompt,
n: 1,
size: "1024x1024",
quality: "hd",
response_format: "url",
});
const imageUrl = response.data.data[0].url;
res.json({
success: true,
imageUrl: imageUrl,
message: 'Mockup generated successfully'
});
} catch (error) {
console.error('Mockup generation error:', error);
res.status(500).json({
success: false,
error: 'Failed to generate mockup: ' + error.message
});
}
});
// Alternative endpoint for when image is sent as base64
router.post('/generate-mockup-base64', async (req, res) => {
try {
const { image, caption, shirtColor = 'white' } = req.body;
if (!image) {
return res.status(400).json({
success: false,
error: 'No image provided'
});
}
// Same DALL-E prompt as above
const prompt = `
Generate a high-quality, realistic photograph of a plain unisex T-shirt on a clean, neutral background.
The T-shirt must:
- Be ${shirtColor} colored
- Have soft, natural lighting with visible fabric texture
- Show natural folds and wrinkles in the fabric
- Have the user's uploaded image seamlessly printed on the chest area
- The printed design should follow the shirt's wrinkles, lighting, and shadows naturally
- Blend the design so it appears part of the fabric, not pasted on
- Preserve realistic shadows, highlights, and depth
- Include the text "${caption}" as part of the design if appropriate
- Style: photorealistic, commercial product photography, high detail
`;
// Call DALL-E API with image editing capability
const response = await openai.createImageEdit(
// Convert base64 to file buffer for API
Buffer.from(image.split(',')[1], 'base64'),
prompt,
null, // No mask needed for full image
1,
"1024x1024"
);
const imageUrl = response.data.data[0].url;
res.json({
success: true,
imageUrl: imageUrl,
message: 'Mockup generated successfully'
});
} catch (error) {
console.error('Mockup generation error:', error);
res.status(500).json({
success: false,
error: 'Failed to generate mockup: ' + error.message
});
}
});
module.exports = router;