├── .editorconfig ├── .eslintrc ├── .gitattributes ├── .gitignore ├── .travis.yml ├── appveyor.yml ├── example ├── simple.js └── test.jpg ├── index.js ├── license ├── package.json ├── readme.md └── test └── index.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false 13 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "airbnb/base", 3 | "env": { 4 | "mocha": true, 5 | "node": true 6 | }, 7 | "rules": { 8 | "no-var": 0, 9 | "vars-on-top": 0, 10 | "comma-dangle": 0, 11 | "strict": 0 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | example/resized-test.png 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | 3 | os: 4 | - linux 5 | - osx 6 | 7 | before_script: 8 | - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then export DISPLAY=:99.0 ; fi 9 | - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sh -e /etc/init.d/xvfb start ; fi 10 | - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sleep 3 ; fi 11 | 12 | language: node_js 13 | 14 | node_js: 15 | - 'node' 16 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | build: off 2 | 3 | os: unstable 4 | 5 | environment: 6 | nodejs_version: "6" 7 | 8 | install: 9 | - ps: Install-Product node $env:nodejs_version 10 | - npm install 11 | 12 | test_script: 13 | - node --version 14 | - npm --version 15 | - npm test 16 | -------------------------------------------------------------------------------- /example/simple.js: -------------------------------------------------------------------------------- 1 | var electronImageResize = require('../'); 2 | var join = require('path').join; 3 | var { writeFileSync } = require('fs'); 4 | const { app } = require('electron'); 5 | 6 | process.on('uncaughtException', (error) => { 7 | console.error(error); 8 | }); 9 | 10 | app.on('ready', () => { 11 | electronImageResize({ 12 | url: `file://${join(__dirname, 'test.jpg')}`, 13 | width: 40, 14 | height: 40 15 | }).then(img => { 16 | var filename = 'resized-test.png'; 17 | writeFileSync( 18 | join(__dirname, filename), 19 | img.toPng() 20 | ); 21 | }, console.log.bind(console)); 22 | }); 23 | -------------------------------------------------------------------------------- /example/test.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/davej/electron-image-resize/1c0b3a845ecc5851b846f4f56e72e3bf1f250ba4/example/test.jpg -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var electron = require('electron'); 4 | var BrowserWindow = electron.BrowserWindow || electron.remote.BrowserWindow; 5 | var nativeImage = electron.nativeImage; 6 | 7 | module.exports = function electronImageResize(params) { 8 | var opts = params || {}; 9 | return new Promise((resolve, reject) => { 10 | if (typeof opts.url !== 'string') { 11 | reject(new TypeError('Expected option: `url` of type string')); 12 | return; 13 | } 14 | 15 | if (typeof opts.height !== 'number' && typeof opts.width !== 'number') { 16 | reject(new TypeError('Expected option: `height` or `width` of type number')); 17 | return; 18 | } 19 | 20 | if (!(typeof opts.height === 'number' && typeof opts.width === 'number')) { 21 | var imageLocation = opts.url.replace('file://', ''); 22 | var originalSize = nativeImage.createFromPath(imageLocation).getSize(); 23 | 24 | if (typeof opts.height !== 'number') { 25 | opts.height = parseInt(originalSize.height * opts.width / originalSize.width, 10); 26 | } else { 27 | opts.width = parseInt(originalSize.width * opts.height / originalSize.height, 10); 28 | } 29 | } 30 | 31 | if (typeof opts.delay !== 'number') { 32 | // We seem to need a delay otherwise the image isn't captured because it 33 | // hasn't been painted yet. 34 | // Ideally we would want something deterministic like Mozilla's `afterPaint` 35 | // event in Chromium/Electron. 36 | // You may need to increase the delay when dealing with very large images 37 | opts.delay = 500; 38 | } 39 | 40 | var win = new BrowserWindow({ 41 | x: 0, 42 | y: 0, 43 | width: opts.width, 44 | height: opts.height, 45 | show: false, 46 | frame: false, 47 | enableLargerThanScreen: true, 48 | webPreferences: { 49 | nodeIntegration: false 50 | } 51 | }); 52 | 53 | var isRejected = false; 54 | var $reject = err => { 55 | win.close(); 56 | isRejected = true; 57 | return reject(err); 58 | }; 59 | 60 | win.once('closed', () => { 61 | win = null; 62 | }); 63 | 64 | win.loadURL(opts.url); 65 | 66 | win.webContents.once('did-get-response-details', 67 | (event, status, newURL, originalURL, httpResponseCode) => { 68 | if (httpResponseCode !== 200) { 69 | $reject( 70 | new Error( 71 | `Expected: 200. Received: ${httpResponseCode}. Response not ok: ${originalURL}` 72 | ) 73 | ); 74 | } 75 | }); 76 | 77 | win.webContents.once('did-fail-load', (ev, errCode, errDescription, url) => 78 | $reject(new Error(`failed loading: ${url} ${errDescription}`)) 79 | ); 80 | win.webContents.once('did-finish-load', () => { 81 | if (isRejected) return; 82 | win.webContents.insertCSS('img { width: 100%; height: 100%; }'); 83 | setTimeout(() => { 84 | win.capturePage(img => { 85 | if (isRejected) return; 86 | resolve(img); 87 | win.close(); 88 | }); 89 | }, opts.delay); 90 | }); 91 | }); 92 | }; 93 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) DaveJ (twitter.com/DaveJ) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "electron-image-resize", 3 | "version": "1.2.4", 4 | "description": "Resize images using Electron. Supports all image types that Chromium/Electron supports, outputs to png, jpeg, dataUrl or NativeImage.", 5 | "license": "MIT", 6 | "repository": "davej/electron-image-resize", 7 | "author": { 8 | "name": "DaveJ", 9 | "email": "dave@davejeffery.com", 10 | "url": "twitter.com/DaveJ" 11 | }, 12 | "engines": { 13 | "node": ">=4" 14 | }, 15 | "scripts": { 16 | "example": "electron example/simple.js", 17 | "test": "npm run lint && mocha", 18 | "lint": "eslint index.js" 19 | }, 20 | "files": [ 21 | "index.js" 22 | ], 23 | "keywords": [ 24 | "electron", 25 | "screenshot", 26 | "resize", 27 | "image", 28 | "png", 29 | "jpg", 30 | "jpeg", 31 | "chromium" 32 | ], 33 | "dependencies": {}, 34 | "devDependencies": { 35 | "electron": "^1.3.5", 36 | "eslint": "^2.9.0", 37 | "eslint-config-airbnb": "^9.0.1", 38 | "eslint-plugin-import": "^1.10.2", 39 | "mocha": "^3.0.2", 40 | "spectron": "^3.3.0" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | # THIS MODULE IS DEPRECATED 4 | 5 | Electron [1.4.3](https://github.com/electron/electron/releases/tag/v1.4.3)+ now has a [native API for resizing images](http://electron.atom.io/docs/api/native-image/#imageresizeoptions). You should use those methods instead of this module. This module will stay around but I won't be maintaining it. 6 | 7 | --- 8 | 9 | # electron-image-resize 10 | 11 | > Resize images using Electron. Supports all image types that Chromium/Electron supports, outputs to png, jpeg, dataUrl or NativeImage. 12 | 13 | [![Build status: Mac & Linux](https://img.shields.io/travis/davej/electron-image-resize/master.svg?label=Mac%20%26%20Linux)](https://travis-ci.org/davej/electron-image-resize) [![Build status: Windows](https://img.shields.io/appveyor/ci/davej/electron-image-resize/master.svg?label=Windows)](https://ci.appveyor.com/project/davej/electron-image-resize/branch/master) 14 | 15 | ## Install 16 | 17 | ``` 18 | $ npm install --save electron-image-resize 19 | ``` 20 | 21 | 22 | ## Usage 23 | 24 | ```js 25 | const electronImageResize = require('electron-image-resize'); 26 | const { writeFileSync } = require('fs'); 27 | 28 | // Resize the svg image to 40x40 29 | electronImageResize({ 30 | url: 'http://electron.atom.io/images/electron-logo.svg', 31 | width: 40, 32 | height: 40 33 | }).then(img => { 34 | // save it as a png file 35 | writeFileSync('/some/path/electron.png', img.toPng()); 36 | }) 37 | ``` 38 | 39 | 40 | ## API 41 | 42 | ### electronImageResize(options) 43 | 44 | #### options 45 | 46 | ##### url 47 | 48 | Type: `string` 49 | *Required* 50 | 51 | URL of image to resize. For local paths prefix the path with '`file://`'. 52 | 53 | ##### width and height 54 | 55 | Type: `number` 56 | *At Least One Is Required* 57 | 58 | The width in pixels to resize the image to. 59 | The height in pixels to resize the image to. 60 | If only one dimension is provided, the image will be re-sized so that the original height/width ratio is maintained. 61 | 62 | ##### delay 63 | 64 | Type: `boolean` 65 | Default: `500` 66 | 67 | We will wait until the image is loaded but often you will need to also add a delay to give the renderer time to paint the image before capturing it. 68 | 69 | 70 | #### Returns 71 | 72 | Returns a promise which resolves to a [NativeImage](https://github.com/atom/electron/blob/master/docs/api/native-image.md). Use methods like `.toPng()` and `.toJpeg()` to get a buffer which you can use to write to the file system. 73 | 74 | ## License 75 | 76 | MIT © [DaveJ](https://twitter.com/DaveJ) 77 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | const electron = require('electron'); 2 | const Application = require('spectron').Application; 3 | const assert = require('assert'); 4 | const { join } = require('path'); 5 | const { stat, unlinkSync } = require('fs'); 6 | 7 | const delay = time => new Promise(resolve => setTimeout(resolve, time)); 8 | 9 | describe('Simple app', function appLaunch() { 10 | this.timeout(10000); 11 | const exampleDir = join(__dirname, '..', 'example'); 12 | const resizedPng = join(exampleDir, 'resized-test.png'); 13 | 14 | beforeEach(() => { 15 | try { 16 | unlinkSync(resizedPng); 17 | console.log(`Deleted: ${resizedPng}`); 18 | } catch (e) { 19 | // do nothing 20 | } 21 | 22 | 23 | this.app = new Application({ 24 | path: electron, 25 | args: [join(exampleDir, 'simple')] 26 | }); 27 | this.app.start(); 28 | return delay(2000); 29 | }); 30 | 31 | it('outputs a resized image', done => { 32 | delay(4000).then(() => { 33 | stat(resizedPng, (err, info) => { 34 | console.log(info); 35 | if (err) { 36 | assert.fail(err); 37 | return done(); 38 | } 39 | assert.ok(info.size > 1024, `File size was ${info.size}B, should be > 1KB`); 40 | return done(); 41 | }); 42 | }); 43 | }); 44 | }); 45 | --------------------------------------------------------------------------------