Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Created October 1, 2008 22:53
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 tenderlove/14218 to your computer and use it in GitHub Desktop.
Save tenderlove/14218 to your computer and use it in GitHub Desktop.
####
# Get the dimensions for a Jpeg
#
class JpegFile
def initialize file
@io = File.open(file, 'rb')
end
def dimensions
raise 'Not a jpeg' unless @io.read(2).unpack('CC') == [0xFF, 0xD8]
loop do
bytes = @io.read(2).unpack('CC')
raise unless bytes[0] == 0xFF
return @io.read(7)[3..-1].unpack('nn') if bytes[1] === 0xC0..0xC3
@io.read(@io.read(2).unpack('n').first - 2)
end
end
end
p JpegFile.new(ARGV[0]).dimensions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment