Skip to content

Commit 5594778

Browse files
author
Kevin Gisi
committed
Update
1 parent aa99ce8 commit 5594778

File tree

7 files changed

+178
-1
lines changed

7 files changed

+178
-1
lines changed

.gems

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
haml --version 2.2.17
2+
rmagick
3+
open-uri

app.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
require 'rubygems'
22
require 'sinatra'
3+
require 'open-uri'
4+
require 'lib/mutable_image'
5+
require 'haml'
6+
require 'sass/plugin/rack'
7+
use Sass::Plugin::Rack
38

49
get '/' do
5-
"Hello, world!"
10+
haml :index
11+
end
12+
13+
get '/image.png' do
14+
send_file 'tmp/image.png', :disposition => 'inline'
15+
end
16+
17+
post '/' do
18+
@image = MutableImage.new(params[:image_url])
19+
@image.send(params[:task]) if params[:task]
20+
@generated = @image.save_to('tmp/image.png')
21+
haml :index
622
end

lib/mutable_image.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
require 'rubygems'
2+
require 'open-uri'
3+
require 'RMagick'
4+
5+
class MutableImage
6+
7+
def initialize(url)
8+
@image = Magick::Image.from_blob(open(url).read).first
9+
@image.format = 'png'
10+
end
11+
12+
def valid?
13+
!@image.nil?
14+
end
15+
16+
def none
17+
end
18+
19+
def save_to(file)
20+
return nil unless valid?
21+
begin
22+
File.open(file,'w') do |f|
23+
f.write(@image.to_blob)
24+
end
25+
rescue
26+
return nil
27+
end
28+
end
29+
30+
def spread
31+
@image = @image.spread(3.0)
32+
end
33+
34+
def flip_vertical
35+
@image = @image.flip
36+
end
37+
38+
def flip_horizontal
39+
@image = @image.flop
40+
end
41+
42+
def frame
43+
@image = @image.frame
44+
end
45+
46+
def blur
47+
@image = @image.gaussian_blur
48+
end
49+
50+
def wet_floor
51+
images = Magick::ImageList.new
52+
images << @image
53+
images << @image.wet_floor(0.5,2.0)
54+
@image = images.append(true)
55+
end
56+
57+
def polaroid
58+
@image[:caption] = "Great Times at RubyCamp 2010"
59+
@image = @image.polaroid
60+
end
61+
62+
end

public/stylesheets/sass/style.sass

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=pane
2+
float: left
3+
width: 50%
4+
padding: 10px 0
5+
6+
body
7+
background-color: #aaa
8+
9+
#wrapper
10+
background-color: #fff
11+
width: 920px
12+
margin: 0 auto
13+
#header
14+
border-bottom: 1px solid #ccc
15+
#main
16+
#left_pane
17+
+pane
18+
#right_pane
19+
+pane
20+
img
21+
width: 100%
22+
#footer
23+
clear: both
24+
border-top: 1px solid #ccc

public/stylesheets/style.css

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
body {
2+
background-color: #aaa; }
3+
4+
#wrapper {
5+
background-color: #fff;
6+
width: 920px;
7+
margin: 0 auto; }
8+
#wrapper #header {
9+
border-bottom: 1px solid #ccc; }
10+
#wrapper #main #left_pane {
11+
float: left;
12+
width: 50%;
13+
padding: 10px 0; }
14+
#wrapper #main #right_pane {
15+
float: left;
16+
width: 50%;
17+
padding: 10px 0; }
18+
#wrapper #main #right_pane img {
19+
width: 100%; }
20+
#wrapper #footer {
21+
clear: both;
22+
border-top: 1px solid #ccc; }

tmp/image.png

1.99 MB
Loading

views/index.haml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
!!!
2+
%html{:lang=>"en-US"}
3+
%head
4+
%link{:rel=>"stylesheet",:href=>"/stylesheets/style.css"}
5+
%meta{'http-equiv' => 'Content-Type',
6+
:content => 'text/html; charset=utf-8'}
7+
%title
8+
Image Tweak
9+
%body
10+
#wrapper
11+
#header
12+
%h1
13+
Image Tweak
14+
#main
15+
#left_pane
16+
%form{:method=>"post"}
17+
%table
18+
%tr
19+
%td
20+
Image URL:
21+
%td
22+
%input{:type=>"text",:name=>"image_url"}
23+
%tr
24+
%td
25+
Effect
26+
%td
27+
%select{:name=>"task"}
28+
%option{:value=>"none"}
29+
None
30+
%option{:value=>"spread"}
31+
Spread
32+
%option{:value=>"flip_vertical"}
33+
Flip vertically
34+
%option{:value=>"flip_horizontal"}
35+
Flip horizontally
36+
%option{:value=>"frame"}
37+
Frame
38+
%option{:value=>"blur"}
39+
Blur
40+
%option{:value=>"wet_floor"}
41+
Wet floor
42+
%option{:value=>"polaroid"}
43+
Polaroid
44+
%p
45+
%input{:type=>"submit",:value=>"Tweak Image"}
46+
#right_pane
47+
-if @generated
48+
%img{:src=>"/image.png"}
49+
#footer
50+
Copyright &copy; 2010

0 commit comments

Comments
 (0)