silkscreen 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/silkscreen +37 -3
- data/lib/silkscreen/version.rb +1 -1
- metadata +1 -1
data/bin/silkscreen
CHANGED
@@ -10,26 +10,60 @@ module Silkscreen
|
|
10
10
|
class App < Thor
|
11
11
|
include Thor::Actions
|
12
12
|
|
13
|
-
desc "capture", "
|
13
|
+
desc "capture", "Capture screenshot"
|
14
14
|
method_option :sleep, aliases: '-s', type: :numeric, default: 2, desc: 'Time in seconds to wait after page load before taking the screenshot'
|
15
15
|
method_option :viewport, aliases: '-v', type: :string, default: '1024x768', desc: 'The size of the viewport'
|
16
|
-
method_option :
|
16
|
+
method_option :max_width, aliases: '-w', type: :numeric, desc: 'The max width of the screenshot'
|
17
|
+
method_option :max_height, aliases: '-h', type: :numeric, desc: 'The max height of the screenshot'
|
18
|
+
method_option :full, aliases: '-f', type: :boolean, default: false, desc: 'Capture full page or just the visible area'
|
17
19
|
method_option :output, aliases: '-o', type: :string, desc: 'Save the captured screenshot as a file at this path'
|
18
20
|
def capture(url)
|
19
21
|
driver = Selenium::WebDriver.for :chrome
|
20
|
-
window_width, window_height = options[:viewport].split('x')
|
22
|
+
window_width, window_height = options[:viewport].split('x').map { |x| x.to_i }
|
21
23
|
driver.manage.window.resize_to window_width, window_height
|
22
24
|
driver.navigate.to url
|
23
25
|
sleep options[:sleep]
|
24
26
|
img = driver.screenshot_as :png
|
25
27
|
driver.quit
|
26
28
|
|
29
|
+
if options[:full]
|
30
|
+
if options[:max_width] || options[:max_height]
|
31
|
+
canvas = ChunkyPNG::Image.from_blob img
|
32
|
+
canvas = resize_canvas(canvas, options[:max_width], options[:max_height])
|
33
|
+
img = canvas.to_s
|
34
|
+
end
|
35
|
+
else
|
36
|
+
canvas = ChunkyPNG::Image.from_blob img
|
37
|
+
canvas.crop! 0, 0, window_width, window_height
|
38
|
+
canvas = resize_canvas(canvas, options[:max_width], options[:max_height])
|
39
|
+
img = canvas.to_s
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
|
27
44
|
if options[:output]
|
28
45
|
create_file options[:output], img
|
29
46
|
else
|
30
47
|
print img
|
31
48
|
end
|
32
49
|
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def resize_canvas(canvas, max_width, max_height)
|
54
|
+
max_width ||= canvas.width
|
55
|
+
max_height ||= canvas.height
|
56
|
+
if canvas.width <= max_width && canvas.height <= max_height
|
57
|
+
canvas
|
58
|
+
else
|
59
|
+
width_ratio = max_width.to_f/canvas.width
|
60
|
+
height_ratio = max_height.to_f/canvas.height
|
61
|
+
lower_ratio = [width_ratio, height_ratio].min
|
62
|
+
new_width = (canvas.width * lower_ratio).round
|
63
|
+
new_height = (canvas.height * lower_ratio).round
|
64
|
+
canvas.resize new_width, new_height
|
65
|
+
end
|
66
|
+
end
|
33
67
|
end
|
34
68
|
end
|
35
69
|
|
data/lib/silkscreen/version.rb
CHANGED