6

Is there any way to do this using node, whether natively or with a plugin?

What I'm trying to accomplish is to choose loseless or lossy image compression depending on the input type. Loseless on a large JPEG is a storage catastrophe.

Slbox
  • 10,957
  • 15
  • 54
  • 106
  • Where is the base64 data coming from, and why doesn't the source tell you what type the data is? – loganfsmyth Apr 05 '18 at 06:55
  • Hey Mark, I'm simply not sure yet if it solves my issue as I got pulled in a different direction. I am thinking maybe no, because I'm often dealing with clipboard data, and I'm not sure that it will have the leading bytes that identify the data, but I can't be sure yet. Thank you for your answer though. If it solves my problem, or if I don't get to it soon, as it's the best answer here, I will come back to mark it answered. Thanks again. – Slbox Apr 09 '18 at 16:52

2 Answers2

13

The first eight bytes of a PNG file always contain the following values - see PNG Specification:

(decimal)              137  80  78  71  13  10  26  10
(hexadecimal)           89  50  4e  47  0d  0a  1a  0a
(ASCII C notation)    \211   P   N   G  \r  \n \032 \n

So, if I take 8 bytes from the start of any PNG file and base64 encode it as follows, I get:

head -c8 test.png | base64
iVBORw0KGgo=

The first 2 bytes of every JPEG file contain ff d8 in hex - see Wikipedia entry for JPEG. So if I take any JPEG file and base64 encode the first two bytes as follows, I get:

head -c2 test.jpg | base64
/9g=

So my suggestion would be to look at the first few (10 for PNG and 2 for JPEG, always excluding the =) characters of your base64-encoded file and see if they match what I am suggesting and then use that as the determinant - be sure to output error messages if your string matches neither in case the test is not sufficiently thorough for some reason!


Why 10 characters for PNG? Because the guaranteed signature is 8 bytes, i.e. 64 bits and base64 splits into 6 bits at a time to generate a character, so the first 10 characters are the first 60 bits. The 11th character will vary depending on what follows the signature.

Same logic for JPEG... 2 bytes is 16 bits, which means 2 characters each corresponding to 6 bits are guaranteed. The 3rd character will vary depending on what follows the 2-byte SOI marker.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
1

@MarkSetchell's answer above is correct in theory. However, in practice, it doesn't work for JPGs!

It is true that head -c2 test.jpg | base64 produces /9g

However

head -c3 test.jpg | base64`
/9j/

So, if you want to "Determine if a base64 string or a buffer contains JPEG" you need to test that it starts with /9j not /9g!

zen
  • 571
  • 5
  • 13