Skip to content

Commit

Permalink
media types
Browse files Browse the repository at this point in the history
  • Loading branch information
toy committed Jan 16, 2024
1 parent 091fc4b commit 6375368
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/image_size.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

require 'image_size/isobmff'
require 'image_size/format_error'
require 'image_size/media_types'
require 'image_size/reader'
require 'image_size/seekable_io_reader'
require 'image_size/stream_io_reader'
Expand Down Expand Up @@ -69,6 +70,16 @@ def size
Size.new([width, height]) if format
end

# Media type (formerly known as a MIME type)
def media_type
MEDIA_TYPES.fetch(format, []).first
end

# All media types
def media_types
MEDIA_TYPES.fetch(format, [])
end

private

SVG_R = /<svg\b([^>]*)>/.freeze
Expand Down
32 changes: 32 additions & 0 deletions lib/image_size/media_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

class ImageSize
MEDIA_TYPES = {
apng: %w[image/apng image/vnd.mozilla.apng],
avif: %w[image/avif],
bmp: %w[image/bmp],
cur: %w[image/vnd.microsoft.icon],
emf: %w[image/emf],
gif: %w[image/gif],
heic: %w[image/heic image/heif],
ico: %w[image/x-icon image/vnd.microsoft.icon],
j2c: %w[image/j2c],
jp2: %w[image/jp2],
jpeg: %w[image/jpeg],
jpx: %w[image/jpx],
mng: %w[image/x-mng video/x-mng],
pam: %w[image/x-portable-arbitrarymap],
pbm: %w[image/x-portable-bitmap image/x-portable-anymap],
pcx: %w[image/vnd.zbrush.pcx],
pgm: %w[image/x-portable-graymap image/x-portable-anymap],
png: %w[image/png],
ppm: %w[image/x-portable-pixmap image/x-portable-anymap],
psd: %w[image/vnd.adobe.photoshop],
svg: %w[image/svg+xml],
swf: %w[application/vnd.adobe.flash.movie],
tiff: %w[image/tiff],
webp: %w[image/webp],
xbm: %w[image/x-xbitmap],
xpm: %w[image/x-xpixmap],
}.freeze
end
4 changes: 4 additions & 0 deletions spec/image_size_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,17 @@ def supported_formats
match = /(\d+)x(\d+)\.([^.]+)$/.match(name)
width, height, format = match[1].to_i, match[2].to_i, match[3].to_sym if match
size = format && [width, height]
media_types = ImageSize::MEDIA_TYPES[format] || []
media_type = format && media_types.first.to_s
{
format: format,
width: width,
height: height,
w: width,
h: height,
size: size,
media_type: media_type,
media_types: media_types,
}
end
let(:file_data){ File.open(path, 'rb', &:read) }
Expand Down

0 comments on commit 6375368

Please sign in to comment.