Cropper will process your image to fit an exact width and height - if your model has crop options set - then the image generated will be a crop of the original image scaled to fit the exact width and height.

Required Columns

  • crop_options (text or varchar) - used to store the coordinates of the crop using rails serialization
Methods
Classes and Modules
Module PeelMeAGrape::IsAttachment::Transformer::Cropper::InstanceMethods
Attributes
[RW] height
[RW] overlay
[RW] width
Public Class methods
new(*options)

options can either be: size (equal width and height)

   Cropper.new(50)

width and height

   Cropper.new(50,60)

an array with width and height

   Cropper.new([50,60])

a hash with :width and :height entries

   Cropper.new(:width => 50, :height => 60)
    # File lib/peel_me_a_grape/is_attachment/transformer/cropper.rb, line 18
18:       def initialize(*options)
19:         options = options.last if options.last.is_a? Array
20:         if options.last.is_a? Hash
21:           options = options.last
22:           raise ArgumentError.new("Required keys - :width, :height") unless options.include?(:width) && options.include?(:height)
23:           self.width = options[:width]
24:           self.height = options[:height]
25:           self.overlay = options[:overlay]
26:         else
27:           self.width = options.first.to_i
28:           self.height = options.last.to_i
29:         end
30:       end
Public Instance methods
cropper_restrictions()
   Cropper.new([50,60])

gives cropper restrictions of

   { :min_width => 50, :min_height => 60, :ratio_dim => { :x => 50, :y => 60 } }

These values are very useful for using with - js_image_cropper peelmeagrape.net/projects/js_image_cropper/ - our plugin that helps simplifying usage of www.defusion.org.uk/code/javascript-image-cropper-ui-using-prototype-scriptaculous/ with ruby on rails.

    # File lib/peel_me_a_grape/is_attachment/transformer/cropper.rb, line 37
37:       def cropper_restrictions
38:         {:min_width => self.width, :min_height => self.height, :ratio_dim => {:x => self.width, :y => self.height}}
39:       end
transform_with_mini_magick(engine, img, attachment_object)

same behaviour as transform_with_rmagick - using mini_magick

    # File lib/peel_me_a_grape/is_attachment/transformer/cropper.rb, line 60
60:       def transform_with_mini_magick(engine, img, attachment_object)
61:         crop_options = attachment_object.crop_options
62:         unless attachment_object.crop_options.blank?
63:           img.crop("#{crop_options[:width].to_i}x#{crop_options[:height].to_i}+#{crop_options[:x1].to_i}+#{crop_options[:y1].to_i}")
64:           img.resize("#{self.width}x#{self.height}!")
65:         else
66:           img.thumbnail("#{self.width}x#{self.height}!")
67:         end
68:         img
69:       end
transform_with_rmagick(engine, img, attachment_object)

performs the cropping when using rmagick image processing engine. if crop_options is nil? then the image is resized to fit the dimensions. if crop options is set then they are used to first crop the image then resize it.

    # File lib/peel_me_a_grape/is_attachment/transformer/cropper.rb, line 44
44:       def transform_with_rmagick(engine, img, attachment_object)
45:         crop_options = attachment_object.crop_options
46:         unless attachment_object.crop_options.blank?
47:           img.crop!(crop_options[:x1].to_i, crop_options[:y1].to_i, crop_options[:width].to_i, crop_options[:height].to_i, true)
48:           img.resize!(self.width, self.height)
49:         else
50:           img.thumbnail!(self.width, self.height)
51:         end
52:         unless self.overlay.blank?
53:           overlay = Magick::Image.read(self.overlay).first
54:           img.composite!(overlay, Magick::CenterGravity, Magick::OverCompositeOp )
55:         end
56:         img
57:       end