Class: JCropper::CroppedImage

Inherits:
Object
  • Object
show all
Defined in:
lib/jcropper/cropped_image.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, options) ⇒ CroppedImage

Returns a new instance of CroppedImage.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/jcropper/cropped_image.rb', line 5

def initialize(object, options)
  @object = object
  @options = options
  @attachment_name = object.class.jcropper_defs[:attachment]
  @style_name = object.jcropper_defs[:style]
  @attachment = object.send(attachment_name)  
  @coord_names = {}
  %w(x y w h).each{|v| @coord_names[v.to_sym] = JCropper.jattr(attachment_name, style_name, v)}

  @starting_crop = @coord_names.inject({}) {|h,pair| h.merge({ pair[0] => object.send("#{pair[1].to_s}_was") }) }
end

Instance Attribute Details

#attachmentObject (readonly)

Returns the value of attribute attachment.



3
4
5
# File 'lib/jcropper/cropped_image.rb', line 3

def attachment
  @attachment
end

#attachment_nameObject (readonly)

Returns the value of attribute attachment_name.



3
4
5
# File 'lib/jcropper/cropped_image.rb', line 3

def attachment_name
  @attachment_name
end

#coord_namesObject (readonly)

Returns the value of attribute coord_names.



3
4
5
# File 'lib/jcropper/cropped_image.rb', line 3

def coord_names
  @coord_names
end

#cropped_geometryObject (readonly)

Returns the value of attribute cropped_geometry.



3
4
5
# File 'lib/jcropper/cropped_image.rb', line 3

def cropped_geometry
  @cropped_geometry
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/jcropper/cropped_image.rb', line 3

def options
  @options
end

#original_geometryObject (readonly)

CRZ these two might get out of date…



18
19
20
# File 'lib/jcropper/cropped_image.rb', line 18

def original_geometry
  @original_geometry
end

#starting_cropObject (readonly)

Returns the value of attribute starting_crop.



3
4
5
# File 'lib/jcropper/cropped_image.rb', line 3

def starting_crop
  @starting_crop
end

#style_nameObject (readonly)

Returns the value of attribute style_name.



3
4
5
# File 'lib/jcropper/cropped_image.rb', line 3

def style_name
  @style_name
end

Instance Method Details

#max_cropObject



30
31
32
33
34
35
36
# File 'lib/jcropper/cropped_image.rb', line 30

def max_crop
  if options[:maintain_aspect_ratio]
    north_center_gravity_max_crop
  else
    [0, 0, original_geometry.width, original_geometry.height]
  end
end

#north_center_gravity_max_cropObject



38
39
40
41
42
# File 'lib/jcropper/cropped_image.rb', line 38

def north_center_gravity_max_crop
  scale = JCropper.find_bounding_scale([original_geometry.width, original_geometry.height], [target_geometry.width, target_geometry.height])
  final_size = {:width => scale*target_geometry.width, :height => scale*target_geometry.height}
  [(original_geometry.width - final_size[:width]) / 2, 0, final_size[:width], final_size[:height]].map &:to_i
end

#target_geometryObject



26
27
28
# File 'lib/jcropper/cropped_image.rb', line 26

def target_geometry
  @target_geometry ||= Paperclip::Geometry.parse(@object.send(attachment_name).styles[style_name.to_sym][:geometry])
end