thmb

An image thumbnailer

npm install thmb
1 downloads in the last day
7 downloads in the last week
11 downloads in the last month

thmb Build Status

Make thumbnails of images

Depends on node-canvas which has special build instructions as it requires Cairo to be installed on your system.

What?

thmb takes a source image path and a destination image path and the dimensions of your desired thumbnail.

The created thumbnail image will be a resized and cropped version of the original. If the destination image isn't the same aspect ratio of the original then it'll resize it to fit within the space and crop the remainder. e.g. if your source aspect ratio is wider than your destination aspect ratio then it'll first be resized to the desired height and then the overhanging width will be cropped.

The thumbnail will not be created if file destination file already exists and has a newer mtime than the source file. If you are planning on using this utility for different size thumbnails then you are advised to use the size in your destination path somewhere as thmb won't check the destination file size if it exists, only the last-modified-timestamp of the file.

thmb builds on:

  • sz gets the sizes of images
  • rsz resizes images
  • crp crops images

API

thmb(src, dst, options, callback)

Where src is the source file path, dst is the destination file path and options contains your thumbnailing options.

The callback function will be called with an error object if an error has occurred (such as the file not existing), the second argument in the case of a successful thumbnail creation, or an existing thumbnail that didn't need to be updated will be the dimensions of the source image (this happens to be what I needed on the callback but you're welcome to suggest alternative callback data).

Options

  • 'height' (Number, required) the height of the cropped image
  • 'width' (Number, required) the width of the cropped image
  • 'type' (String, optional, default: 'png') set to 'jpeg' to return a JPEG Buffer or write a JPEG file.
  • 'quality' (Number, optional) used when creating a JPEG, a number between 1 (lowest quality) and 100 (highest quality).

By default, thmb will write a PNG file. You can change this by specifying the 'type' on your options object. You can also adjust the JPEG quality with a 'quality' property.

Example

var thmb = require('thmb')

thmb(
    '/path/to/nyancat.gif'
  , '/path/to/nyancat_50x50.jpg'
  , { width: 50, height: 50, type: 'jpeg', quality: 40 }
  , function (err, srcSize) {
      // srcSize might be something like `{ width: 400, height: 250 }`
    }
)

Want more options?

Send a pull request or file an issue & I'll gladly consider extensions if you have a use-case.

Licence

thmb is Copyright (c) 2013 Rod Vagg @rvagg and licensed under the MIT licence. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.

npm loves you