| |
| |
|
|
| 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(); |
|
|
| |
| const configuration = new Configuration({ |
| apiKey: process.env.OPENAI_API_KEY, |
| }); |
|
|
| const openai = new OpenAIApi(configuration); |
|
|
| |
| const upload = multer({ |
| dest: 'uploads/', |
| limits: { |
| fileSize: 10 * 1024 * 1024, |
| }, |
| fileFilter: (req, file, cb) => { |
| if (file.mimetype.startsWith('image/')) { |
| cb(null, true); |
| } else { |
| cb(new Error('Only image files are allowed')); |
| } |
| } |
| }); |
|
|
| |
| 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 (imagePath) { |
| const imageBuffer = fs.readFileSync(imagePath); |
| base64Image = imageBuffer.toString('base64'); |
| |
| fs.unlinkSync(imagePath); |
| } |
|
|
| |
| 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 |
| `; |
|
|
| |
| 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 |
| }); |
| } |
| }); |
|
|
| |
| 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' |
| }); |
| } |
|
|
| |
| 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 |
| `; |
|
|
| |
| const response = await openai.createImageEdit( |
| |
| Buffer.from(image.split(',')[1], 'base64'), |
| prompt, |
| null, |
| 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; |