npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

willyb-sharper

v1.0.3

Published

Automatic image processor built on top of sharp and multer for express.

Downloads

3

Readme

sharper Build Status npm downloads npm version npm dependencies

Automatic image processor built on top of sharp and multer for express.


How does it work?

When you are working on a website which needs to create multiple size copies of a single image uploaded by user, resize | crop | process and finally upload in a dynamic directory based on current date or time, then this module will save your entire lifetime.

Basically, this module provide an express middleware that do all above things at once. If something goes wrong, you can catch it in error middleware. You need to provide few options that will tell sharper about location of upload, dynamic date format for upload folder to be created, accepted image types, upload size limits, output format, and output sizes with prefixes etc.

This module is based on sharp node-js module for image processing. It's the fastest module there is and based on libvips library.


Install

npm install --save sharper

Configure

set options

const sharper = require('sharper');
var Sharper = sharper({
    field : 'file',
    location : '/var/www/uploads/',
    dirFormat : 'yyyy/mmm/d',
    maxFileSize: '20mb',
    fileNameLen : 50,
    accept : ['jpeg', 'jpg', 'png'],
    output : 'jpg',
    sizes : [
        {suffix : 'xlg', width : 1200, height : 1200},
        {suffix : 'lg', width : 800, height : 800},
        {suffix : 'md', width : 500, height : 500},
        {suffix : 'sm', width : 300, height : 300},
        {suffix : 'xs', width : 100, height : 100},
    ]
});

add middleware

app.post('/upload', Sharper, function(err, req, res, next){
    console.log(err);
    res.status(500).send('upload failed...');
}, function(req, res){
    console.log(res.sharper);
    res.send('upload successful...');
});

Options

var Sharper = sharper(options);

sharper options

| option | default | role | | ------ | ------- | ---- | | field | 'file' | input file field in multipart form data. ex. <input type="file" name="file"/> | | location | '/var/www/html/' | upload directory. | | dirFormat | 'yyyy/mmm/d' | date pattern to follow for folder creation for image storage. Explore formats at [node-format module](https://github.com/felixge/node-dateformat#mask-options) | | fileNameLen | 50 | length of the output image filename. | | maxFileSize | '10mb' | maximum size of input/upload file to accept. | | accept | ['png','jpeg','jpg'] | array of file types/extensions to accept. | | output | 'jpg' | type of output file to produce. valid options : jpg, png, gif, webp, tiff, bmp | | sizes | [{suffix : 'lg', width : 500, height:500}, ...] | Array of size specification object for output images (with filename suffix to produce output-name.lg.jpg format). |

sharp options

Please visit this sharp sharp for detailed overview of specific option.

| option | default | role | | ------ | ------- | ---- | | resize | true | resize images as per their sizes mentioned in options.sizes | | crop | false | crop images as per their sizes mentioned in options.sizes | | background | {r: 200 , g:200, b:200, a:1} | set the background for the embed, flatten and extend operations. | | embed | false | embed on canvas | | max | false | set maximum output dimension | | min | false | set minimum output dimension | | withoutEnlargement | false | do not enlarge small images | | ignoreAspectRatio | false | ignore aspect ration while resizing images | | extract | false | extract specific part of image | | trim | false | Trim boring pixels from all edges | | flatten | false | Merge alpha transparency channel, if any, with background. | | extend | false | Extends/pads the edges of the image with background. | | negate | false | Produces the negative of the image. | | rotate | false | Rotate the output image by either an explicit angle | | flip | false | Flip the image about the vertical Y axis. | | flop | false | Flop the image about the horizontal X axis. | | blur | false | Mild blur of the output image | | sharpen | false | Mild sharpen of the output image | | gamma | false | Apply a gamma correction. | | grayscale or greyscale | false | Convert to 8-bit greyscale; 256 shades of grey. | | normalize or normalise | false | Enhance output image contrast by stretching its luminance to cover the full dynamic range. | | progressive | false | Use progressive (interlace) scan for JPEG and PNG output. | | quality | false | The output quality to use for lossy JPEG, WebP and TIFF output formats. The default quality is 80. |


Errors

You can catch errors in error middleware as shown previously. err object includes (but not limited to) following error codes (key code).

| code | role | | ------ | ------- | | FILE_TYPE_UNSUPPORTED | Input file type is not acceptable to process. check accept option. | | LIMIT_FILE_SIZE | Input file size is too large. Check maxFileSize option. |


Success

Upon successful upload, sharper will add sharper object in res response object. It will have following properties.

| key | role | | --- | ---- | | dir | dir that was created for storing process image. | | destination | absolute path of storage destination. | | filename | name of the image files created using sharper. |


Test

  • npm install
  • npm install -g mocha
  • npm test

in practice

  • npm install -g bower
  • bower install
  • Open terminal and run node test-server.js in demo folder of this module.
  • Open your browser and go to http://localhost:8800.
  • Upload a photo by clicking on dropzone.
  • If upload sucessfully completes, check upload directory /var/www/html.
  • If error occurs, check your terminal.

If you are facing node-gyp related error, go to node_modules/sharper/node_modules/sharp and use cmd command node-gyp rebuild. Make sure you have node-gyp installed or use npm install -g node-gyp