Module: CarrierWave::ImageScience

Extended by:
ActiveSupport::Concern
Defined in:
lib/carrierwave/processing/image_science.rb

Defined Under Namespace

Modules: ClassMethods

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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/carrierwave/processing/image_science.rb', line 57

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)

    # check if if new dimensions are too small for the new image
    if width < new_width
      width = new_width
      height = (new_width.to_f*(img.height.to_f/img.width.to_f)).round
    elsif height < new_height
      height = new_height
      width = (new_height.to_f*(img.width.to_f/img.height.to_f)).round
    end

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

      # check to make sure offset is not negative
      if x_offset < 0
        x_offset = 0
      end
      if y_offset < 0
        y_offset = 0
      end

      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



36
37
38
39
40
41
42
43
# File 'lib/carrierwave/processing/image_science.rb', line 36

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



99
100
101
102
103
104
105
# File 'lib/carrierwave/processing/image_science.rb', line 99

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