Skip to content

Commit b2213c4

Browse files
author
itsvetkoff
committed
Added basic image parsing and viewport size settings
1 parent 189e596 commit b2213c4

File tree

4 files changed

+92
-9
lines changed

4 files changed

+92
-9
lines changed

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,30 @@ PhearJS accepts the following parameters:
8181
- **cache_namespace**=<*string*\>
8282
A namespace to use on the cache. Can be useful for multi-client settings.
8383
Default: *global-*.
84-
84+
85+
- **viewport_width**=<*number*\>
86+
Width in pixels. Default: 960
87+
88+
- **viewport_height**=<*number*\>
89+
height in pixels. Default: 540
90+
91+
- **as_image**=[*false*|*true*]
92+
Include `rendered` field to the response with the path to saved screenshot of the page.
93+
Format, quality, path to save the screenshot can be set in `as_image_config`.
94+
95+
```
96+
"as_image_config": {
97+
"format": "PNG", // PNG and JPEG are recommended among others.
98+
"quality": 75, // details: http://phantomjs.org/api/webpage/method/render.html
99+
"path": "screenshots/"
100+
}
101+
```
102+
103+
Images are saved by phantomJS on day-by-day basis with some unique names as: `screenshots/2016-07-31/01:05:25.116-c5fsf.PNG`
104+
105+
Image rendering is not perfect. Just try `http://localhost:8100/?fetch_url=http://phantomjs.org&as_image=true`
106+
to see possible defects. Default: *false*.
107+
85108
### Status page
86109

87110
When PhearJS is running you can find a status page at `http://localhost:8100/status`. It

config/config.json

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,15 @@
1919
"user_agent": "PhearBot/0.4.1 - http://phear.io",
2020
"max_connections": 10,
2121
"get_requests": false,
22-
"get_cookies": false
22+
"get_cookies": false,
23+
"viewport_width": 960,
24+
"viewport_height": 540,
25+
"as_image": false,
26+
"as_image_config": {
27+
"format": "PNG",
28+
"quality": 75,
29+
"path": "screenshots/"
30+
}
2331
}
2432
},
2533
"production": {
@@ -47,7 +55,15 @@
4755
"max_connections": 10,
4856
"allowed_clients": ["127.0.0.1"],
4957
"get_requests": false,
50-
"get_cookies": false
58+
"get_cookies": false,
59+
"viewport_width": 960,
60+
"viewport_height": 540,
61+
"as_image": false,
62+
"as_image_config": {
63+
"format": "PNG",
64+
"quality": 75,
65+
"path": "screenshots/"
66+
}
5167
}
5268
}
5369
}

lib/worker.js

Lines changed: 22 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/worker.coffee

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ run_server = ->
5252
# Check whether cookies should be included in JSON
5353
get_cookies = request_url.query?.get_cookies or config.get_cookies
5454

55+
# Check for rendering images
56+
as_image = request_url.query?.as_image or config.as_image
57+
as_image_config = config.as_image_config
58+
59+
# Check width and heigth
60+
viewport_width = request_url.query?.viewport_width or config.viewport_width
61+
viewport_height = request_url.query?.viewport_height or config.viewport_height
62+
5563
# Parse optional headers
5664
request_headers = {}
5765
if request_url.query.headers?
@@ -65,7 +73,7 @@ run_server = ->
6573

6674
try
6775
#Get the page
68-
fetch_url escaped_fetch_url, response, this_inst, parse_delay, request_headers, get_requests, get_cookies
76+
fetch_url escaped_fetch_url, response, this_inst, parse_delay, request_headers, get_requests, get_cookies, as_image, as_image_config, viewport_width, viewport_height
6977
catch err
7078
response.statusCode = 500
7179
return close_response(this_inst, "Error on fetching.", response)
@@ -74,7 +82,7 @@ run_server = ->
7482
logger.info "worker", "Running PhantomJS worker." if service
7583

7684
# Fetch and parse a page
77-
fetch_url = (url, response, this_inst, parse_delay, request_headers, get_requests, get_cookies) ->
85+
fetch_url = (url, response, this_inst, parse_delay, request_headers, get_requests, get_cookies, as_image, as_image_config, viewport_width, viewport_height) ->
7886
final_url = url # store a final URL for redirects
7987
headers = {} # store response headers
8088
requests = [] # store requests made
@@ -141,6 +149,11 @@ fetch_url = (url, response, this_inst, parse_delay, request_headers, get_request
141149
# Create an instance of PhantomJS's webpage (the actual fetching and parsing happens here)
142150
page_inst.open url, (status) ->
143151

152+
page_inst.viewportSize = {
153+
width: viewport_width,
154+
height: viewport_height
155+
}
156+
144157
# Prevent double execution
145158
if done then return true else done = true
146159

@@ -177,6 +190,18 @@ fetch_url = (url, response, this_inst, parse_delay, request_headers, get_request
177190

178191
return
179192

193+
if as_image
194+
iso_date = new Date().toISOString()
195+
path_to_image = "
196+
#{as_image_config.path}#{iso_date.substr(0, 10)}/#{iso_date.substr(11, 12)}
197+
-
198+
#{Math.random().toString(36).substring(2, 7)}.#{as_image_config.format}
199+
"
200+
page_inst.render(path_to_image, {
201+
format: as_image_config.format,
202+
quality: as_image_config.quality
203+
})
204+
180205
response.statusCode = 200
181206
response.write JSON.stringify(
182207
success: true
@@ -188,6 +213,7 @@ fetch_url = (url, response, this_inst, parse_delay, request_headers, get_request
188213
cookies: cookie_inst.cookies if get_cookies in ["true", "1"]
189214
had_js_errors: had_js_errors
190215
content: strip_scripts(page_inst.content)
216+
rendered: path_to_image
191217
)
192218
close_response this_inst, status, response
193219
page_inst.close()

0 commit comments

Comments
 (0)