Skip to content

Commit

Permalink
provide access to image media types, resolves #22
Browse files Browse the repository at this point in the history
  • Loading branch information
toy committed Jan 16, 2024
1 parent 928c28b commit 4d566a5
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## unreleased

* Provide access to media types using media_type and media_types [#22](https://github.com/toy/image_size/issues/22) [@toy](https://github.com/toy)
* Allow fetching from HTTP server by requiring image_size/uri [@toy](https://github.com/toy)
* Fix for ArgumentError when requiring only image_size/uri_reader (without image_size) [@toy](https://github.com/toy)
* Require ruby 1.9.3 [@toy](https://github.com/toy)
Expand Down
14 changes: 14 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,19 @@ 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:
# * commonly used and official like for apng and ico
# * main and compatible like for heic and pnm (pbm, pgm, ppm)
# * multiple unregistered like for mng
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[video/x-mng image/x-mng],
pam: %w[image/x-portable-arbitrarymap],
pbm: %w[image/x-portable-bitmap image/x-portable-anymap],
pcx: %w[image/x-pcx 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/x-shockwave-flash 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 @@ -68,13 +68,17 @@ def supported_formats
format = match[3].to_sym
end
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.binread(path) }
Expand Down

0 comments on commit 4d566a5

Please sign in to comment.