Class Fleximage::Operator::Base
In: lib/fleximage/operator/base.rb
Parent: Object

The Operator::Base class is what all other Operator classes inherit from. To write your own Operator class, simply inherit from this class, and implement your own operate methods, with your own arguments. Just return a new RMagick image object that represents the new image, and the model will be updated automatically.

You have access to a few instance variables in the operate method:

  • @image : The current image from the model. Use this is a starting point for all transformations.
  • @model : The model instance that this image transformation is happenining in. Use it to get data out of your model for display in your image.

Methods

Public Instance methods

Perform the operation. Override this method in your Operator::Base subclasses in order to write your own image operators.

[Source]

    # File lib/fleximage/operator/base.rb, line 41
41:       def operate(*args)
42:         raise "Override this method in your own subclass."
43:       end

Scale the image, respecting aspect ratio. Operation will happen in the main @image unless you supply the img argument to operate on instead.

[Source]

    # File lib/fleximage/operator/base.rb, line 72
72:       def scale(size, img = nil)
73:         (img || @image).change_geometry!(size_to_xy(size).join('x')) do |cols, rows, img|
74:           cols = 1 if cols < 1
75:           rows = 1 if rows < 1
76:           img.resize!(cols, rows)
77:         end
78:       end

Scale to the desired size and crop edges off to get the exact dimensions needed. Operation will happen in the main @image unless you supply the img argument to operate on instead.

[Source]

    # File lib/fleximage/operator/base.rb, line 83
83:       def scale_and_crop(size, img = nil)
84:         (img || @image).crop_resized!(*size_to_xy(size))
85:       end

Converts a size object to an [x,y] array. Acceptible formats are:

  • 10
  • "10"
  • "10x20"
  • [10, 20]

Usage:

  x, y = size_to_xy("10x20")

[Source]

    # File lib/fleximage/operator/base.rb, line 59
59:       def size_to_xy(size)
60:         if size.is_a?(Array) && size.size == 2
61:           size
62:         elsif size.to_s.include?('x')
63:           size.split('x').collect(&:to_i)
64:         else
65:           [size.to_i, size.to_i]
66:         end
67:       end

Resize the image, with no respect to aspect ratio. Operation will happen in the main @image unless you supply the img argument to operate on instead.

[Source]

    # File lib/fleximage/operator/base.rb, line 90
90:       def stretch(size, img = nil)
91:         (img || @image).resize!(*size_to_xy(size))
92:       end

Convert a symbol to an RMagick blending mode.

The blending mode governs how the overlay gets composited onto the image. You can get some funky effects with modes like :copy_cyan or :screen. For a full list of blending modes checkout the RMagick documentation (www.simplesystems.org/RMagick/doc/constants.html#CompositeOperator). To use a blend mode remove the CompositeOp form the name and "unserscorize" the rest. For instance, MultiplyCompositeOp becomes :multiply, and CopyBlackCompositeOp becomes :copy_black.

[Source]

     # File lib/fleximage/operator/base.rb, line 101
101:       def symbol_to_blending_mode(mode)
102:         "Magick::#{mode.to_s.camelize}CompositeOp".constantize
103:       rescue NameError
104:         raise ArgumentError, ":#{mode} is not a valid blending mode."
105:       end

[Validate]