Last active
August 29, 2015 14:06
-
-
Save keshihoriuchi/690bb8d82672cbb75e34 to your computer and use it in GitHub Desktop.
画像ファイルフォーマットのチェック
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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