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], icns: %w[image/x-icns], 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
Class Attribute Summary collapse
-
.use_display_pixels ⇒ Object
Use display pixels instead of physical/image pixels for icns.
Instance Attribute Summary collapse
-
#byte_size ⇒ Object
readonly
Returns the value of attribute byte_size.
-
#format ⇒ Object
readonly
Image format.
-
#height ⇒ Object
(also: #h)
readonly
Image height.
-
#width ⇒ Object
(also: #w)
readonly
Image width.
Class Method Summary collapse
-
.chunk_size ⇒ Object
Size of chunk to use by IO and URI readers.
-
.chunk_size=(chunk_size) ⇒ Object
Set size of chunk to use by IO and URI readers.
-
.dpi ⇒ Object
DPI used for svg and emf.
-
.dpi=(dpi) ⇒ Object
Set DPI used for svg and emf.
-
.max_redirects ⇒ Object
Maximum number of redirects.
-
.max_redirects=(max_redirects) ⇒ Object
Set maximum number of redirects.
-
.path(path) ⇒ Object
Given path to image finds its format, width and height.
-
.uri_checker ⇒ Object
Hook to call before making every request.
-
.uri_checker=(uri_checker) ⇒ Object
Set hook to call before making every request.
- .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
71 72 73 74 75 76 77 |
# File 'lib/image_size.rb', line 71 def initialize(data) Reader.open(data) do |ir| @format = detect_format(ir) @width, @height = send("size_of_#{@format}", ir) if @format @byte_size = ir.byte_size end end |
Class Attribute Details
.use_display_pixels ⇒ Object
Use display pixels instead of physical/image pixels for icns
53 54 55 |
# File 'lib/image_size.rb', line 53 def use_display_pixels @use_display_pixels end |
Instance Attribute Details
#byte_size ⇒ Object (readonly)
Returns the value of attribute byte_size.
90 91 92 |
# File 'lib/image_size.rb', line 90 def byte_size @byte_size end |
#format ⇒ Object (readonly)
Image format
80 81 82 |
# File 'lib/image_size.rb', line 80 def format @format end |
#height ⇒ Object (readonly) Also known as: h
Image height
87 88 89 |
# File 'lib/image_size.rb', line 87 def height @height end |
#width ⇒ Object (readonly) Also known as: w
Image width
83 84 85 |
# File 'lib/image_size.rb', line 83 def width @width end |
Class Method Details
.chunk_size ⇒ Object
Size of chunk to use by IO and URI readers
56 57 58 |
# File 'lib/image_size.rb', line 56 def chunk_size @chunk_size || 4096 end |
.chunk_size=(chunk_size) ⇒ Object
Set size of chunk to use by IO and URI readers
61 62 63 64 65 66 67 |
# File 'lib/image_size.rb', line 61 def chunk_size=(chunk_size) unless chunk_size.nil? || (chunk_size.is_a?(Integer) && chunk_size > 0) fail ArgumentError, "chunk_size should be a positive Integer or nil, got #{chunk_size}" end @chunk_size = chunk_size end |
.dpi ⇒ Object
DPI used for svg and emf
41 42 43 |
# File 'lib/image_size.rb', line 41 def dpi @dpi || 72.0 end |
.dpi=(dpi) ⇒ Object
Set DPI used for svg and emf
46 47 48 49 50 |
# File 'lib/image_size.rb', line 46 def dpi=(dpi) fail ArgumentError, "dpi should be nil or positive, got #{dpi}" unless dpi.nil? || dpi > 0 @dpi = dpi ? dpi.to_f : nil end |
.max_redirects ⇒ Object
Maximum number of redirects
152 153 154 |
# File 'lib/image_size/uri_reader.rb', line 152 def self.max_redirects @max_redirects || 5 end |
.max_redirects=(max_redirects) ⇒ Object
Set maximum number of redirects
157 158 159 160 161 162 163 |
# File 'lib/image_size/uri_reader.rb', line 157 def self.max_redirects=(max_redirects) unless max_redirects.nil? || (max_redirects.is_a?(Integer) && max_redirects >= 0) fail ArgumentError, "max_redirects should be 0, a positive Integer or nil, got #{max_redirects}" end @max_redirects = max_redirects end |
.path(path) ⇒ Object
Given path to image finds its format, width and height
36 37 38 |
# File 'lib/image_size.rb', line 36 def path(path) new(Pathname.new(path)) end |
.uri_checker ⇒ Object
Hook to call before making every request
166 167 168 |
# File 'lib/image_size/uri_reader.rb', line 166 def self.uri_checker @uri_checker || proc{ |_uri| } end |
.uri_checker=(uri_checker) ⇒ Object
Set hook to call before making every request
171 172 173 174 175 176 177 |
# File 'lib/image_size/uri_reader.rb', line 171 def self.uri_checker=(uri_checker) unless uri_checker.nil? || uri_checker.respond_to?(:call) fail ArgumentError, "uri_checker should respond to call or be nil, got #{uri_checker}" end @uri_checker = uri_checker end |
.url(url) ⇒ Object
147 148 149 |
# File 'lib/image_size/uri_reader.rb', line 147 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)
98 99 100 |
# File 'lib/image_size.rb', line 98 def media_type media_types.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
106 107 108 |
# File 'lib/image_size.rb', line 106 def media_types MEDIA_TYPES.fetch(format, []) end |