4

I'm using Dragonfly and would like to have a default image that resizes in the same way present thumbnails do.

I currently have the following code, but when Dragonfly uses the fetch_file method, it tries to process a thumbnail but the resulting URL is a dead link.

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"

I can't find much help on line for this, so any hints are most appreciated! Thanks!

4

3 回答 3

6

您需要将配置值 '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

希望有帮助

于 2011-08-18T22:49:32.913 回答
3

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

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

请参阅文档: http: //markevans.github.io/dragonfly/models/#default-content

于 2014-10-29T16:24:42.307 回答
1

我设法通过首先添加 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'))
于 2011-08-19T08:43:32.127 回答