File size: 4,713 Bytes
6159e33 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | // 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; |