How do I pass a variable watermark path to a paperclip watermark processor?
Asked Answered
E

2

6

I have watermarks, galleries, and photos models.

Gallery belongs_to Watermark Photo belongs_to Gallery

class Photo < ActiveRecord::Base
  before_save :save_dimensions, :set_orientation
  belongs_to :gallery

  has_attached_file :image,
    :processors => [:watermark],
    :styles => {
      :thumbnail => ["80x80>"],
      :small => {
        :geometry => "200x200>",
        :watermark_path => "#{gallery.watermark.image.path(:small)}",
        :position => "Center"
      },
      :medium => {
        :geometry => "400x400>",
        :watermark_path => "#{gallery.watermark.image.path(:medium)}",
        :position => "Center"
      },
      :large => {
        :geometry => "600x600>",
        :watermark_path => "#{gallery.watermark.image.path(:large)}",
        :position => "Center"
      }
    },
    :path => ":rails_root/public/images/galleries/:gallery_id/:id/:style_:basename.:extension",
    :url => "galleries/:gallery_id/:id/:style_:basename.:extension"
  validates_attachment_presence :image
  validates_attachment_content_type :image, :content_type => ['image/jpeg', 'image/png']
  attr_protected :image_file_name, :image_content_type, :image_file_size

end

With this I get undefined local variable or method 'gallery'. If I try like this with :gallery_watermark_path defined using Paperclip.interpolations it silently fails.

:watermark_path => ":gallery_watermark_path"

The :gallery_watermark_path doesn't get processed by Paperclip.interpolations:

development.log:

[paperclip] identify -format %wx%h '/tmp/stream20110620-30644-1j6i9in.jpg[0]' 2>/dev/null
[paperclip] convert '/tmp/stream20110620-30644-1j6i9in.jpg' '-resize' '80x80>' '-auto-orient' '/tmp/stream20110620-30644-1j6i9in20110620-30644-1mcqvox' 2>/dev/null
[paperclip] identify -format %wx%h '/tmp/stream20110620-30644-1j6i9in.jpg[0]' 2>/dev/null
[paperclip] convert '/tmp/stream20110620-30644-1j6i9in.jpg' '-resize' '200x200>' '-auto-orient' '/tmp/stream20110620-30644-1j6i9in20110620-30644-60zlb' 2>/dev/null
[paperclip] composite '-gravity' 'Center' ':gallery_watermark_path' '/tmp/stream20110620-30644-1j6i9in20110620-30644-60zlb' '/tmp/stream20110620-30644-1j6i9in20110620-30644-60zlb' 2>/dev/null
[paperclip] An error was received while processing: #<Paperclip::PaperclipError: There was an error processing the watermark for stream20110620-30644-1j6i9in>
[paperclip] identify -format %wx%h '/tmp/stream20110620-30644-1j6i9in.jpg[0]' 2>/dev/null
[paperclip] convert '/tmp/stream20110620-30644-1j6i9in.jpg' '-resize' '400x400>' '-auto-orient' '/tmp/stream20110620-30644-1j6i9in20110620-30644-1ronidq' 2>/dev/null
[paperclip] identify -format %wx%h '/tmp/stream20110620-30644-1j6i9in.jpg[0]' 2>/dev/null
[paperclip] convert '/tmp/stream20110620-30644-1j6i9in.jpg' '-resize' '600x600>' '-auto-orient' '/tmp/stream20110620-30644-1j6i9in20110620-30644-1ikfy72' 2>/dev/null

In short, how do I pass the watermark variable to the processor?

Encomiast answered 20/6, 2011 at 17:2 Comment(0)
K
0

You could use a proc for the styles:

  has_attached_file :image,
    :processors => [:watermark],
    :styles => proc { |attachment|
      {
        :thumbnail => ["80x80>"],
        :small => {
          :geometry => "200x200>",
          :watermark_path => attachment.instance.gallery.watermark.image.path(:small),
          :position => "Center"
        },
        :medium => {
          :geometry => "400x400>",
          :watermark_path => attachment.instance.gallery.watermark.image.path(:medium),
          :position => "Center"
        },
        :large => {
          :geometry => "600x600>",
          :watermark_path => attachment.instance.gallery.watermark.image.path(:large),
          :position => "Center"
        }
      }
    },
    [...]

But sometimes, the proc is called before the instance get all its attributes (so sometimes gallery may be nil because you may have assigned image before gallery_id).

Kaitlin answered 20/8, 2011 at 8:34 Comment(1)
Confirmed, the attributes like (gallery_id), are not assigned so attachment.instance.gallery comes up as nil.Durance
A
0

The watermark_path needs to be a Pathname, try to use Rails.root.join like this:

has_attached_file :image,
:processors => [:watermark],
:styles => {
  :thumbnail => ["80x80>"],
  :small => {
    :geometry => "200x200>",
    :watermark_path => Rails.root.join("#{gallery.watermark.image.path(:small)}")
    :position => "Center"
  },
  :medium => {
    :geometry => "400x400>",
    :watermark_path => Rails.root.join("#{gallery.watermark.image.path(:medium)}")
    :position => "Center"
  },
  :large => {
    :geometry => "600x600>",
    :watermark_path => Rails.root.join("#{gallery.watermark.image.path(:large)}"),
    :position => "Center"
  }
}
[...]

I hope that helps you,

cheers.

Aurelie answered 22/11, 2015 at 5:20 Comment(1)
Just in case anyone is wondering, you have to write your own watermark processor for Paperclip. It doesn't come bundled or anything.Statutable

© 2022 - 2024 — McMap. All rights reserved.