Class | Fleximage::Operator::Resize |
In: |
lib/fleximage/operator/resize.rb
|
Parent: | Operator::Base |
Resize this image, constraining proportions. Options allow cropping, stretching, upsampling and padding.
image.resize(size, options = {})
size is size of the output image after the resize operation. Accepts either ‘123x456‘ format or [123, 456] format.
Use the following keys in the options hash:
Example:
@photo.operate do |image| image.resize '200x200', :crop => true end
# File lib/fleximage/operator/resize.rb, line 39 39: def operate(size, options = {}) 40: options = options.symbolize_keys 41: 42: # Find dimensions 43: x, y = size_to_xy(size) 44: 45: # prevent upscaling unless :usample param exists. 46: unless options[:upsample] 47: x = @image.columns if x > @image.columns 48: y = @image.rows if y > @image.rows 49: end 50: 51: # Perform image resize 52: case 53: when options[:crop] && !options[:crop].is_a?(Hash) && @image.respond_to?(:crop_resized!) 54: # perform resize and crop 55: scale_and_crop([x, y]) 56: 57: when options[:stretch] 58: # stretch the image, ignoring aspect ratio 59: stretch([x, y]) 60: 61: else 62: # perform the resize without crop 63: scale([x, y]) 64: 65: end 66: 67: # apply padding if necesary 68: if padding_color = options[:padding] 69: # get color 70: padding_color = 'white' if padding_color == true 71: 72: # get original x and y. This makes it play nice if the requested size is larger 73: # than the image and upsampling is not allowed. 74: x, y = size_to_xy(size) 75: 76: # get proper border sizes 77: x_border = [0, (x - @image.columns + 1) / 2].max 78: y_border = [0, (y - @image.rows + 1) / 2].max 79: 80: # apply padding 81: @image.border!(x_border, y_border, padding_color) 82: 83: # crop to remove possible extra pixel 84: @image.crop!(0, 0, x, y, true) 85: end 86: 87: return @image 88: end