Coder Social home page Coder Social logo

volving / pica Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nodeca/pica

0.0 2.0 0.0 5.71 MB

Resize image in browser with high quality and high speed

Home Page: http://nodeca.github.io/pica/demo/

License: MIT License

Makefile 2.47% JavaScript 92.39% CSS 1.44% HTML 3.70%

pica's Introduction

pica - high quality image resize in browser

Build Status NPM version

pica is one more experiment with high speed javascript, from authors of paco. It does high quality image resize in browser as fast as possible.

demo

When you resize images using modern browsers' canvas low quality interpolation algorythms are used by default. That's why we wrote pica.

  • It's not as fast as canvas, but still reasonably fast. With Lanczos filter and window=3 huge image resize (5000x3000px) takes ~1s on desktop and ~3s on mobile.
  • If your browser supports Webworkers, pica automatically uses it to avoid interface freeze.

Why it's useful:

  • reduces upload size for large images to pre-process in browser, saving time and bandwidth
  • saves server resources on image processing
  • HiDPI image technique for responsive and retina
  • use single image for both thumbnail and detailed view

Prior to use

Pica is a low level library that does math with minimal wrappers. If you need to resize binary image, you should take care to load it into canvas first (and to save it back to blob). Here is a short list of problems you can face:

  • Loading image:
    • Due to JS security restrictions, you can load to canvas only images from same domain or local files. If you load images from remote domain use proper Access-Control-Allow-Origin header.
    • iOS has resource limits for canvas size & image size. Look here for details and possible solutions.
    • If you plan to show images on screen after load, you must parse exif header to get proper orientation. Images can be rotated.
  • Saving image:
    • Some ancient browsers do not support .toBlob() method, use https://github.com/blueimp/JavaScript-Canvas-to-Blob if needed.
    • It's a good idea to keep exif data, to avoid palette & rotation info loss. The most simple way is to cut original header and glue it to resized result. Look here for examples.
  • 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

node.js (to develop, build via browserify and so on):

npm install pica

bower:

bower install pica

API

.resizeCanvas(from, to, options, callback)

Resize image from one canvas to another. Sizes are taken from canvas.

  • from - source canvas.
  • to - destination canvas.
  • options - quality (number) or object:
    • quality - 0..3. Default = 3 (lanczos, win=3).
    • alpha - use alpha channel. Default = false.
    • unsharpAmount - >=0, in percents. Default = 0 (off). Usually between 50 to 100 is good.
    • unsharpRadius - >=0.5. Radius of Gaussian blur. If it is less than 0.5, Unsharp Mask is off.
    • unsharpThreshold - 0..255. Default = 0. Threshold for applying unsharp mask.
  • callback(err) - function to call after resize complete:
    • err - error if happened

(!) If WebWorker available, it's returned as function result (not via callback) to allow early termination.

.resizeBuffer(options, callback)

Async resize Uint8Array with raw RGBA bitmap (don't confuse with jpeg / png / ... binaries).

  • options:
    • src - Uint8Array with source data.
    • width - src image width.
    • height - src image height.
    • toWidth - output width.
    • toHeigh - output height.
    • quality - 0..3. Default = 3 (lanczos, win=3).
    • alpha - use alpha channel. Default = false.
    • unsharpAmount - >=0, in percents. Default = 0 (off). Usually between 50 to 100 is good.
    • usnharpRadius - >=0.5. Radius of Gaussian blur. If it is less than 0.5, Unsharp Mask is off.
    • unsharpThreshold - 0..255. Default = 0. Threshold for applying unsharp mask.
    • dest - Optional. Output buffer to write data. Help to avoid data copy if no WebWorkers available. Callback will return result buffer anyway.
    • transferable - Optional. Default = false. Whether to use transferable objects. with webworkers. Can be faster sometime, but you cannot use the source buffer afterward.
  • callback(err, output) - function to call after resize complete:
    • err - error if happened.
    • output - Uint8Array with resized RGBA image data.

(!) If WebWorker available, it's returned as function result (not via callback) to allow early termination.

.WW - true/false

true if webworkers are supported. You can use it for browser capabilities detection. Also, you can set it to false for debuging, so pica will use direct function calls.

What is quality

Pica has presets, to adjust speed/quality ratio. Simply use quality option param:

  • 0 - Box filter, window 0.5px
  • 1 - Hamming filter, window 1.0px
  • 2 - Lanczos filter, window 2.0px
  • 3 - Lanczos filter, window 3.0px

Unsharp mask

Pica has built-in unsharp mask. Set unsharpAmount to positive number to activate the filter.

The parameters of it are similar to ones from Photoshop. We recommend to start from unsharpAmount = 80, unsharpRadius = 0.6, unsharpThreshop = 2. There is a correspondence between UnsharpMask parameters in popular graphics software.

Browser support

We didn't have time to test all possible combinations, but in general:

Note. Though you can run this package on node.js, browsers are the main target platform. On server side we recommend to use GraphicsMagick or ImageMagick for better speed.

References

You can find these links useful:

Authors

Licence

MIT

pica's People

Contributors

llacroix avatar avalanche1 avatar devongovett avatar thokari avatar tombyrer avatar

Watchers

James Cloos avatar WEI avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.