Class: ScaleDown::Image

Inherits:
Object
  • Object
show all
Includes:
Magick
Defined in:
lib/scale_down/image.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(properties = {}) ⇒ Image

Returns a new instance of Image.



40
41
42
43
44
45
46
# File 'lib/scale_down/image.rb', line 40

def initialize(properties = {})
  load_file(properties[:file]) do |file|
    resize(file, properties[:options])
    corrected = fix_color_space(file)
    @valid = write(corrected, properties[:out])
  end
end

Class Method Details

.geometry(properties) ⇒ Object



20
21
22
# File 'lib/scale_down/image.rb', line 20

def geometry(properties)
  validate_geometry Magick::Geometry.new(properties[:width], properties[:height], nil, nil, ">")
end

.scale(properties) ⇒ Object



16
17
18
# File 'lib/scale_down/image.rb', line 16

def scale(properties)
  new(properties).valid?
end

.validate_file_size(file_path) ⇒ Object



33
34
35
36
37
# File 'lib/scale_down/image.rb', line 33

def validate_file_size(file_path)
  unless File.size(file_path) < ScaleDown.max_file_size
    raise ScaleDown::FileSizeTooLarge
  end
end

.validate_geometry(geometry) ⇒ Object

Ensures that the dimensions are not both ‘auto’ and within the max dimensions



25
26
27
28
29
30
31
# File 'lib/scale_down/image.rb', line 25

def validate_geometry(geometry)
  geometry.tap do |g|
    total = g.width + g.height
    raise ScaleDown::InvalidGeometry if total == 0
    raise ScaleDown::InvalidGeometry if g.width > ScaleDown.max_dimensions[0] || g.height > ScaleDown.max_dimensions[1]
  end
end

Instance Method Details

#geometry(properties) ⇒ Object



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

def geometry(properties)
  self.class.geometry properties
end

#load_file(file_path) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/scale_down/image.rb', line 48

def load_file(file_path)
  self.class.validate_file_size(file_path)
  begin
    file = Magick::Image.read(file_path).first
    unless file.nil?
      yield file
      # file.destroy!
    end
  rescue Magick::ImageMagickError => e
    # TODO include the error in the HTTP response
  end
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  @valid ||= false
end