diff --git a/lib/Page.js b/lib/Page.js index b03049a9b6c..c161798cc7d 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -17,6 +17,7 @@ var fs = require('fs'); var EventEmitter = require('events'); var helpers = require('./helpers'); +var mime = require('mime'); class Page extends EventEmitter { /** @@ -335,6 +336,24 @@ class Page extends EventEmitter { return new Buffer(result.data, 'base64'); } + /** + * @param {string} filePath + * @param {?{x: number, y: number, width: number, height: number}} clipRect + * @return {!Promise} + */ + async saveScreenshot(filePath, clipRect) { + var mimeType = mime.lookup(filePath); + var screenshotType = null; + if (mimeType === 'image/png') + screenshotType = Page.ScreenshotTypes.PNG; + else if (mimeType === 'image/jpeg') + screenshotType = Page.ScreenshotTypes.JPG; + if (!screenshotType) + throw new Error(`Cannot render to file ${fileName} - unsupported mimeType ${mimeType}`); + var buffer = await this.screenshot(screenshotType, clipRect); + fs.writeFileSync(filePath, buffer); + } + /** * @return {!Promise} */ diff --git a/phantomjs/WebPage.js b/phantomjs/WebPage.js index 77e7c9bad6d..3c1069a37ba 100644 --- a/phantomjs/WebPage.js +++ b/phantomjs/WebPage.js @@ -226,14 +226,6 @@ class WebPage { * {string} fileName */ render(fileName) { - var mimeType = mime.lookup(fileName); - var screenshotType = null; - if (mimeType === 'image/png') - screenshotType = ScreenshotTypes.PNG; - else if (mimeType === 'image/jpeg') - screenshotType = ScreenshotTypes.JPG; - if (!screenshotType) - throw new Error(`Cannot render to file ${fileName} - unsupported mimeType ${mimeType}`); var clipRect = null; if (this.clipRect && (this.clipRect.left || this.clipRect.top || this.clipRect.width || this.clipRect.height)) { clipRect = { @@ -243,8 +235,7 @@ class WebPage { height: this.clipRect.height }; } - var imageBuffer = await(this._page.screenshot(screenshotType, clipRect)); - fs.writeFileSync(fileName, imageBuffer); + var imageBuffer = await(this._page.saveScreenshot(fileName, clipRect)); } release() {