Crop image files or Buffer
s
Depends on node-canvas which has special build instructions as it requires Cairo to be installed on your system.
crp shares the same API as rsz, except rsz is for resizing images rather than cropping. See also sz for simply obtaining the size of an image, and thmb for making thumbnails of images.
There is one method but multiple ways to use it:
crp(src, width, height, function (err, buf) { /* */ })
Where src
is a String
specifying the path to the image or a Buffer
containing the image data, and buf
is a Buffer
containing the cropped image data.
crp(src, width, height, dst, function (err) { /* */ })
Where src
is a String
specifying the path to the image or a Buffer
containing the image data, and dst
is a String
specifying the path to write the output file to.
crp(src, { width: w, height: h }, function (err, buf) { /* */ })
Where w
and h
are the width and height respectively, src
is a String
specifying the path to the image or a Buffer
containing the image data, and buf
is a Buffer
containing the cropped image data.
crp(src, { width: w, height: h }, dst, function (err) { /* */ })
Where w
and h
are the width and height respectively, src
is a String
specifying the path to the image or a Buffer
containing the image data, and dst
is a String
specifying the path to write the output file to.
By default, crp will return a PNG Buffer
or write a PNG file. You can change this when you pass an options
object: { height: 100, width: 100, type: 'jpeg' }
. You can also adjust the quality with a 'quality'
property.
'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 JPEGBuffer
or write a JPEG file.'quality'
(Number
, optional) used when creating a JPEG, a number between 1 (lowest quality) and 100 (highest quality).
var crp = require('crp')
, fs = require('fs')
crp('/path/to/nyancat.gif', 200, 350, function (err, buf) {
fs.writeFileSync('/path/to/nyancat_200_350.png', buf)
})
// or
crp('/path/to/nyancat.gif', 200, 350, '/path/to/nyancat_200_350.png', function (err) {
})
// or
crp('/path/to/nyancat.gif', { width: 200, height: 350 }, function (err, buf) {
fs.writeFileSync('/path/to/nyancat_200_350.png', buf)
})
// or
crp('/path/to/nyancat.gif', { width: 200, height: 350 }, '/path/to/nyancat_200_350.png', function (err) {
})
// or a jpeg
crp(
'/path/to/avatar.png'
, { width: 50, height: 50, type: 'jpeg', quality: 40 }
, '/path/to/avatar_50_50.jpg'
, function (err) {
/* ... */
}
)
crp currently only does a centred crop and if the image is smaller than the crop size then it'll likely fill black. I'm open to adding options to change behaviour to making it more useful, just file issues or send pull requests for what you need!
crp 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.