1

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

Mark Rotteveel
110k241 gold badges160 silver badges233 bronze badges
asked Feb 11, 2022 at 12:52
13
  • Your saving it as a PNG, but you mention it is a BMP. What is the actual file type? Can you open the file if you save the blob to a file from your favourite query tool? Also, which Firebird Node.js driver are you using? Commented Feb 11, 2022 at 15:59
  • As an aside, negative blob sub-types have no special meaning in Firebird, it just means it is a user-defined blob-type (which might have special meaning for the application reading or writing the data). Commented Feb 11, 2022 at 16:01
  • Actual type of image, inserted in firebird db, using application, is a .bmp file. I've tried to save as .bmp, .jpeg, .png. But Windows keeps telling that the file is a wrong pixel picture bmp (while trying to open using Paint). Commented Feb 11, 2022 at 19:26
  • @MarkRotteveel I'm using IBExpert as a DB viewer. I can see data as hex, but as a picture it is not loading (image I added in question) Commented Feb 11, 2022 at 19:31
  • I'm using 'node-firebird' Commented Feb 11, 2022 at 20:00

1 Answer 1

1

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.

answered Feb 12, 2022 at 12:54
Sign up to request clarification or add additional context in comments.

5 Comments

These 5 bytes most likely are from a header added by Delphi TDBImage that was supposed to contain image type and other internal info but didn't get much support since introduction.
When I try to use SUBSTRING(OBJ_IMAGE1 FROM 6) - an error ocurres Error: invalid BLOB ID
I tried to compare hex data of the same picture. This is actual buffer, which was seen durring fs.readFileSync(): <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 02 01 00 48 00 48 00 00 ff e1 0e bf 45 78 69 66 00 00 4d 4d.... And this data is from ibExpert: FF D8 FF E0 00 10 4A 46 FF 49 46 00 01 02 01 00 48 7F 02 00 FF E1 0E EB BF 25 78 69 66 12 7B 4D. It's not the same(
FF D8 FF E0 xx xx 4A 46 49 46 00 is also JPEG according to that File Signatures page.
that яяШя in his screenshot is a typical JFIF (JPEG with tags) header when seen in Russian Windows codepage. The prefix 0ドルf15 might be a sream length saved as int32, with decimal value 3861. Or something else. I believe few months ago we already saw a very similar question, which was bound to some specific language/library. The data in the blob was not the picture file, but a picture object of that library/language, with some tagged data before and/or after the stream. The topic starter has to show the piece code of the program that writes to the database, to learn language and library

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.