Create in-memory image blob processing via `express`
vit1251 opened this issue · comments
How to processing image in-memory and delivery user without save on disk? i.e. convert image to Blob
or ArrayBuffer
to delivery with express
server handler?
Hi, the function pngPtr
for example will return a Buffer
which you can send directly to the client via res.send()
. Be aware to add the proper Content-Type
HTTP header in the response so that the client knows it is dealing with an image.
The function pngPtr
and jpegPtr
and gifPtr
are not (yet) well documented in the docs, but you can see for example from https://github.com/y-a-v-a/node-gd/blob/master/src/node_gd.cc#L562 that the macro RETURN_DATA
(available here: https://github.com/y-a-v-a/node-gd/blob/master/src/node_gd.h#L214 ) returns a Buffer.
I haven't tested this out but I think this could be more or less the solution:
const gd = require('node-gd');
app.get('/', async (req, res, next) => {
try {
const img = await gd.create(100,100);
// do other stuff with img
res.set('Content-Type', 'image/png');
res.send(img.pngPtr());
} catch(e) {
next();
}
});
Your code cause an segmentation error:
GD Warning: gd-png error: no colors in palette
Segmentation fault (core dumped)
But correct gd
source code sequence provide well image content. Thanks.
Cool to hear. I will review what causes that error but probably because an empty created paletted images does not contain any image actually.
Cool to hear. I will review what causes that error but probably because an empty created paletted images does not contain any image actually.
Yep. But await an exception instead Segfault
with coredump entire express server.