browser-image-resizer
Introduction
This library allows for cross-browser image downscaling and resizing utilizing <canvas>
. The code was part of Ross Turner's HTML5-ImageUploader. Note that this is meant to be a browser-only utility and will not work in Node.js. Adapted from original work to allow manyally setting the exif-orientation for rotation and flipping purposes.
Demo
https://browser-image-resizer-test.firebaseapp.com/
Installation
npm install browser-image-resizer
yarn add browser-image-resizer
Sample - Promises
; const config = quality: 05 maxWidth: 800 maxHeight: 600 autoRotate: true debug: true; // Note: A single file comes from event.target.files on <input> ;
Sample - Async/Await
; const config = quality: 07 width: 800 height: 600; // Note: A single file comes from event.target.files on <input> { try let resizedImage = await ; const url = `http://localhost:3001/upload`; const formData = ; formData; const options = method: 'POST' body: formData ; let result = await ; // TODO: Handle the result console; return result; catch error console; throwerror; }
readAndCompressImage(file, config) => Promise
Inputs
file
: A File object, usually from an<input>
config
: See below
Property Name | Purpose | Default Value |
---|---|---|
quality |
The quality of the JPEG | 0.5 |
maxWidth |
The maximum width for the downscaled image | 800 |
maxHeight |
The maximum height for the downscaled image | 600 |
autoRotate |
Reads EXIF data on the image to determine orientation | true |
exifOrientation |
Manually sets EXIF orientation value. Only works if autoRotate is false |
0 |
debug |
console.log image update operations | false |
Outputs
A Promise that yields a JPEG Blob