Module: UploadColumn::Manipulators::ImageScience

Defined in:
lib/upload_column/manipulators/image_science.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



6
7
8
# File 'lib/upload_column/manipulators/image_science.rb', line 6

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



6
7
8
# File 'lib/upload_column/manipulators/image_science.rb', line 6

def width
  @width
end

Instance Method Details

#crop_resized!(geometry) ⇒ Object

Resize and crop the image so that it will have the exact dimensions passed via geometry, geometry should be a string, formatted like ‘200x100’ where the first number is the height and the second is the width



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/upload_column/manipulators/image_science.rb', line 35

def crop_resized!( geometry )
  ::ImageScience.with_image(self.path) do |img|
    new_width, new_height = geometry.split('x').map{|i| i.to_i }

    width, height = extract_dimensions_for_crop(img.width, img.height, geometry)
    x_offset, y_offset = extract_placement_for_crop(width, height, geometry)

    img.resize( width, height ) do |i2|

      i2.with_crop( x_offset, y_offset, new_width + x_offset, new_height + y_offset) do |file|
        file.save( self.path )
      end
    end
  end
end

#load_manipulator_dependenciesObject

:nodoc:



8
9
10
# File 'lib/upload_column/manipulators/image_science.rb', line 8

def load_manipulator_dependencies #:nodoc:
  require 'image_science'
end

#process!(instruction) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/upload_column/manipulators/image_science.rb', line 12

def process!(instruction)
  if instruction.to_s =~ /^c(\d+x\d+)/
    crop_resized!($1)
  elsif instruction.to_s =~ /\d+x\d+/
    resize!(instruction)
  end
end

#resize!(geometry) ⇒ Object

Resize the image so that it will not exceed the dimensions passed via geometry, geometry should be a string, formatted like ‘200x100’ where the first number is the height and the second is the width



23
24
25
26
27
28
29
30
# File 'lib/upload_column/manipulators/image_science.rb', line 23

def resize!( geometry )
  ::ImageScience.with_image(self.path) do |img|
    width, height = extract_dimensions(img.width, img.height, geometry)
    img.resize( width, height ) do |file|
      file.save( self.path )
    end
  end
end