-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdragonfly_helper.rb
45 lines (36 loc) · 1.38 KB
/
dragonfly_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
module DragonflyHelper
# Cache Dragonfly thumbnail generation and produce friendly SEO URLs
def thumbnail_tag(image, size, options={})
if options.has_key?(:path)
sub_dir = options[:path]
options.delete(:path)
else
sub_dir = 'images'
end
path = File.join('/', sub_dir, size, this_image.name)
resource = File.join(Rails.root, 'public', path)
unless File.exists?(resource)
image.thumb(size).to_file(resource)
end
attrs = ''
options.each do |key, value|
if key == :data
value.each do |key, value|
attrs << "data-#{key.to_s}='#{value.to_s}' "
end
else
attrs << "#{key.to_s}='#{value.to_s}' "
end
end
attrs << "alt='#{File.basename(this_image.name, '.*')}'" if options[:alt].blank?
# Optionnal stuff : since resize params can be very tricky (ex: '400x300+50+100'),
# make sure they are in simple 'widthxheight' format to pick up width/height params
# If not, just forget it and leave the browser deal with image dimensions
width, height = ''
if(size.split('x')[0].to_i.to_s == size.split('x')[0] && size.split('x')[1].to_i.to_s == size.split('x')[1])
width = "width='#{size.split('x')[0].to_i}px'"
height = "height='#{size.split('x')[1].to_i}px'"
end
"<img src='#{path}' #{width} #{height} #{attrs} />".html_safe
end
end