How to create an image file from Firebird blob data?

776 Views Asked by At

I have a Firebird DB table, which contains fields with image data (as blob sybtype -5). I need to get this data and convert it to an image file. Code example below creates a file (but size is very large, and while trying to open it - it is said, that file is not right BMP file). What am I missing here?

result[0].OBJ_IMAGE1((err, name, eventEmitter) => {
    if(err) throw err;

    let chunks = [];
    eventEmitter.on('data', chunk => {
        chunks.push(chunk);                
    });
    eventEmitter.once('end', () => {
        let buffer = Buffer.concat(chunks);         

        fs.writeFile('test.png',  buffer, err => {
            if(err) throw err;
            console.log('image file written');
        });            
    });
});

enter image description here

enter image description here

1

There are 1 best solutions below

5
Mark Rotteveel On

I have tested your code, and it will correctly write out the blob to a file. Your problem is therefore - as also discussed in the comments - one of not knowing what the actual image file type is, so you're storing it with the wrong file extension. You need to know the file type (or encoding, or encryption) used by the application storing the image data to be able to select the right file extension, image viewer to display the data and/or additional transformations required to get a viewable file.

Looking at the example data and the File Signatures link Marcodor provided, it is possible that the stored data is type "Standard JPEG file with Exif metadata" (FF D8 FF E1 xx xx 45 78 69 66 00), but the file is prefixed with 5 bytes of application specific data (maybe some sort of type identifier, or a file length, or something like that). Based on that, try using SUBSTRING(OBJ_IMAGE1 FROM 6) and see if saving as jpg allows you to open the file (NOTE: This doesn't necessarily mean all files stored in these blobs are JPEGs!).

As an aside, it is more efficient to store the image using:

result[0].OBJ_IMAGE1((err, name, e) => {
    if (err) throw err;
    var imgStream = fs.createWriteStream('filename.ext')
    e.pipe(imgStream);
});

Using pipe will write the chunks directly to a file as they are received, instead of accumulating the entire file into memory before writing it out.