flickrcaptionr 0.0.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/flickrcaptionr/app.rb +36 -43
- data/lib/flickrcaptionr/processor.rb +1 -2
- data/lib/flickrcaptionr/version.rb +1 -1
- data/views/index.haml +16 -5
- metadata +1 -1
data/lib/flickrcaptionr/app.rb
CHANGED
@@ -13,37 +13,41 @@ class Flickrcaptionr::App < Sinatra::Base
|
|
13
13
|
get '/' do
|
14
14
|
haml :index
|
15
15
|
end
|
16
|
+
get '/get/:image_url' do
|
17
|
+
handle_image_generation(params)
|
18
|
+
haml :index
|
19
|
+
end
|
20
|
+
get '/get/:image_url/:caption_text' do
|
21
|
+
handle_image_generation(params)
|
22
|
+
haml :index
|
23
|
+
end
|
24
|
+
get '/get/:image_url/:image_width/:image_height' do
|
25
|
+
handle_image_generation(params)
|
26
|
+
haml :index
|
27
|
+
end
|
16
28
|
get '/get/:image_url/:image_width/:image_height/:caption_text' do
|
17
|
-
|
18
|
-
@p ||= Flickrcaptionr::Processor.new
|
19
|
-
# {"image_url"=>"https://secure.flickr.com/photos/must_love_cartoons/7474524298/",
|
20
|
-
# "image_width"=>"300", "image_height"=>"400",
|
21
|
-
# "caption_text"=>"You have angered the developer", "caption_font_size"=>"36", "caption_font_stroke"=>"2"}
|
22
|
-
|
23
|
-
if params['image_url'] and params['image_url'].size > 0
|
24
|
-
caption_opts = {}
|
25
|
-
if params['caption_text'] and params['caption_text'] != ''
|
26
|
-
caption_opts[:font_size] = params['caption_font_size'].to_i if params['caption_font_size'] and params['caption_font_size'].size > 0
|
27
|
-
caption_opts[:font_stroke] = params['caption_font_stroke'].to_i if params['caption_font_stroke'] and params['caption_font_stroke'].size > 0
|
28
|
-
end
|
29
|
-
begin
|
30
|
-
file = @f.fetch(params['image_url'])
|
31
|
-
w = params['image_width'].to_i
|
32
|
-
h = params['image_height'].to_i
|
33
|
-
if w > 0 and h > 0
|
34
|
-
file = @p.resize!(file, w, h)
|
35
|
-
end
|
36
|
-
file = @p.add_text!(file, params['caption_text'], caption_opts) if params['caption_text'] and params['caption_text'] != ''
|
37
|
-
send_file file
|
38
|
-
rescue Exception => e
|
39
|
-
params['error'] = e.message
|
40
|
-
end
|
41
|
-
else
|
42
|
-
params['error'] = "You didn't supply a URL. Not sure what you expect me to do without that."
|
43
|
-
end
|
29
|
+
handle_image_generation(params)
|
44
30
|
haml :index
|
45
31
|
end
|
46
32
|
post '/' do
|
33
|
+
params['redirect'] = 'true'
|
34
|
+
handle_image_generation(params)
|
35
|
+
haml :index
|
36
|
+
end
|
37
|
+
get '/image/:filename' do
|
38
|
+
send_file File.join(Flickrcaptionr::Config.output_path, params['filename'])
|
39
|
+
end
|
40
|
+
get '/favicon.ico' do
|
41
|
+
""
|
42
|
+
end
|
43
|
+
@@public_files.each do |public_file, public_file_type|
|
44
|
+
get "/#{public_file}" do
|
45
|
+
content_type(public_file_type)
|
46
|
+
::File.open(::File.expand_path("../../../pub/#{public_file}", __FILE__)).read
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def handle_image_generation(params)
|
47
51
|
@f ||= Flickrcaptionr::Fetcher.new
|
48
52
|
@p ||= Flickrcaptionr::Processor.new
|
49
53
|
# {"image_url"=>"https://secure.flickr.com/photos/must_love_cartoons/7474524298/",
|
@@ -51,7 +55,6 @@ class Flickrcaptionr::App < Sinatra::Base
|
|
51
55
|
# "caption_text"=>"You have angered the developer", "caption_font_size"=>"36", "caption_font_stroke"=>"2"}
|
52
56
|
|
53
57
|
if params['image_url'] and params['image_url'].size > 0
|
54
|
-
|
55
58
|
caption_opts = {}
|
56
59
|
if params['caption_text'] and params['caption_text'] != ''
|
57
60
|
caption_opts[:font_size] = params['caption_font_size'].to_i if params['caption_font_size'] and params['caption_font_size'].size > 0
|
@@ -65,26 +68,16 @@ class Flickrcaptionr::App < Sinatra::Base
|
|
65
68
|
file = @p.resize!(file, w, h)
|
66
69
|
end
|
67
70
|
file = @p.add_text!(file, params['caption_text'], caption_opts) if params['caption_text'] and params['caption_text'] != ''
|
68
|
-
redirect
|
71
|
+
if params['redirect'] and params['redirect'] == 'true'
|
72
|
+
redirect "/image/#{File.basename(file)}"
|
73
|
+
else
|
74
|
+
send_file file
|
75
|
+
end
|
69
76
|
rescue Exception => e
|
70
77
|
params['error'] = e.message
|
71
78
|
end
|
72
|
-
|
73
79
|
else
|
74
80
|
params['error'] = "You didn't supply a URL. Not sure what you expect me to do without that."
|
75
81
|
end
|
76
|
-
haml :index
|
77
|
-
end
|
78
|
-
get '/image/:filename' do
|
79
|
-
send_file File.join(Flickrcaptionr::Config.output_path, params['filename'])
|
80
|
-
end
|
81
|
-
get '/favicon.ico' do
|
82
|
-
""
|
83
|
-
end
|
84
|
-
@@public_files.each do |public_file, public_file_type|
|
85
|
-
get "/#{public_file}" do
|
86
|
-
content_type(public_file_type)
|
87
|
-
::File.open(::File.expand_path("../../../pub/#{public_file}", __FILE__)).read
|
88
|
-
end
|
89
82
|
end
|
90
83
|
end
|
@@ -3,7 +3,6 @@ require 'dimensions'
|
|
3
3
|
class Flickrcaptionr::Processor
|
4
4
|
def initialize
|
5
5
|
Flickrcaptionr::Processor.has_dependencies?
|
6
|
-
|
7
6
|
end
|
8
7
|
# Resize an image, fitting the space provided as best as possible with a centre-weighted crop
|
9
8
|
def resize!(path, width, height)
|
@@ -29,7 +28,7 @@ class Flickrcaptionr::Processor
|
|
29
28
|
puts "Already added text to this image, not doing it again"
|
30
29
|
else
|
31
30
|
puts "Adding text '#{text}' to #{path}"
|
32
|
-
`convert -background none -fill white -font "#{opts[:font_path] ? opts[:font_path] : (File.join(File.dirname(__FILE__), '..', '..', 'fonts', 'Coda-Heavy.ttf' ))}" -stroke black -strokewidth #{opts[:font_stroke] ? opts[:font_stroke].to_s : 2.to_s} -pointsize #{opts[:font_size] ? opts[:font_size].to_s : 36.to_s} -size #{((Dimensions.width(path)-10).to_s)} -gravity Center caption:'#{text.gsub(/[^A-Za-z0-9 \-"
|
31
|
+
`convert -background none -fill white -font "#{opts[:font_path] ? opts[:font_path] : (File.join(File.dirname(__FILE__), '..', '..', 'fonts', 'Coda-Heavy.ttf' ))}" -stroke black -strokewidth #{opts[:font_stroke] ? opts[:font_stroke].to_s : 2.to_s} -pointsize #{opts[:font_size] ? opts[:font_size].to_s : 36.to_s} -size #{((Dimensions.width(path)-10).to_s)} -gravity Center caption:'#{text.gsub(/[^A-Za-z0-9 \-"\.,\?\!]/,"")}' caption-tmp.png`
|
33
32
|
`composite caption-tmp.png #{path} -compose atop -gravity South #{out_filename}`
|
34
33
|
`rm -rf caption-tmp.png`
|
35
34
|
end
|
data/views/index.haml
CHANGED
@@ -32,11 +32,11 @@
|
|
32
32
|
.control-group
|
33
33
|
%label.control-label Image Width
|
34
34
|
.controls
|
35
|
-
%input{type: 'text', name: 'image_width', value: params[:image_width] ? params[:image_width] :
|
35
|
+
%input{type: 'text', name: 'image_width', value: params[:image_width] ? params[:image_width] : 400}
|
36
36
|
.control-group
|
37
37
|
%label.control-label Image Height
|
38
38
|
.controls
|
39
|
-
%input{type: 'text', name: 'image_height', value: params[:image_height] ? params[:image_height] :
|
39
|
+
%input{type: 'text', name: 'image_height', value: params[:image_height] ? params[:image_height] : 300}
|
40
40
|
%p.help-block
|
41
41
|
We can resize your image to whichever size you desire. The image will be cropped to fit these dimensions if required.
|
42
42
|
%fieldset
|
@@ -60,8 +60,19 @@
|
|
60
60
|
|
61
61
|
%p
|
62
62
|
You can also use the GET API as follows:
|
63
|
-
%
|
64
|
-
\/get/flickr-photo-id-or-encoded-url/width/height/caption-text
|
63
|
+
%pre /get/flickr-photo-id-or-encoded-url/width/height/caption-text
|
65
64
|
%p
|
66
65
|
For instance,
|
67
|
-
%a{href: "/get/6792442709/300/
|
66
|
+
%a{href: "/get/6792442709/400/300/I%20has%20a%20LED"} something like this.
|
67
|
+
%p
|
68
|
+
You can omit values to disable functionality, for instance
|
69
|
+
%code /get/flickr-photo-id-or-encoded-url/width/height
|
70
|
+
will not add a caption,
|
71
|
+
%code /get/flickr-photo-id-or-encoded-url/caption
|
72
|
+
will not resize the image and
|
73
|
+
%code /get/flickr-photo-id-or-encoded-url
|
74
|
+
will just provide the unresized image.
|
75
|
+
%p
|
76
|
+
You can add a
|
77
|
+
%code ?redirect=true
|
78
|
+
flag if you prefer to be redirected to the image path rather than be served the image directly.
|