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 3bfb94e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
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 methods [#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
4 changes: 3 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ gem 'image_size', '~> 3.0'
```ruby
image_size = ImageSize.path('spec/images/jpeg/436x429.jpeg')

image_size.format #=> :jpec
image_size.format #=> :jpeg
image_size.width #=> 436
image_size.height #=> 429
image_size.w #=> 436
Expand All @@ -38,6 +38,8 @@ image_size.size.width #=> 436
image_size.size.height #=> 429
image_size.size.w #=> 436
image_size.size.h #=> 429
image_size.media_type #=> "image/jpeg"
image_size.media_types #=> ["image/jpeg"]
```

Or using `IO` object:
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 3bfb94e

Please sign in to comment.