pica - high quality image resize in browser
===========================================
Resize images in browser without pixelation and reasonably fast.
Autoselect the best of available technologies: webworkers,
webassembly, createImageBitmap, pure JS.
With pica you can:
- Reduce upload size for large images, saving upload time.
- Saves server resources on image processing.
- Generate thumbnails in browser.
- ...
Note. If you need File/Blob resize (from form's file input), consider use
additional machinery to process orientation, keep EXIF metadata and so on.
Migration from pica v6 to pica v7
Multiply unsharpAmount by 2, divide unsharpThreshold by 2, example:
- pica@6: pica.resize(a, b, { unsharpAmount: 80, unsharpThreshold: 2 })
- pica@7: pica.resize(a, b, { unsharpAmount: 160, unsharpThreshold: 1 })
Prior to use
Here is a short list of problems you can face:
- Loading image:
- Due to JS security restrictions, you can process images
from the same domain or local files only. If you load images from
remote domain use proper Access-Control-Allow-Origin header.
- iOS has a memory limits for canvas elements, that may cause
- If your source data is jpeg image, it can be rotated. Consider use
- Saving image:
- Some ancient browsers do not support canvas.toBlob() method.
Use pica.toBlob(), it includes required shim.
- For jpeg source, it's a good idea to keep exif data. Consider use
- Quality
- JS canvas does not support access to info about gamma correction.
Bitmaps have 8 bits per channel. That causes some quality loss,
because with gamma correction precision could be 12 bits per
channel.
- Precision loss will not be noticeable for ordinary images like
kittens, selfies and so on. But we don't recommend this library
for resizing professional quality images.
Install
Use
- ``` js
- const pica = require('pica')();
- // Resize from Canvas/Image to another Canvas
- pica.resize(from, to)
- .then(result => console.log('resize done!'));
- // Resize & convert to blob
- pica.resize(from, to)
- .then(result => pica.toBlob(result, 'image/jpeg', 0.90))
- .then(blob => console.log('resized to canvas & created blob!'));
- ```
API
new Pica(config)
Create resizer instance with given config (optional):
- __tile__ - tile width/height. Images are processed by regions,
to restrict peak memory use. Default 1024.
- __features__ - list of features to use. Default is
[ 'js', 'wasm', 'ww' ]. Can be [ 'js', 'wasm', 'cib', 'ww' ]
or [ 'all' ]. Note, cib is buggy in Chrome and not supports default
mks2013 filter.
- __idle__ - cache timeout, ms. Webworkers create is not fast.
This option allow reuse webworkers effectively. Default 2000.
- __concurrency__ - max webworkers pool size. Default is autodetected
CPU count, but not more than 4.
- __createCanvas__ - function which returns a new canvas, used internally
by pica.
Default returns a [\