Dezgo

PAID
By dezgo | Updated 13 days ago | Artificial Intelligence/Machine Learning
Popularity

9.6 / 10

Latency

7,113ms

Service Level

99%

Health Check

N/A

Back to All Discussions

How to display the generated image using React.js?

Rapid account: Vegaslancer 1025
vegaslancer1025
a year ago

Hello, thanks for your reading.
I generated image using text input and get the result successfully.
But I canโ€™t display the result image in img tag.
The getting response is success and now I donโ€™t know how can I process this result and how can I display this result as image.
I checked the response in browser inspect window and the response is good. It seems to look as binary data.
So I want to know about how to display the image using React.js?
Please solve this problem. Best regards.

Rapid account: Davidzuk 94
davidzuk94 Commented a year ago

Hi. i have encountered the same problem and after consulting with chat gpt we came up with this.
hope it will help you

function from the component -

async function getTextToImage() {
try {
const { data, headers } = await textToImage();
if (headers && headers[โ€œcontent-typeโ€]) {
const blob = new Blob([data], { type: headers[โ€œcontent-typeโ€] });
const imageSrc = URL.createObjectURL(blob);
setImage(imageSrc);
} else {
console.error(โ€œError: Content-Type header is missing or undefined.โ€);
}
} catch (error) {
console.error(โ€œError:โ€, error);
}

service function -

export async function textToImage() {
const params = {
prompt:
โ€œmagical dolphin , masterpiece, highly detailed , photorealistic , trending, HQโ€,
guidance: 10,
steps: 30,
sampler: โ€œeuler_aโ€,
upscale: 1,
negative_prompt: โ€œfaceโ€,
model: โ€œepic_diffusion_1_1โ€,
};
try {
const response = await httpServices.post("/api/text2image", params, {
responseType: โ€œarraybufferโ€,
});

return response;

} catch (error) {
console.log(error);
return error;
}
}

Best of luck

Rapid account: Dezgo
dezgo Commented a year ago

Hello,
thanks for reaching out and for using the API!

I do not have experience with React.js in particular, but hereโ€™s a link that explains how to do it in vanilla JS: https://rapidapi.com/dezgo/api/dezgo/tutorials/how-to-query-the-api-from-a-web-browser-and-display-the-image-with-an-img-element

The most common cause could be that the response is interpreted as a string of text, instead of as a binary blob.

Hope this helps!

Best regards

Join in the discussion - add comment below:

Login / Signup to post new comments