rynn-k / gists
gpt-image-edit.js javascript
const axios = require('axios');
const fs = require('fs');

async function gptimage({ prompt, image, model = 'gpt-image-1' } = {}) {
    try {
        const models = ['gpt-image-1', 'gpt-image-1.5'];
        
        if (!prompt) throw new Error('Prompt is required.');
        if (!Buffer.isBuffer(image)) throw new Error('Image must be a buffer.');
        if (!models.includes(model)) throw new Error(`Available models: ${models.join(', ')}.`);
        
        const { data } = await axios.post('https://ghibli-proxy.netlify.app/.netlify/functions/ghibli-proxy', {
            image: 'data:image/png;base64,' + image.toString('base64'),
            prompt: prompt,
            model: model,
            n: 1,
            size: 'auto',
            quality: 'low'
        }, {
            headers: {
                origin: 'https://overchat.ai',
                referer: 'https://overchat.ai/',
                'user-agent': 'Mozilla/5.0 (Linux; Android 15; SM-F958 Build/AP3A.240905.015) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.86 Mobile Safari/537.36'
            }
        });
        
        const result = data?.data?.[0]?.b64_json;
        if (!result) throw new Error('No result found.');
        
        return Buffer.from(result, 'base64');
    } catch (error) {
        throw new Error(error.message);
    }
}

// Usage:
gptimage({
    prompt: 'change the skin color to black',
    image: fs.readFileSync('./ex/image.jpg'),
    model: 'gpt-image-1.5'
}).then(res => {
    fs.writeFileSync('./result.png', res);
});
1545 bytes ยท Updated Feb 27, 2026