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');
});
});
});
-
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?Mark Rotteveel– Mark Rotteveel2022年02月11日 15:59:54 +00:00Commented 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).Mark Rotteveel– Mark Rotteveel2022年02月11日 16:01:28 +00:00Commented 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).Yurii– Yurii2022年02月11日 19:26:42 +00:00Commented 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)Yurii– Yurii2022年02月11日 19:31:47 +00:00Commented Feb 11, 2022 at 19:31
-
I'm using 'node-firebird'Yurii– Yurii2022年02月11日 20:00:03 +00:00Commented Feb 11, 2022 at 20:00
1 Answer 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.
5 Comments
SUBSTRING(OBJ_IMAGE1 FROM 6) - an error ocurres Error: invalid BLOB IDfs.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.яяШя 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 libraryExplore related questions
See similar questions with these tags.