Giter VIP home page Giter VIP logo

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 images resize in browser as fast as possible.

demo

If you need to resize images in modern browsers, by default, use canvas' low quality interpolation algorythms. That's why we wrote pica.

  • It's not as fast as canvas, but still reasonably fast. With Lanczos filter and window=3, and huge image 5000x3000px resize 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:

  • reduce upload size for big images to pre-process in browser, saving time and bandwidth
  • save server resources for image processing
  • HiDPI image technique for responsive and retna
  • download single image for both thumbnail and detail-zoom

Install

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

npm install pica

bower:

bower install pica

API

.resizeBuffer(options, callback)

Async resize Uint8Array with RGBA image.

  • 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..500. Default = 0 (off). Usually between 50 to 100 is good.
    • unsharpThreshold - 0..100. Default = 0. Try 10 as starting point.
    • 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.

.resizeCanvas(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..500. Default = 0 (off). Usually between 50 to 100 is good.
    • unsharpThreshold - 0..100. Default = 0. Try 10 as starting point.
  • 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.

.WW - true/false

true if webworkers are supported. You can use it for capabilities detection. Also, you can set it 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, similar to photoshop, but limited with radius 1.0. It's off by default. Set unsharpAmount and unsharpThresold to activate filter.

Browsers support

We had no 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

devongovett avatar llacroix avatar tombyrer avatar

Stargazers

 avatar

Watchers

 avatar  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.