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:

  • crop: pass true to this option to make the ouput image exactly the same dimensions as size. The default behaviour will resize the image without cropping any part meaning the image will be no bigger than the size. When :crop is true the final image is resized to fit as much as possible in the frame, and then crops it to make it exactly the dimensions declared by the size argument.
  • upsample: By default the image will never display larger than its original dimensions, no matter how large the size argument is. Pass true to use this option to allow upsampling, disabling the default behaviour.
  • padding: This option will pad the space around your image with a solid color to make it exactly the requested size. Pass true, for the default of white, or give it a text or pixel color like "red" or color(255, 127, 0). This is like the opposite of the crop option. Instead of trimming the image to make it exactly the requested size, it will make sure the entire image is visible, but adds space around the edges to make it the right dimensions.
  • stretch: Set this option to true and the image will not preserve its aspect ratio. The final image will stretch to fit the requested size. The resulting image is exactly the size you ask for.

Example:

  @photo.operate do |image|
    image.resize '200x200', :crop => true
  end

Methods

operate  

Public Instance methods

[Source]

    # 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

[Validate]