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



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

#formatObject (readonly)

Image format



58
59
60
# File 'lib/image_size.rb', line 58

def format
  @format
end

#heightObject (readonly) Also known as: h

Image height



65
66
67
# File 'lib/image_size.rb', line 65

def height
  @height
end

#widthObject (readonly) Also known as: w

Image width



61
62
63
# File 'lib/image_size.rb', line 61

def width
  @width
end

Class Method Details

.dpiObject

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_typeObject

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_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



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

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

#sizeObject

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



69
70
71
# File 'lib/image_size.rb', line 69

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