Class: ImageSize
- Inherits:
-
Object
- Object
- ImageSize
- Defined in:
- lib/image_size.rb,
lib/image_size/reader.rb,
lib/image_size/isobmff.rb,
lib/image_size/uri_reader.rb,
lib/image_size/media_types.rb,
lib/image_size/format_error.rb,
lib/image_size/chunky_reader.rb,
lib/image_size/string_reader.rb,
lib/image_size/stream_io_reader.rb,
lib/image_size/seekable_io_reader.rb
Overview
Experimental, not yet part of stable API
It adds ability to fetch image meta from HTTP server while downloading only needed chunks if the server recognises Range header, otherwise fetches only required amount of data
Defined Under Namespace
Modules: ChunkyReader, Reader, URIReader Classes: FormatError, ISOBMFF, SeekableIOReader, Size, StreamIOReader, StringReader
Constant Summary collapse
- 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
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Image format.
-
#height ⇒ Object
(also: #h)
readonly
Image height.
-
#width ⇒ Object
(also: #w)
readonly
Image width.
Class Method Summary collapse
-
.dpi ⇒ Object
Used for svg.
-
.dpi=(dpi) ⇒ Object
Used for svg.
-
.path(path) ⇒ Object
Given path to image finds its format, width and height.
- .url(url) ⇒ Object
Instance Method Summary collapse
-
#initialize(data) ⇒ ImageSize
constructor
Given image as any class responding to read and eof? or data as String, finds its format and dimensions.
-
#media_type ⇒ Object
Media type (formerly known as a MIME type).
-
#media_types ⇒ Object
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.
- #size ⇒ Object
Constructor Details
#initialize(data) ⇒ ImageSize
Given image as any class responding to read and eof? or data as String, finds its format and dimensions
50 51 52 53 54 55 |
# File 'lib/image_size.rb', line 50 def initialize(data) Reader.open(data) do |ir| @format = detect_format(ir) @width, @height = send("size_of_#{@format}", ir) if @format end end |
Instance Attribute Details
#format ⇒ Object (readonly)
Image format
58 59 60 |
# File 'lib/image_size.rb', line 58 def format @format end |
#height ⇒ Object (readonly) Also known as: h
Image height
65 66 67 |
# File 'lib/image_size.rb', line 65 def height @height end |
#width ⇒ Object (readonly) Also known as: w
Image width
61 62 63 |
# File 'lib/image_size.rb', line 61 def width @width end |
Class Method Details
.dpi ⇒ Object
Used for svg
40 41 42 |
# File 'lib/image_size.rb', line 40 def self.dpi @dpi || 72 end |
.dpi=(dpi) ⇒ Object
Used for svg
45 46 47 |
# File 'lib/image_size.rb', line 45 def self.dpi=(dpi) @dpi = dpi.to_f end |
.path(path) ⇒ Object
Given path to image finds its format, width and height
35 36 37 |
# File 'lib/image_size.rb', line 35 def self.path(path) new(Pathname.new(path)) end |
.url(url) ⇒ Object
124 125 126 |
# File 'lib/image_size/uri_reader.rb', line 124 def self.url(url) new(url.is_a?(URI) ? url : URI(url)) end |
Instance Method Details
#media_type ⇒ Object
Media type (formerly known as a MIME type)
74 75 76 |
# File 'lib/image_size.rb', line 74 def media_type MEDIA_TYPES.fetch(format, []).first end |
#media_types ⇒ Object
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
82 83 84 |
# File 'lib/image_size.rb', line 82 def media_types MEDIA_TYPES.fetch(format, []) end |