Module: Papercrop::Helpers
- Defined in:
- lib/papercrop/helpers.rb
Instance Method Summary collapse
-
#crop_preview(attachment, opts = {}) ⇒ Object
Form helper to render the cropping preview box of an attachment.
-
#cropbox(attachment, opts = {}) ⇒ Object
Form helper to render the main cropping box of an attachment.
Instance Method Details
#crop_preview(attachment, opts = {}) ⇒ Object
Form helper to render the cropping preview box of an attachment. Box width can be handled by setting the :width option. Width is 100 by default. Height is calculated by the aspect ratio.
crop_preview :avatar
crop_preview :avatar, :width => 150
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/papercrop/helpers.rb', line 13 def crop_preview(, opts = {}) = .to_sym width = opts[:width] || 100 height = (width / self.object.send(:"#{}_aspect")).round if self.object.send().class == Paperclip::Attachment = { :id => "#{}_crop_preview_wrapper", :style => "width:#{width}px; height:#{height}px; overflow:hidden" } preview_image = @template.image_tag(self.object.send().url, :id => "#{}_crop_preview") @template.content_tag(:div, preview_image, ) end end |
#cropbox(attachment, opts = {}) ⇒ Object
Form helper to render the main cropping box of an attachment. Loads the original image. Initially the cropbox has no limits on dimensions, showing the image at full size. You can restrict it by setting the :width option to the width you want.
cropbox :avatar, :width => 650
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/papercrop/helpers.rb', line 39 def cropbox(, opts = {}) = .to_sym original_width = self.object.image_geometry(, :original).width original_height = self.object.image_geometry(, :original).height box_width = opts[:width] || original_width if self.object.send().class == Paperclip::Attachment box = self.hidden_field(:"#{}_original_w", :value => original_width) box << self.hidden_field(:"#{}_original_h", :value => original_height) box << self.hidden_field(:"#{}_box_w", :value => box_width) for attribute in [:crop_x, :crop_y, :crop_w, :crop_h, :aspect] do box << self.hidden_field(:"#{}_#{attribute}", :id => "#{}_#{attribute}") end crop_image = @template.image_tag(self.object.send().url) box << @template.content_tag(:div, crop_image, :id => "#{}_cropbox") end end |