Module: CarrierWave::ImageScience

Defined in:
lib/carrierwave/processing/image_science.rb

Instance Method Summary collapse

Instance Method Details

#resize_to_fill(new_width, new_height) ⇒ Object

Resize the image to fit within the specified dimensions while retaining the aspect ratio of the original image. If necessary, crop the image in the larger dimension.

See even www.imagemagick.org/RMagick/doc/image3.html#resize_to_fill

Parameters

width (Integer)

the width to scale the image to

height (Integer)

the height to scale the image to



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/carrierwave/processing/image_science.rb', line 42

def resize_to_fill(new_width, new_height)
  ::ImageScience.with_image(self.current_path) do |img|
    width, height = extract_dimensions_for_crop(img.width, img.height, new_width, new_height)
    x_offset, y_offset = extract_placement_for_crop(width, height, new_width, new_height)

    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.current_path )
      end
    end
  end
end

#resize_to_fit(new_width, new_height) ⇒ Object

Resize the image to fit within the specified dimensions while retaining the original aspect ratio. The image may be shorter or narrower than specified in the smaller dimension but will not be larger than the specified values.

See even www.imagemagick.org/RMagick/doc/image3.html#resize_to_fit

Parameters

width (Integer)

the width to scale the image to

height (Integer)

the height to scale the image to



21
22
23
24
25
26
27
28
# File 'lib/carrierwave/processing/image_science.rb', line 21

def resize_to_fit(new_width, new_height)
  ::ImageScience.with_image(self.current_path) do |img|
    width, height = extract_dimensions(img.width, img.height, new_width, new_height)
    img.resize( width, height ) do |file|
      file.save( self.current_path )
    end
  end
end

#resize_to_limit(new_width, new_height) ⇒ Object

Resize the image to fit within the specified dimensions while retaining the original aspect ratio. Will only resize the image if it is larger than the specified dimensions. The resulting image may be shorter or narrower than specified in the smaller dimension but will not be larger than the specified values.

Parameters

width (Integer)

the width to scale the image to

height (Integer)

the height to scale the image to



67
68
69
70
71
72
73
# File 'lib/carrierwave/processing/image_science.rb', line 67

def resize_to_limit(new_width, new_height)
  ::ImageScience.with_image(self.current_path) do |img|
    if img.width > new_width or img.height > new_height
      resize_to_fit(new_width, new_height)
    end
  end
end