Class: ImageSize

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ ImageSize

Given image as any class responding to read and eof? or data as String, finds its format and dimensions



66
67
68
69
70
71
72
# File 'lib/image_size.rb', line 66

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

Instance Attribute Details

#byte_sizeObject (readonly)

Returns the value of attribute byte_size.



85
86
87
# File 'lib/image_size.rb', line 85

def byte_size
  @byte_size
end

#formatObject (readonly)

Image format



75
76
77
# File 'lib/image_size.rb', line 75

def format
  @format
end

#heightObject (readonly) Also known as: h

Image height



82
83
84
# File 'lib/image_size.rb', line 82

def height
  @height
end

#widthObject (readonly) Also known as: w

Image width



78
79
80
# File 'lib/image_size.rb', line 78

def width
  @width
end

Class Method Details

.chunk_sizeObject

Size of chunk to use by IO and URI readers



52
53
54
# File 'lib/image_size.rb', line 52

def self.chunk_size
  @chunk_size || 4096
end

.chunk_size=(chunk_size) ⇒ Object

Set size of chunk to use by IO and URI readers



57
58
59
60
61
62
63
# File 'lib/image_size.rb', line 57

def self.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

.dpiObject

Used for svg and emf



40
41
42
# File 'lib/image_size.rb', line 40

def self.dpi
  @dpi || 72.0
end

.dpi=(dpi) ⇒ Object

Used for svg and emf



45
46
47
48
49
# File 'lib/image_size.rb', line 45

def self.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_redirectsObject

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



35
36
37
# File 'lib/image_size.rb', line 35

def self.path(path)
  new(Pathname.new(path))
end

.uri_checkerObject

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_typeObject

Media type (formerly known as a MIME type)



93
94
95
# File 'lib/image_size.rb', line 93

def media_type
  media_types.first
end

#media_typesObject

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



101
102
103
# File 'lib/image_size.rb', line 101

def media_types
  MEDIA_TYPES.fetch(format, [])
end

#sizeObject

get image width and height as an array which to_s method returns “##widthx##height



88
89
90
# File 'lib/image_size.rb', line 88

def size
  Size.new([width, height]) if format
end