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!

c69
  • 19,951
  • 7
  • 52
  • 82
Blake Simpson
  • 1,078
  • 6
  • 10
  • It seems there is a default option now: http://markevans.github.io/dragonfly/models/#default-content – Chloe May 11 '14 at 21:17

3 Answers3

6

you need to set the config value 'allow_fetch_file' to true - requesting over the server using fetch_file is turned off by default for security (this isn't documented particularly except for here: http://markevans.github.com/dragonfly/Dragonfly/Server.html If you do this, however, you should probably turn on 'protect_from_dos_attacks' to true, again for security:

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

Hope that helps

Mark Evans
  • 311
  • 1
  • 2
  • Hi Mark, thanks for the reply. This sounds like it should work, but I am seeing no change. Should the the "secret" be something specific? I've just put a random string for now – Blake Simpson Aug 19 '11 at 08:18
  • 2
    the secret should just be any random string known only to you – Mark Evans Aug 19 '11 at 09:31
3

You can set a default image with your model accessor:

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

See the docs: http://markevans.github.io/dragonfly/models/#default-content

rizidoro
  • 13,073
  • 18
  • 59
  • 86
1

I managed to get this fixed by first adding the config code provided by Mark.

I was then getting this error in my logs:

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.

Since ImageMagick can't seem to use a path name relative to the project, I had to assign an absolute path. Like this:

img = Dragonfly[:images].fetch_file(File.join(Rails.root, 'public', 'toekomst', 'images', 'speech-bubble.png'))
Blake Simpson
  • 1,078
  • 6
  • 10