Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Ruby isJPEG? method
# @param [Object] block A 512 byte block to check if it contains a jpeg header in first 4 bytes
def isJPEG?(block)
# Encodings are diff between reading an image file and strings so block[0] == 0xFF wont work
# http://stackoverflow.com/questions/16815308/ruby-comparing-hex-value-to-string
jpeg_header_part = ['FFD8FF'].pack('H*')
return block[0,3] == jpeg_header_part && (block[3] == 0xe0.chr || block[3] == 0x0e1.chr)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment