Skip to content

Instantly share code, notes, and snippets.

@keshihoriuchi
Last active August 29, 2015 14:06
Show Gist options
  • Save keshihoriuchi/690bb8d82672cbb75e34 to your computer and use it in GitHub Desktop.
Save keshihoriuchi/690bb8d82672cbb75e34 to your computer and use it in GitHub Desktop.
画像ファイルフォーマットのチェック
HEADERS = {
[0xFF, 0xD8] => 'jpg',
[0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A] => 'png',
[0x47, 0x49, 0x46] => 'gif'
}
%w(jpg png gif).each do |suffix|
p suffix
path = File.expand_path("sample.#{suffix}", File.dirname(__FILE__))
fuga = nil
File.open(path, 'rb') do |io|
fuga = io.read(8).unpack('C8')
end
HEADERS.map do |prefix, format|
puts "#{format}: #{prefix == fuga.slice(0, prefix.size)}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment