Module: Sinatra::Thumbnails

Defined in:
lib/sinatra/thumbnails.rb

Defined Under Namespace

Modules: Helpers, Settings

Class Method Summary collapse

Class Method Details

.convert(src, dest, format) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sinatra/thumbnails.rb', line 49

def self.convert(src, dest, format)
  if (format =~ /(.*)-crop$/) 
    if (im_version <=> [6,6,4]) >= 0
      format = $1 + "^" + " -gravity center -extent " + $1
    else
      format = $1
    end
  end
  FileUtils.mkdir_p(File.dirname(dest))
  command = "#{Sinatra::Thumbnails.settings.convert_executable} -define jpeg:size=400x400 '#{src}' -thumbnail #{format} '#{dest}'"
  # puts "Sinatra::Thumbnails: issuing \"#{command}\""
  run_command(command)
end

.ffmpeg(src, dest, format) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/sinatra/thumbnails.rb', line 63

def self.ffmpeg(src, dest, format)
  puts "making movie thumb on the fly"
  seconds = (src =~ /\.ss([\d]+)/) ? Regexp.last_match(1).to_i : 0 
  command = "#{Sinatra::Thumbnails.settings.ffmpeg_executable} -y -i '#{src}' -an -ss #{seconds} -r 1 -vframes 1 -f mjpeg  '#{dest}'"
  # puts "Sinatra::Thumbnails: issuing \"#{command}\""
  run_command(command)
  convert(dest, dest, format)
end

.im_versionObject



44
45
46
47
# File 'lib/sinatra/thumbnails.rb', line 44

def self.im_version
  `convert --version` =~ /Version: ImageMagick ([\d\.]+)/
  Regexp.last_match(1).split('.').map{|s|s.to_i}
end

.make_thumb(file, format, original_extension) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/sinatra/thumbnails.rb', line 27

def self.make_thumb(file, format, original_extension)
  original_extension ||= "jpg"
  thumbnail_file = File.join(settings.thumbnail_path, "#{format}/#{file}")
  FileUtils.mkdir_p File.dirname(thumbnail_file)
  orig_file = File.join(settings.image_path_prefix, file.gsub(/(.*\.)(.*$)/,"\\1#{original_extension}"))

  unless File.exists?(thumbnail_file) and (File.stat(thumbnail_file).mtime >= File.stat(orig_file).mtime)
    
    if original_extension =~ /mov$/
      ffmpeg(orig_file, thumbnail_file, format)
    else
      convert(orig_file, thumbnail_file, format)
    end
  end
  thumbnail_file
end

.registered(app) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/sinatra/thumbnails.rb', line 95

def self.registered(app)
  app.helpers Helpers

  # gotta make this a regexp, :asset wont match stuff with "/" in it
  app.get /#{settings.thumbnail_path}\/([^\/]+)\/(.*)$/ do
      send_file Thumbnails.make_thumb(params[:captures][1], params[:captures][0], params[:original_extension])
  end
end

.run_command(command) ⇒ Object



72
73
74
75
# File 'lib/sinatra/thumbnails.rb', line 72

def self.run_command(command)
  output = `#{command} 2>&1`
  raise "couldn't run #{command}: #{output}" unless $?.success?
end

.settingsObject



78
79
80
81
82
83
84
85
# File 'lib/sinatra/thumbnails.rb', line 78

def self.settings
  @@settings ||= { :convert_executable  => 'convert'     ,   
                   :ffmpeg_executable   => 'ffmpeg'      ,   
                   :thumbnail_path      => 'public/thumbnails'  ,
                   :image_path_prefix   => 'public'             ,
                   :thumbnail_extension => 'png'         ,   
                   :thumbnail_format    => '100x100'       }.extend(Thumbnails::Settings) 
end