Skip to content

Instantly share code, notes, and snippets.

@tdshipley
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdshipley/85508efbe53b3691ca72 to your computer and use it in GitHub Desktop.
Save tdshipley/85508efbe53b3691ca72 to your computer and use it in GitHub Desktop.
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