Module: Papercrop::Helpers

Defined in:
lib/papercrop/helpers.rb

Instance Method Summary collapse

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

Parameters:

  • attachment (Symbol)

    attachment name

  • opts (Hash) (defaults to: {})


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(attachment, opts = {})
  attachment = attachment.to_sym
  width      = opts[:width] || 100
  height     = (width / self.object.send(:"#{attachment}_aspect")).round 

  if self.object.send(attachment).class == Paperclip::Attachment
    wrapper_options = {
      :id    => "#{attachment}_crop_preview_wrapper",
      :style => "width:#{width}px; height:#{height}px; overflow:hidden"
    }

    preview_image = @template.image_tag(self.object.send(attachment).url, :id => "#{attachment}_crop_preview")

    @template.(:div, preview_image, wrapper_options)
  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

Parameters:

  • attachment (Symbol)

    attachment name

  • opts (Hash) (defaults to: {})


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(attachment, opts = {})
  attachment      = attachment.to_sym
  original_width  = self.object.image_geometry(attachment, :original).width
  original_height = self.object.image_geometry(attachment, :original).height
  box_width       = opts[:width] || original_width

  if self.object.send(attachment).class == Paperclip::Attachment
    box  = self.hidden_field(:"#{attachment}_original_w", :value => original_width)
    box << self.hidden_field(:"#{attachment}_original_h", :value => original_height)
    box << self.hidden_field(:"#{attachment}_box_w",      :value => box_width)

    for attribute in [:crop_x, :crop_y, :crop_w, :crop_h, :aspect] do
      box << self.hidden_field(:"#{attachment}_#{attribute}", :id => "#{attachment}_#{attribute}")
    end

    crop_image = @template.image_tag(self.object.send(attachment).url)

    box << @template.(:div, crop_image, :id => "#{attachment}_cropbox")
  end
end