Class: Flickrcaptionr::App

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/flickrcaptionr/app.rb

Constant Summary collapse

@@public_files =
{
  "bootstrap.min.css" => "text/css"
}

Instance Method Summary collapse

Instance Method Details

#handle_image_generation(params) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/flickrcaptionr/app.rb', line 50

def handle_image_generation(params)
  @f ||= Flickrcaptionr::Fetcher.new
  @p ||= Flickrcaptionr::Processor.new
  # {"image_url"=>"https://secure.flickr.com/photos/must_love_cartoons/7474524298/",
  #  "image_width"=>"300", "image_height"=>"400", 
  #  "caption_text"=>"You have angered the developer", "caption_font_size"=>"36", "caption_font_stroke"=>"2"}

  if params['image_url'] and params['image_url'].size > 0
    caption_opts = {}
    if params['caption_text'] and params['caption_text'] != ''
      caption_opts[:font_size] = params['caption_font_size'].to_i if params['caption_font_size'] and params['caption_font_size'].size > 0
      caption_opts[:font_stroke] = params['caption_font_stroke'].to_i if params['caption_font_stroke'] and params['caption_font_stroke'].size > 0
    end
    begin
      file = @f.fetch(params['image_url'])
      w = params['image_width'].to_i
      h = params['image_height'].to_i
      if w > 0 and h > 0
        file = @p.resize!(file, w, h)
      end
      file = @p.add_text!(file, params['caption_text'], caption_opts) if params['caption_text'] and params['caption_text'] != ''
      if params['redirect'] and params['redirect'] == 'true'
        redirect "/image/#{File.basename(file)}"
      else
        send_file file
      end
    rescue Exception => e
      params['error'] = e.message
    end
  else
    params['error'] = "You didn't supply a URL. Not sure what you expect me to do without that."
  end
end