【问题标题】:Dragonfly gem - default images?蜻蜓宝石 - 默认图像?
【发布时间】:2011-08-17 16:54:13
【问题描述】:

我正在使用 Dragonfly,并希望有一个默认图像,可以像当前缩略图一样调整大小。

我目前有以下代码,但是当 Dragonfly 使用 fetch_file 方法时,它会尝试处理缩略图,但生成的 URL 是死链接。

if listing.image
  image = listing.image.jpg
else
  image = Dragonfly[:images].fetch_file('/toekomst/images/speech-bubble.png')
end  
image_tag image.jpg.thumb(size).url, :class => "framed"

我在网上找不到太多帮助,因此非常感谢任何提示!谢谢!

【问题讨论】:

标签: ruby-on-rails ruby-on-rails-3 rmagick dragonfly-gem


【解决方案1】:

您需要将配置值 'allow_fetch_file' 设置为 true - 使用 fetch_file 请求服务器默认情况下出于安全考虑已关闭(除此处外,没有特别记录: http://markevans.github.com/dragonfly/Dragonfly/Server.html 但是,如果您这样做,您可能应该再次将“protect_from_dos_attacks”设置为 true,以确保安全:

Dragonfly[:images].configure do |c|
  # ...
  c.allow_fetch_file = true
  c.protect_from_dos_attacks = true
  c.secret = "some secret here..."
end

希望有帮助

【讨论】:

  • 嗨,马克,感谢您的回复。这听起来应该可行,但我没有看到任何变化。 “秘密”应该是具体的吗?我现在只是放了一个随机字符串
  • 秘密应该是只有你知道的任何随机字符串
【解决方案2】:

您可以使用模型访问器设置默认图像:

class Photo
  dragonfly_accessor :image do
    default 'public/images/default.png'
  end
end

查看文档:http://markevans.github.io/dragonfly/models/#default-content

【讨论】:

    【解决方案3】:

    我设法通过首先添加 Mark 提供的配置代码来解决这个问题。

    然后我的日志中出现了这个错误:

    identify: unable to open image `/toekomst/images/speech-bubble.png': No such file or directory @ error/blob.c/OpenBlob/2584.
    identify: unable to open file `/toekomst/images/speech-bubble.png' @ error/png.c/ReadPNGImage/3079.
    [2011-08-19 10:33:51] ERROR Dragonfly::FunctionManager::UnableToHandle: None of the functions registered with #<Dragonfly::Encoder:0x00000100d66d88> were able to deal with the method call encode(#<Dragonfly::TempObject:0x00000104aa2800 pathname=#<Pathname:/toekomst/images/speech-bubble.png> >,:jpg). You may need to register one that can.
    

    由于 ImageMagick 似乎无法使用相对于项目的路径名,因此我必须指定一个绝对路径。像这样:

    img = Dragonfly[:images].fetch_file(File.join(Rails.root, 'public', 'toekomst', 'images', 'speech-bubble.png'))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多