From 4d566a5022c7f4d3dd0780e2d3db14bab19637b3 Mon Sep 17 00:00:00 2001 From: Ivan Kuchin Date: Tue, 16 Jan 2024 11:13:08 +0100 Subject: [PATCH] provide access to image media types, resolves #22 --- CHANGELOG.markdown | 1 + lib/image_size.rb | 14 ++++++++++++++ lib/image_size/media_types.rb | 32 ++++++++++++++++++++++++++++++++ spec/image_size_spec.rb | 4 ++++ 4 files changed, 51 insertions(+) create mode 100644 lib/image_size/media_types.rb diff --git a/CHANGELOG.markdown b/CHANGELOG.markdown index a0462f8..2ec350f 100644 --- a/CHANGELOG.markdown +++ b/CHANGELOG.markdown @@ -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) diff --git a/lib/image_size.rb b/lib/image_size.rb index c8cf71d..22aca2d 100644 --- a/lib/image_size.rb +++ b/lib/image_size.rb @@ -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' @@ -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 = /]*)>/.freeze diff --git a/lib/image_size/media_types.rb b/lib/image_size/media_types.rb new file mode 100644 index 0000000..7c2f23b --- /dev/null +++ b/lib/image_size/media_types.rb @@ -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 diff --git a/spec/image_size_spec.rb b/spec/image_size_spec.rb index 8b2d0d1..892c58a 100644 --- a/spec/image_size_spec.rb +++ b/spec/image_size_spec.rb @@ -68,6 +68,8 @@ 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, @@ -75,6 +77,8 @@ def supported_formats w: width, h: height, size: size, + media_type: media_type, + media_types: media_types, } end let(:file_data){ File.binread(path) }