thumbshooter 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of thumbshooter might be problematic. Click here for more details.
- data/README.md +49 -6
- data/lib/thumbshooter.rb +61 -22
- metadata +2 -2
data/README.md
CHANGED
@@ -16,21 +16,64 @@ x server by doing "apt-get install xvfb" and enabling it:
|
|
16
16
|
|
17
17
|
Thumbshooter.use_xvfb = true
|
18
18
|
|
19
|
-
|
19
|
+
Usage
|
20
20
|
=======
|
21
21
|
|
22
|
+
#Setup options:
|
22
23
|
shooter = Thumbshooter.new(
|
23
24
|
:screen => '800x600',
|
24
|
-
:resize => '
|
25
|
+
:resize => '600x450',
|
26
|
+
:crop => '200x150'
|
25
27
|
)
|
26
|
-
|
27
|
-
#
|
28
|
+
|
29
|
+
#Generate thumbnail:
|
28
30
|
img = shooter.create('http://github.com/')
|
29
|
-
|
30
|
-
#
|
31
|
+
|
32
|
+
#Write thumbnail to file:
|
31
33
|
File.open('thumbshot.png', 'w') {|f| f.write(img) }
|
32
34
|
|
33
35
|
|
36
|
+
**Options for Thumbshooter class:**
|
37
|
+
|
38
|
+
**screen**
|
39
|
+
:screen => '<width>x<height>'
|
40
|
+
|
41
|
+
example:
|
42
|
+
:screen => '800x600'
|
43
|
+
|
44
|
+
**resize**
|
45
|
+
:resize => '<width>x<height><scaling_option>'
|
46
|
+
|
47
|
+
scaling_option [optional]:
|
48
|
+
% - image will be resized proportionaly by percentage
|
49
|
+
# - image will be resized proportionaly by width
|
50
|
+
example:
|
51
|
+
:resize => '200x150'
|
52
|
+
:resize => '200x150#'
|
53
|
+
:resize => '80x50%'
|
54
|
+
|
55
|
+
**crop**
|
56
|
+
:crop => '<width>x<height><scaling_option>'
|
57
|
+
|
58
|
+
scaling_option [optional]:
|
59
|
+
% - image will be cropped proportionaly by percentage
|
60
|
+
example:
|
61
|
+
:crop => '200x150'
|
62
|
+
:crop => '80x50%'
|
63
|
+
|
64
|
+
important:
|
65
|
+
size of croped area is based on effect of previous process:
|
66
|
+
:screen if :resize is not set
|
67
|
+
:resize if :resize is set
|
68
|
+
croped area is always gravity center of image
|
69
|
+
|
70
|
+
TO-DO
|
71
|
+
=======
|
72
|
+
|
73
|
+
Croping and resizing image should have more options, similar to "thoughtbot's Paperclip"(http://github.com/thoughtbot/paperclip) .
|
74
|
+
|
75
|
+
License
|
76
|
+
=======
|
34
77
|
|
35
78
|
Copyright (c) 2009 Julian Kornberger | Digineo GmbH Germany
|
36
79
|
released under the GNU license
|
data/lib/thumbshooter.rb
CHANGED
@@ -2,19 +2,19 @@
|
|
2
2
|
# Binding for webkit2png
|
3
3
|
#
|
4
4
|
class Thumbshooter
|
5
|
-
|
5
|
+
|
6
6
|
# use X window virtual framebuffer?
|
7
7
|
def self.use_xvfb=(value)
|
8
8
|
@use_xvfb = value
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
# use X window virtual framebuffer?
|
12
12
|
def self.use_xvfb
|
13
13
|
@use_xvfb
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
WEBKIT2PNG = File.dirname(__FILE__) + '/webkit2png.rb'
|
17
|
-
|
17
|
+
|
18
18
|
#
|
19
19
|
# screen: dimension of the view part (w * h) i.e. 800x800
|
20
20
|
# resize: resize thumbshot (w * h) i.e. 160x160
|
@@ -22,6 +22,7 @@ class Thumbshooter
|
|
22
22
|
# timeout: timeout for the page to load
|
23
23
|
def initialize(options={})
|
24
24
|
@args = ''
|
25
|
+
@screen = '1024x768'
|
25
26
|
for key,value in options
|
26
27
|
next if value.nil?
|
27
28
|
case key
|
@@ -33,70 +34,108 @@ class Thumbshooter
|
|
33
34
|
when :timeout
|
34
35
|
@args << " --timeout=#{value}"
|
35
36
|
when :resize
|
36
|
-
raise ArgumentError, "invalid value for #{key}: #{value}" unless value =~ /^\d+x\d
|
37
|
+
raise ArgumentError, "invalid value for #{key}: #{value}" unless value =~ /^\d+x\d+(\%|#|)$/
|
37
38
|
@resize = value
|
39
|
+
when :crop
|
40
|
+
raise ArgumentError, "invalid value for #{key}: #{value}" unless value =~ /^\d+x\d+(\%|)$/
|
41
|
+
@crop = value
|
38
42
|
else
|
39
43
|
raise ArgumentError, "unknown option: #{key}"
|
40
44
|
end
|
41
45
|
end
|
42
46
|
end
|
43
|
-
|
47
|
+
|
44
48
|
# creates a thumbshot
|
45
49
|
# returns it if no output-path given
|
46
50
|
def create(url, output=nil)
|
47
51
|
args = @args
|
48
52
|
args << "--output=#{output}" if output
|
49
|
-
|
53
|
+
|
50
54
|
# execute webkit2png-script and save stdout
|
51
55
|
command = ''
|
52
56
|
if self.class.use_xvfb
|
53
57
|
# calculate screen size
|
54
|
-
screen = @screen
|
58
|
+
screen = @screen.split('x').collect{|i|i.to_i+100}.join("x")
|
55
59
|
# add xvfb wrapper
|
56
|
-
command << "xvfb-run -
|
60
|
+
command << "xvfb-run -a --server-args='-screen 0, #{screen}x24' "
|
57
61
|
end
|
58
|
-
|
62
|
+
|
59
63
|
command << "#{WEBKIT2PNG} '#{url}' #{args}"
|
60
|
-
|
64
|
+
|
61
65
|
img = `#{command} 2>&1`
|
62
66
|
status = $?.to_i
|
63
67
|
pos = img.index("\211PNG")
|
64
|
-
|
68
|
+
|
65
69
|
if status != 0 || !pos
|
66
70
|
raise "#{WEBKIT2PNG} failed with status #{status}: #{img}"
|
67
71
|
end
|
68
|
-
|
72
|
+
|
69
73
|
# strip beginning rubish
|
70
74
|
img = img[pos..-1]
|
71
|
-
|
75
|
+
|
72
76
|
if @resize
|
73
|
-
width,height = @resize
|
77
|
+
width,height = geometry(@resize)
|
74
78
|
img = resize(img,width,height)
|
75
79
|
end
|
76
|
-
|
80
|
+
|
81
|
+
if @crop
|
82
|
+
width,height = geometry(@crop)
|
83
|
+
img = crop(img,width,height)
|
84
|
+
end
|
85
|
+
|
77
86
|
img
|
78
87
|
end
|
79
|
-
|
88
|
+
|
80
89
|
# creates a thumb from direct html using a temporary file
|
81
90
|
def create_by_html(html)
|
82
91
|
tmp_file = Tempfile.new('thumbshot.html')
|
83
92
|
tmp_file.write(html)
|
84
93
|
tmp_file.close
|
85
|
-
|
94
|
+
|
86
95
|
begin
|
87
96
|
create(tmp_file.path)
|
88
97
|
ensure
|
89
98
|
tmp_file.close!
|
90
99
|
end
|
91
100
|
end
|
92
|
-
|
101
|
+
|
93
102
|
protected
|
94
|
-
|
103
|
+
|
95
104
|
# resizes the image using RMagick
|
96
105
|
def resize(image_data, width, height)
|
97
106
|
img = Magick::Image.from_blob(image_data)[0]
|
98
|
-
|
107
|
+
@resize=[width, height]
|
108
|
+
img.resize!(width,height)
|
99
109
|
img.to_blob
|
100
110
|
end
|
101
|
-
|
111
|
+
|
112
|
+
# crop the image
|
113
|
+
|
114
|
+
def crop(image_data, width, height)
|
115
|
+
img = Magick::Image.from_blob(image_data)[0]
|
116
|
+
img.crop!(Magick::CenterGravity, width,height)
|
117
|
+
img.to_blob
|
118
|
+
end
|
119
|
+
|
120
|
+
# resolve dimension
|
121
|
+
# # - to scale proportionally to width
|
122
|
+
# % - to scale in percent
|
123
|
+
# in future:
|
124
|
+
# # - to scale by dimensions
|
125
|
+
# > - to scale by higher
|
126
|
+
# < - to scale by lower
|
127
|
+
|
128
|
+
def geometry(size)
|
129
|
+
width,height,ratio = size.scan(/\d+|\%|\#$/)
|
130
|
+
orginal_width, orginal_height = (@resize.is_a?(Array) ? @resize : @screen.split('x')).map(&:to_f)
|
131
|
+
case ratio
|
132
|
+
when '%'
|
133
|
+
width, height = [orginal_width * (width.to_f/100), orginal_height * (height.to_f/100)]
|
134
|
+
when '#'
|
135
|
+
aspect = orginal_width / orginal_height
|
136
|
+
height = orginal_height / aspect
|
137
|
+
end
|
138
|
+
[width.to_i, height.to_i]
|
139
|
+
end
|
140
|
+
|
102
141
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: thumbshooter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Julian Kornberger
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2010-03-05 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|