Class: ImageSize

Inherits:
Object
  • Object
show all
Defined in:
lib/image_size.rb

Overview

Determine image format and size

Defined Under Namespace

Classes: FormatError, ImageReader, Size

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



61
62
63
64
65
66
67
# File 'lib/image_size.rb', line 61

def initialize(data)
  ir = ImageReader.new(data)
  @format = detect_format(ir)
  return unless @format

  @width, @height = send("size_of_#{@format}", ir)
end

Instance Attribute Details

#formatObject (readonly)

Image format



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

def format
  @format
end

#heightObject (readonly) Also known as: h

Image height



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

def height
  @height
end

#widthObject (readonly) Also known as: w

Image width



73
74
75
# File 'lib/image_size.rb', line 73

def width
  @width
end

Class Method Details

.dpiObject

Used for svg



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

def self.dpi
  @dpi || 72
end

.dpi=(dpi) ⇒ Object

Used for svg



56
57
58
# File 'lib/image_size.rb', line 56

def self.dpi=(dpi)
  @dpi = dpi.to_f
end

.path(path) ⇒ Object

Given path to image finds its format, width and height



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

def self.path(path)
  File.open(path, 'rb'){ |f| new(f) }
end

Instance Method Details

#sizeObject

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



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

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