Skip to content

Instantly share code, notes, and snippets.

@dafrancis
Created April 22, 2011 09:55
Show Gist options
  • Save dafrancis/936371 to your computer and use it in GitHub Desktop.
Save dafrancis/936371 to your computer and use it in GitHub Desktop.
require 'sinatra'
require 'haml'
require 'hpricot'
require 'open-uri'
require 'RMagick'
get '/' do
haml :index
end
post '/' do
redirect "/#{params[:noun]}"
end
get '/:noun' do
@image = params[:noun]
haml :fuck_yeah
end
get '/images/:image' do
appid = 'WHATEVER YOUR YAHOO APP ID IS HERE'# You can get an id at https://developer.apps.yahoo.com/wsregapp/
uri = "http://search.yahooapis.com/ImageSearchService/V1/imageSearch?query=#{params[:image]}&appid=#{appid}"
image = (Hpricot(open(URI.escape(uri)))/"url")[0].inner_text
magick_image image, params[:image]
end
def magick_image(image, text)
img = Magick::Image.read(image).first
img = img.resize_to_fit(400,1000)
my_text = "FUCK YEAH\n#{text.upcase}"
txt = Magick::Draw.new
txt.annotate(img, 0, 0, 3, 18, my_text) do
self.font = 'Arial'
self.pointsize = 46
self.font_weight = Magick::BoldWeight
self.fill = 'white'
self.stroke = 'black'
self.gravity = Magick::SouthGravity
end
img.format = 'jpeg'
content_type 'image/jpeg'
img.to_blob
end
__END__
@@ layout
!!! 5
%html
%head
%meta{:charset=>'utf-8'}
%title= "FUCK YEAH " + (params[:noun]||"NOUNS").upcase
%style
body,input{width:800px;margin:auto;text-transform:uppercase;font-size:64px;font-family:Arial}
img{width:400px;margin-left:200px;margin-right:200px;}
input{width:50%;color:black;}
form{margin-top:50px}
%body
= yield
@@ index
%form{:action=>'/',:method=>'POST'}
%label{:for=>'noun'} FUCK YEAH
%input{:type=>'text',:name=>'noun',:id=>'noun'}
@@ fuck_yeah
%img{:alt=>"",:src=>"/images/#{@image}"}
= haml :index
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment