ruby-on-rails - 如何将可变水印路径传递给回形针水印处理器?

标签 ruby-on-rails paperclip

我有水印、画廊和照片模型。

画廊 belongs_to水印
照片belongs_to画廊

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

有了这个,我得到 undefined local variable or method 'gallery' .如果我像这样尝试 :gallery_watermark_path使用 Paperclip.interpolations 定义它会默默地失败。
:watermark_path => ":gallery_watermark_path"
:gallery_watermark_path不会被 Paperclip.interpolations 处理:

开发日志:
[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

简而言之,如何将水印变量传递给处理器?

最佳答案

您可以对样式使用 proc:

  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"
        }
      }
    },
    [...]

但有时,在实例获取其所有属性之前调用 proc(因此有时 gallery 可能是 nil 因为您可能在 image 之前分配了 gallery_id )。

关于ruby-on-rails - 如何将可变水印路径传递给回形针水印处理器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6414786/

相关文章:

ruby-on-rails-3 - 回形针 + S3 : Migrating existing files from one :path format to another

ruby-on-rails - 回形针:无法附加 XLS(但 DOC 可以)

ruby-on-rails - Rails、Rake 计划任务和 DateTime with config.time_zone on Heroku

java - 如何更新Jruby使用的java版本?

ruby-on-rails - PaperClip 在处理之前检查是否存在

ruby-on-rails - 回形针错误 - NotIdentifiedByImageMagickError 使用亚马逊 S3

ruby-on-rails - rake 数据库 :migrate error - paperclip

ruby-on-rails - 在 Rails 5 的数据透视表中保存关联

ruby-on-rails - 在更新数据库之前,在多个模型的 CRUD 更新操作中运行验证

ruby-on-rails - rails中 `stringify_keys'是什么以及出现这个错误时如何解决