silkscreen 0.1 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/silkscreen +3 -39
- data/lib/silkscreen.rb +38 -1
- data/lib/silkscreen/version.rb +1 -1
- metadata +1 -1
data/bin/silkscreen
CHANGED
@@ -3,8 +3,7 @@ require 'rubygems'
|
|
3
3
|
require 'thor'
|
4
4
|
require 'headless'
|
5
5
|
require 'selenium-webdriver'
|
6
|
-
require '
|
7
|
-
require 'oily_png'
|
6
|
+
require 'silkscreen'
|
8
7
|
|
9
8
|
module Silkscreen
|
10
9
|
class App < Thor
|
@@ -22,53 +21,18 @@ module Silkscreen
|
|
22
21
|
headless.start
|
23
22
|
|
24
23
|
driver = Selenium::WebDriver.for :chrome
|
25
|
-
|
26
|
-
|
27
|
-
driver.navigate.to url
|
28
|
-
sleep options[:sleep]
|
29
|
-
img = driver.screenshot_as :png
|
24
|
+
|
25
|
+
img = Silkscreen.get_screenshot url, options, driver
|
30
26
|
|
31
27
|
driver.quit
|
32
28
|
headless.destroy
|
33
29
|
|
34
|
-
if options[:full]
|
35
|
-
if options[:max_width] || options[:max_height]
|
36
|
-
canvas = ChunkyPNG::Image.from_blob img
|
37
|
-
canvas = resize_canvas(canvas, options[:max_width], options[:max_height])
|
38
|
-
img = canvas.to_s
|
39
|
-
end
|
40
|
-
else
|
41
|
-
canvas = ChunkyPNG::Image.from_blob img
|
42
|
-
canvas.crop! 0, 0, [window_width, canvas.width].min, [window_height, canvas.height].min
|
43
|
-
canvas = resize_canvas(canvas, options[:max_width], options[:max_height])
|
44
|
-
img = canvas.to_s
|
45
|
-
end
|
46
|
-
|
47
|
-
|
48
|
-
|
49
30
|
if options[:output]
|
50
31
|
create_file options[:output], img
|
51
32
|
else
|
52
33
|
print img
|
53
34
|
end
|
54
35
|
end
|
55
|
-
|
56
|
-
private
|
57
|
-
|
58
|
-
def resize_canvas(canvas, max_width, max_height)
|
59
|
-
max_width ||= canvas.width
|
60
|
-
max_height ||= canvas.height
|
61
|
-
if canvas.width <= max_width && canvas.height <= max_height
|
62
|
-
canvas
|
63
|
-
else
|
64
|
-
width_ratio = max_width.to_f/canvas.width
|
65
|
-
height_ratio = max_height.to_f/canvas.height
|
66
|
-
lower_ratio = [width_ratio, height_ratio].min
|
67
|
-
new_width = (canvas.width * lower_ratio).round
|
68
|
-
new_height = (canvas.height * lower_ratio).round
|
69
|
-
canvas.resample_bilinear new_width, new_height
|
70
|
-
end
|
71
|
-
end
|
72
36
|
end
|
73
37
|
end
|
74
38
|
|
data/lib/silkscreen.rb
CHANGED
@@ -1,5 +1,42 @@
|
|
1
1
|
require "silkscreen/version"
|
2
|
+
require 'chunky_png'
|
3
|
+
require 'oily_png'
|
2
4
|
|
3
5
|
module Silkscreen
|
4
|
-
|
6
|
+
def self.get_screenshot(url, options, driver)
|
7
|
+
window_width, window_height = options[:viewport].split('x').map { |x| x.to_i }
|
8
|
+
driver.manage.window.resize_to window_width, window_height
|
9
|
+
driver.navigate.to url
|
10
|
+
sleep options[:sleep]
|
11
|
+
img = driver.screenshot_as :png
|
12
|
+
if options[:full]
|
13
|
+
if options[:max_width] || options[:max_height]
|
14
|
+
canvas = ChunkyPNG::Image.from_blob img
|
15
|
+
canvas = resize_canvas(canvas, options[:max_width], options[:max_height])
|
16
|
+
img = canvas.to_s
|
17
|
+
end
|
18
|
+
else
|
19
|
+
canvas = ChunkyPNG::Image.from_blob img
|
20
|
+
canvas.crop! 0, 0, [window_width, canvas.width].min, [window_height, canvas.height].min
|
21
|
+
canvas = resize_canvas(canvas, options[:max_width], options[:max_height])
|
22
|
+
img = canvas.to_s
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def self.resize_canvas(canvas, max_width, max_height)
|
29
|
+
max_width ||= canvas.width
|
30
|
+
max_height ||= canvas.height
|
31
|
+
if canvas.width <= max_width && canvas.height <= max_height
|
32
|
+
canvas
|
33
|
+
else
|
34
|
+
width_ratio = max_width.to_f/canvas.width
|
35
|
+
height_ratio = max_height.to_f/canvas.height
|
36
|
+
lower_ratio = [width_ratio, height_ratio].min
|
37
|
+
new_width = (canvas.width * lower_ratio).round
|
38
|
+
new_height = (canvas.height * lower_ratio).round
|
39
|
+
canvas.resample_bilinear new_width, new_height
|
40
|
+
end
|
41
|
+
end
|
5
42
|
end
|
data/lib/silkscreen/version.rb
CHANGED