Add usage example and further unit test for new max option

Simplify max vs crop logic
This commit is contained in:
Lovell Fuller
2014-05-19 21:52:47 +01:00
parent ad7735a0a6
commit 276ba5228b
3 changed files with 40 additions and 18 deletions

View File

@@ -85,25 +85,25 @@ sharp('input.jpg').resize(300, 200).write('output.jpg', function(err) {
```
```javascript
sharp('input.jpg').resize(null, 200).progressive().toBuffer(function(err, buffer) {
sharp('input.jpg').resize(null, 200).progressive().toBuffer(function(err, outputBuffer) {
if (err) {
throw err;
}
// buffer contains progressive JPEG image data, 200 pixels high
// outputBuffer contains progressive JPEG image data, 200 pixels high
});
```
```javascript
sharp('input.png').resize(300).sharpen().quality(90).webp(function(err, buffer) {
sharp('input.png').resize(300).sharpen().quality(90).webp(function(err, outputBuffer) {
if (err) {
throw err;
}
// buffer contains 300 pixels wide, sharpened, 90% quality WebP image data
// outputBuffer contains 300 pixels wide, sharpened, 90% quality WebP image data
});
```
```javascript
sharp(buffer).resize(200, 300).embedWhite().write('output.tiff', function(err) {
sharp(inputBuffer).resize(200, 300).embedWhite().write('output.tiff', function(err) {
if (err) {
throw err;
}
@@ -113,15 +113,25 @@ sharp(buffer).resize(200, 300).embedWhite().write('output.tiff', function(err) {
```
```javascript
sharp('input.gif').resize(200, 300).embedBlack().webp(function(err, buffer) {
sharp('input.gif').resize(200, 300).embedBlack().webp(function(err, outputBuffer) {
if (err) {
throw err;
}
// buffer contains WebP image data of a 200 pixels wide and 300 pixels high image
// outputBuffer contains WebP image data of a 200 pixels wide and 300 pixels high
// containing a scaled version, embedded on a black canvas, of input.gif
});
```
```javascript
sharp(inputBuffer).resize(200, 200).max().jpeg(function(err, outputBuffer) {
if (err) {
throw err;
}
// outputBuffer contains JPEG image data no wider than 200 pixels and no higher
// than 200 pixels regardless of the inputBuffer image dimensions
});
```
## API
### sharp(input)
@@ -147,6 +157,8 @@ Crop the resized image to the exact size specified, the default behaviour.
Preserving aspect ratio, resize the image to the maximum width or height specified.
Both `width` and `height` must be provided via `resize` otherwise the behaviour will default to `crop`.
### embedWhite()
Embed the resized image on a white background of the exact size specified.