Skip to content

Instantly share code, notes, and snippets.

@gaqzi
Created October 14, 2010 06:00
Show Gist options
  • Save gaqzi/625658 to your computer and use it in GitHub Desktop.
Save gaqzi/625658 to your computer and use it in GitHub Desktop.
module ImageHelper
def image(name, alt, link = '')
tmp = File.join(Dir.getwd, 'content')
possible_paths = [
File.join(tmp, @page.directory, 'images', name),
File.join(tmp, 'images', name)
]
file = possible_paths.find {|path| File.exists? path }
file
thumbnail = generate_thumbnail file
w, h = image_size(thumbnail ? thumbnail : file)
output = sprintf('<div class="image" style="width: %dpx;">!', w)
output += (thumbnail ? thumbnail : file).sub(/^(.+?content)/, '')
output += "(#{alt})"
output += '!'
output += ":#{link}" unless link.empty?
if thumbnail
output += "<div>"
output += "<div class=\"enlarge\">!/images/enlarge.png!:#{file.sub(/^(.+?content)/, '')}</div>"
output += "#{alt}</div>"
end
output += '</div>'
output
end
private
def generate_thumbnail(file)
w, h = image_size file
aspect = if w > 500
'500'
elsif h > 500
'x500'
end
if aspect
filename = File.basename(file)
dirname = File.dirname(file)
thumb_file = File.join(File.dirname(file), "thumb_#{File.basename(file)}")
unless File.exists? thumb_file
`convert -resize "#{aspect}" "#{file}" "#{thumb_file}"`
unless $?.exitstatus == 0
STDERR.puts "EO EO Error creating thumbnail!!"
return nil
end
end
return thumb_file
end
return false
end
def image_size(file)
`/usr/bin/env identify -format "%w %h" "#{file}"`.split(' ').map{|x| x.to_i}
end
end
Webby::Helpers.register(ImageHelper)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment