├── .gitignore ├── README.md ├── example.js ├── image.png ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | package-lock.json 3 | .DS_Store 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # resize-png-buffer 2 | 3 | Resize a PNG buffer into square images of different sizes. 4 | 5 | ## Usage 6 | 7 | ``` javascript 8 | var resizePngBuffer = require('resize-png-buffer') 9 | var fs = require('fs') 10 | 11 | var resize = resizePngBuffer(fs.readFileSync('image.png')) 12 | 13 | resize([96, 96], function (err, buffer) { 14 | if (err) return next(err) 15 | fs.writeFile('image-96x96.png', buffer, next) 16 | }) 17 | ``` 18 | 19 | ### `resize = resizePngBuffer(buffer)` 20 | 21 | Returns a `resize` function, where `buffer` is the contents of a PNG file 22 | as a Node.js [Buffer](https://nodejs.org/api/buffer.html). 23 | 24 | ### `resize(shape, done(err, buffer))` 25 | 26 | Resizes the image using `shape`, an array with `[width, height]` for the output 27 | image. Calls `done(err, buffer)` when complete, where `buffer` is the contents 28 | of the output PNG file as a Node.js [Buffer](https://nodejs.org/api/buffer.html). 29 | 30 | ## License 31 | 32 | MIT -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var resizePngBuffer = require('./') 2 | var map = require('map-limit') 3 | var fs = require('fs') 4 | 5 | var resize = resizePngBuffer(fs.readFileSync('image.png')) 6 | 7 | map([ 8 | 96, 128, 196, 72, 57, 64, 32, 48, 9 | 60, 76, 114, 120, 144, 152, 180, 192, 512 10 | ], 10, function (size, next) { 11 | resize([size, size], function (err, buffer) { 12 | if (err) return next(err) 13 | fs.writeFile(`modified-${size}x${size}.png`, buffer, next) 14 | }) 15 | }, function (err) { 16 | if (err) throw err 17 | console.log('done! ✨') 18 | }) 19 | -------------------------------------------------------------------------------- /image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yoshuawuyts/resize-png-buffer/8042aee83e9d8aa515dedac262ea148b14c3e796/image.png -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var resample = require('ndarray-resample') 2 | var savePixels = require('save-pixels') 3 | var getPixels = require('get-pixels') 4 | var ndarray = require('ndarray') 5 | var bl = require('bl') 6 | 7 | module.exports = resizePngBuffer 8 | 9 | function resizePngBuffer (image) { 10 | var pixels = null 11 | var queue = [] 12 | 13 | getPixels(image, 'image/png', function (err, _pixels) { 14 | if (err) return done(err) 15 | 16 | pixels = _pixels 17 | for (var i = 0; i < queue.length; i++) { 18 | handle(queue[i][0], queue[i][1]) 19 | } 20 | 21 | queue = null 22 | }) 23 | 24 | return handle 25 | 26 | function handle (shape, done) { 27 | if (shape[0] !== shape[1]) return process.nextTick(done.bind(null, new Error( 28 | '[resize-png-buffer] We currently only support square output images. Sorry! PRs welcome :D' 29 | ))) 30 | 31 | if (!pixels) return queue.push([shape, done]) 32 | 33 | var cropped = pixels 34 | var width = pixels.shape[0] 35 | var height = pixels.shape[1] 36 | if (width > height) { 37 | var trim = Math.floor((width - height) / 2) 38 | cropped = cropped.hi(width - trim, height) 39 | cropped = cropped.lo(trim, 0) 40 | } else { 41 | var trim = Math.floor((height - width) / 2) 42 | cropped = cropped.hi(width, height - trim) 43 | cropped = cropped.lo(0, trim) 44 | } 45 | 46 | var outputShape = shape.concat(4) // RGBA = 4 channels 47 | var outputData = new Uint8ClampedArray(shape[0] * shape[1] * 4) 48 | var output = ndarray(outputData, outputShape) 49 | 50 | for (var i = 0; i < 4; i++) { 51 | resample(output.pick(null, null, i), cropped.pick(null, null, i)) 52 | } 53 | 54 | savePixels(output, 'png').pipe(bl(done)) 55 | } 56 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "resize-png-buffer", 3 | "description": "Resize a PNG buffer into square images of different sizes.", 4 | "version": "1.0.1", 5 | "main": "index.js", 6 | "dependencies": { 7 | "bl": "^1.2.1", 8 | "get-pixels": "^3.3.0", 9 | "ndarray": "^1.0.18", 10 | "ndarray-resample": "^1.0.1", 11 | "save-pixels": "^2.3.4" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "git+https://github.com/yoshuawuyts/resize-png-buffer.git" 16 | }, 17 | "keywords": [ 18 | "resize", 19 | "image", 20 | "png", 21 | "buffer", 22 | "square", 23 | "icon", 24 | "favicon", 25 | "pixels" 26 | ], 27 | "authors": [ 28 | "Hugh Kennedy (https://hughsk.io/)", 29 | "Yoshua Wuyts (https://yoshuwuyts.com/)" 30 | ], 31 | "license": "MIT", 32 | "bugs": { 33 | "url": "https://github.com/hughsk/resize-png-buffer/issues" 34 | }, 35 | "homepage": "https://github.com/hughsk/resize-png-buffer#readme" 36 | } 37 | --------------------------------------------------------------------------------