---
title: Images
project: Total.js V4
version: 0.0.99
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/total-js-v4/0-0-99/reference/images/
updated: 2026-11-16T15:41:20.819Z
---

# Images

> The global Image object, the methods of an image, and the image config keys.

The global `Image` object loads and measures images, and an image object builds the commands for GraphicsMagick or ImageMagick. The guide [Images](../../guides/images/) explains how to use them with examples; this page is the exact reference.

## API

### new Image.Image(source, [converter], [width], [height])

Type: `class`

An image with a list of operations for GraphicsMagick or ImageMagick.

Returns: `Image`

Create it with `Image.load()`; the constructor is `Image.Image` (alias `Image.Picture`). Properties:

- `filename` — the source file, or `null` for a stream or a buffer
- `outputType` — the output format; the extension of the file, `jpg` for streams and buffers, or the value of `output()`
- `width`, `height` — the values passed to `Image.load()` (they are not measured)
- `builder` — the list of operations added so far

All operation methods return the image. Add your own methods with `F.prototypes()`, whose argument has the `Image` prototype.

Example: Extend the prototype
```js
F.prototypes(function(proto) {
	proto.Image.avatar = function() {
		return this.resizeCenter(128, 128).quality(80);
	};
});

Image.load(filename).avatar().save(target);
```

### Image.load(source, [converter], [width], [height])

Type: `method`

Creates an image object from a file, a stream or a buffer.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `source` | `String|Stream|Buffer` | yes |  | A file name, a readable stream or a buffer with the image. |
| `converter` | `String` | no | `CONF.default_image_converter` | `gm` (GraphicsMagick), `im` (ImageMagick 6, `convert`) or `magick` (ImageMagick 7). |
| `width` | `Number` | no |  | Stored in `image.width`; not used by the operations. |
| `height` | `Number` | no |  | Stored in `image.height`; not used by the operations. |

Returns: `Image` A new image.

Nothing is read or run yet. For a file name, the output format is the file extension; for a stream or a buffer, it is `jpg` until you call `output()`.

Example: Resize a file
```js
Image.load(PATH.public('photos/1.jpg')).resize(640).save(PATH.public('photos/1-small.jpg'));
```

Example: From a buffer with ImageMagick 7
```js
var image = Image.load(buffer, 'magick');
image.output('png');
image.resize(64, 64);
image.pipe(res);
```

### Image.init(source, [converter], [width], [height])

Type: `method`

The same function as `Image.load()`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `source` | `String|Stream|Buffer` | yes |  | A file name, a readable stream or a buffer with the image. |
| `converter` | `String` | no | `CONF.default_image_converter` | `gm` (GraphicsMagick), `im` (ImageMagick 6, `convert`) or `magick` (ImageMagick 7). |
| `width` | `Number` | no |  | Stored in `image.width`; not used by the operations. |
| `height` | `Number` | no |  | Stored in `image.height`; not used by the operations. |

Returns: `Image` A new image.

Creates an image object; the arguments and the result are the same as in `Image.load()`.

Example: Create an image
```js
var image = Image.init(filename);
image.grayscale().save(filename);
```

### Image.measure(type, buffer)

Type: `method`

Returns the width and height of an image from its first bytes.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `type` | `String` | yes |  | Extension or MIME type. |
| `buffer` | `Buffer` | yes |  | The beginning of the file (the whole file for SVG; for JPEG, enough bytes to reach the frame header). |

Returns: `Object` `{ width, height }`, or `undefined`/`null` when it cannot be read.

Calls the measuring function for the type. `type` is an extension with or without the dot, or a MIME type: `jpg`, `jpeg`, `png`, `gif`, `svg`, `webp`, `bmp`, `psd` (`image/jpeg`, `image/png`, `image/gif`, `image/svg+xml`, `image/webp`, `image/bmp`, `image/vnd.adobe.photoshop`). For other types it returns `undefined`. No external tool is needed.

Example: Measure an uploaded buffer
```js
var size = Image.measure('image/png', buffer);
if (size && size.width > 2000)
	console.log('Too large');
```

### Image.measureJPG(buffer)

Type: `method`

Returns the width and height of a JPG image.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `buffer` | `Buffer` | yes |  | The beginning of the file. |

Returns: `Object` `{ width, height }`

Walks the JPEG markers to the first frame header (SOF). Returns `null` when the buffer is not a JPEG or the header is not in the buffer.

Example: Measure a file
```js
var size = Image.measureJPG(Fs.readFileSync(filename));
console.log(size.width, size.height);
```

### Image.measurePNG(buffer)

Type: `method`

Returns the width and height of a PNG image.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `buffer` | `Buffer` | yes |  | The beginning of the file. |

Returns: `Object` `{ width, height }`

Reads the size from the IHDR chunk; 24 bytes are enough.

Example: Measure a file
```js
var size = Image.measurePNG(Fs.readFileSync(filename));
console.log(size.width, size.height);
```

### Image.measureGIF(buffer)

Type: `method`

Returns the width and height of a GIF image.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `buffer` | `Buffer` | yes |  | The beginning of the file. |

Returns: `Object` `{ width, height }`

Reads the logical screen size; 10 bytes are enough.

Example: Measure a file
```js
var size = Image.measureGIF(Fs.readFileSync(filename));
console.log(size.width, size.height);
```

### Image.measureBMP(buffer)

Type: `method`

Returns the width and height of a BMP image.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `buffer` | `Buffer` | yes |  | The beginning of the file. |

Returns: `Object` `{ width, height }`

Reads the size from the BMP header. The height is always positive (top-down bitmaps have a negative height in the file).

Example: Measure a file
```js
var size = Image.measureBMP(Fs.readFileSync(filename));
console.log(size.width, size.height);
```

### Image.measurePSD(buffer)

Type: `method`

Returns the width and height of a PSD image.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `buffer` | `Buffer` | yes |  | The beginning of the file. |

Returns: `Object` `{ width, height }`

Reads the size from the Photoshop header.

Example: Measure a file
```js
var size = Image.measurePSD(Fs.readFileSync(filename));
console.log(size.width, size.height);
```

### Image.measureWEBP(buffer)

Type: `method`

Returns the width and height of a lossy or lossless WebP image.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `buffer` | `Buffer` | yes |  | The beginning of the file. |

Returns: `Object` `{ width, height }`, or `undefined`.

Supports lossy (`VP8 `) and lossless (`VP8L`) WebP images. It needs the first 30 bytes. It returns `undefined` for an unknown header.

Example: Measure a WebP file
```js
var size = Image.measureWEBP(Fs.readFileSync(PATH.public('img/hero.webp')));
```

### Image.measureSVG(buffer)

Type: `method`

Returns the width and height of an SVG image.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `buffer` | `Buffer` | yes |  | The SVG file. |

Returns: `Object` `{ width, height }`, or `undefined`.

Searches the text for the first `width="N"` and `height="N"` attributes with whole numbers. Units and `viewBox` are not read. It returns `undefined` when there are no such attributes, and `0` for a missing side.

Example: Measure an SVG
```js
var size = Image.measureSVG(Buffer.from('<svg width="120" height="40"></svg>'));
// { width: 120, height: 40 }
```

### Image.middleware(type, fn)

Type: `method`

Registers a stream that processes every output of a type.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `type` | `String` | yes |  | Output type, for example `png` or `.png`. |
| `fn` | `Function` | yes |  | Returns a new transform stream. |

Returns: `undefined`

`fn()` must return a new transform (duplex) stream on each call. The output of `pipe()` and `stream()` for that type passes through it. After `save()`, the saved file is read, passed through the stream and written again before the callback. Only one middleware per type; a new one replaces the old one. The type is matched with the output type (`png`, `jpg`...), so `jpeg` and `jpg` are different.

Example: Pass all PNG output through a stream
```js
var Stream = require('stream');

Image.middleware('png', function() {
	return new Stream.PassThrough();
});
```

### image.resize([w], [h], [options])

Type: `method`

Resizes the image and keeps its aspect ratio.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `w` | `Number` | no |  | Width in pixels. Use `0` or `null` to compute it from the height. |
| `h` | `Number` | no |  | Height in pixels. Omit it to compute it from the width. |
| `options` | `String` | no |  | A suffix of the size: `!` (ignore aspect ratio), `>` (only shrink), `<` (only enlarge), `^` (fill), `%` (percent). |

Returns: `Image` The same image, for chaining.

Adds `-resize WxH`. With only the width, the height is computed, and the other way round. The image fits into the box; add `options` to change it.

Example: Common sizes
```js
image.resize(800);           // width 800
image.resize(null, 600);     // height 600
image.resize(800, 600, '!'); // exactly 800x600
image.resize(1920, 1920, '>'); // only shrink
```

### image.resizeCenter(w, h, [color])

Type: `method`

Fills the size and crops the rest around the center.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `w` | `Number` | yes |  | Width in pixels. |
| `h` | `Number` | yes |  | Height in pixels. |
| `color` | `String` | no | `'white'` | Background color, for example `white` or `#F0F0F0`. |

Returns: `Image` The same image, for chaining.

Resizes with `^` so the image covers `w` x `h`, then crops the center part. The result has exactly the given size. `resize_center()` is an alias.

Example: A square avatar
```js
Image.load(filename).resizeCenter(200, 200).save(target);
```

### image.resizeAlign(w, h, [align], [color])

Type: `method`

Fills the size and crops the rest around a side or a corner.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `w` | `Number` | yes |  | Width in pixels. |
| `h` | `Number` | yes |  | Height in pixels. |
| `align` | `String` | no | `'center'` | Which part to keep. |
| `color` | `String` | no | `'white'` | Background color, for example `white` or `#F0F0F0`. |

Returns: `Image` The same image, for chaining.

Like `resizeCenter()`, but keeps the part given by `align` (the values of `align()`, for example `top`, `bottom`, `top left`). `resize_align()` is an alias.

Example: Keep the top part
```js
image.resizeAlign(1200, 400, 'top');
```

### image.miniature(w, h, [color], [filter])

Type: `method`

Fits the whole image into the size and fills the empty area.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `w` | `Number` | yes |  | Width in pixels. |
| `h` | `Number` | yes |  | Height in pixels. |
| `color` | `String` | no | `'white'` | Background color, for example `white` or `#F0F0F0`. |
| `filter` | `String` | no | `'Hamming'` | Resize filter. |

Returns: `Image` The same image, for chaining.

Runs `thumbnail(w, h)` with the filter, centers the result and extends it to exactly `w` x `h` with the background color. Nothing is cut off.

Example: Product picture on white
```js
image.miniature(300, 300);
image.miniature(300, 300, '#000000', 'Lanczos');
```

### image.thumbnail([w], [h], [options])

Type: `method`

Resizes the image with the `-thumbnail` option.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `w` | `Number` | no |  | Width in pixels. Use `0` or `null` to compute it from the height. |
| `h` | `Number` | no |  | Height in pixels. Omit it to compute it from the width. |
| `options` | `String` | no |  | A suffix of the size: `!` (ignore aspect ratio), `>` (only shrink), `<` (only enlarge), `^` (fill), `%` (percent). |

Returns: `Image` The same image, for chaining.

Works like `resize()`. The tool also removes the profiles, so it is a good choice for small previews.

Example: Preview
```js
image.thumbnail(150, 150);
```

### image.scale([w], [h], [options])

Type: `method`

Resizes the image without a resampling filter.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `w` | `Number` | no |  | Width in pixels. Use `0` or `null` to compute it from the height. |
| `h` | `Number` | no |  | Height in pixels. Omit it to compute it from the width. |
| `options` | `String` | no |  | A suffix of the size: `!` (ignore aspect ratio), `>` (only shrink), `<` (only enlarge), `^` (fill), `%` (percent). |

Returns: `Image` The same image, for chaining.

Adds `-scale`. It is faster than `resize()` and gives a lower quality.

Example: Half size
```js
image.scale(50, 50, '%');
```

### image.crop(w, h, [x], [y])

Type: `method`

Cuts out a rectangle.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `w` | `Number` | yes |  | Width in pixels. |
| `h` | `Number` | yes |  | Height in pixels. |
| `x` | `Number` | no | `0` | Left offset. |
| `y` | `Number` | no | `0` | Top offset. |

Returns: `Image` The same image, for chaining.

Adds `-crop WxH+X+Y`. The offset is relative to the gravity set with `align()` (top left by default). The offset must be zero or positive.

Example: Crop a region
```js
image.crop(400, 300, 50, 20);
```

### image.extent([w], [h], [x], [y])

Type: `method`

Sets the canvas size.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `w` | `Number` | no |  | Width in pixels. Use `0` or `null` to compute it from the height. |
| `h` | `Number` | no |  | Height in pixels. Omit it to compute it from the width. |
| `x` | `Number` | no |  | Horizontal offset. |
| `y` | `Number` | no |  | Vertical offset. |

Returns: `Image` The same image, for chaining.

Adds `-extent`. A larger canvas is filled with the background color; a smaller one cuts the image. `x` and `y` move the image (negative values are allowed).

Example: Square canvas
```js
image.background('white').align('center').extent(500, 500);
```

### image.align(type)

Type: `method`

Sets the gravity of `crop()`, `extent()` and `watermark()`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `type` | `String` | yes |  | The position. |

Returns: `Image` The same image, for chaining.

Adds `-gravity`. Values: `top left`, `top`, `top right`, `left`, `center` (or `middle`), `right`, `bottom left`, `bottom`, `bottom right`; the words can be in any order (`left top`) and `center` can be added (`top center`). Other values go to the tool unchanged, for example `NorthEast`. `gravity()` is an alias.

Example: Watermark in the corner
```js
image.align('bottom right');
image.watermark(PATH.public('img/logo.png'), 10, 10);
```

### image.filter(type)

Type: `method`

Sets the resampling filter for resizing.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `type` | `String` | yes |  | Filter name. |

Returns: `Image` The same image, for chaining.

Adds `-filter`, for example `Lanczos`, `Box`, `Hamming`, `Triangle`. See the documentation of your tool for all names.

Example: Sharp downscale
```js
image.filter('Lanczos').resize(800);
```

### image.quality([percentage])

Type: `method`

Sets the compression quality.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `percentage` | `Number` | no | `80` | From 1 to 100. |

Returns: `Image` The same image, for chaining.

Adds `-quality`. Without an argument, the value is `80`.

Example: Set the quality
```js
image.quality(85);
```

### image.rotate(deg)

Type: `method`

Rotates the image.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `deg` | `Number` | yes | `0` | Degrees. |

Returns: `Image` The same image, for chaining.

Adds `-rotate`. Positive values rotate clockwise. It runs after the resize and crop operations (priority 8).

Example: Rotate
```js
image.rotate(90);
```

### image.flip()

Type: `method`

Mirrors the image vertically (top to bottom).

Returns: `Image` The same image, for chaining.

Adds `-flip`.

Example: Flip
```js
image.flip();
```

### image.flop()

Type: `method`

Mirrors the image horizontally (left to right).

Returns: `Image` The same image, for chaining.

Adds `-flop`.

Example: Flop
```js
image.flop();
```

### image.blur(radius)

Type: `method`

Blurs the image.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `radius` | `Number|String` | yes |  | The blur radius. |

Returns: `Image` The same image, for chaining.

Adds `-blur`. The value is passed to the tool, so it can be a number or `radiusxsigma`, for example `0x8`.

Example: Blur
```js
image.blur('0x8');
```

### image.grayscale()

Type: `method`

Converts the image to gray.

Returns: `Image` The same image, for chaining.

Adds `-colorspace Gray`.

Example: Gray
```js
image.grayscale();
```

### image.sepia()

Type: `method`

Gives the image a sepia tone.

Returns: `Image` The same image, for chaining.

Adds `-modulate 115,0,100` and `-colorize 7,21,50`.

Example: Sepia
```js
image.sepia();
```

### image.normalize()

Type: `method`

Stretches the contrast to the full range.

Returns: `Image` The same image, for chaining.

Adds `-normalize`.

Example: Normalize
```js
image.normalize();
```

### image.colors(value)

Type: `method`

Reduces the number of colors.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Number` | yes |  | The maximum number of colors. |

Returns: `Image` The same image, for chaining.

Adds `-colors`. Useful for small PNG and GIF files.

Example: 256 colors
```js
image.output('png').colors(256);
```

### image.bitdepth(value)

Type: `method`

Sets the bit depth of a channel.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Number` | yes |  | Bits per channel, for example `8`. |

Returns: `Image` The same image, for chaining.

Adds `-depth`.

Example: 8 bits
```js
image.bitdepth(8);
```

### image.background(color)

Type: `method`

Sets the background color.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `color` | `String` | yes |  | A color name or `#RRGGBB`. |

Returns: `Image` The same image, for chaining.

Adds `-background` and `-extent 0x0`. The color fills transparent areas and the area added by `extent()`, `rotate()` and `miniature()`.

Example: White background
```js
image.background('#FFFFFF');
```

### image.fill(color)

Type: `method`

Sets the fill color for drawing.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `color` | `String` | yes |  | A color name or `#RRGGBB`. |

Returns: `Image` The same image, for chaining.

Adds `-fill`. Use it with `command()` options that draw, for example `-colorize`.

Example: Fill
```js
image.fill('red');
```

### image.watermark(filename, [x], [y], [w], [h])

Type: `method`

Draws another image over this one.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `filename` | `String` | yes |  | The watermark file. |
| `x` | `Number` | no | `0` | Horizontal offset. |
| `y` | `Number` | no | `0` | Vertical offset. |
| `w` | `Number` | no | `0` | Width of the watermark. |
| `h` | `Number` | no | `0` | Height of the watermark. |

Returns: `Image` The same image, for chaining.

Adds `-draw` with `image over X,Y W,H filename`. The file name must not contain spaces or quotes. The position is relative to the gravity from `align()`. With `w` and `h` set to `0`, the watermark keeps its size.

Example: Logo in the bottom right corner
```js
Image.load(filename)
	.resize(1200)
	.align('bottom right')
	.watermark(PATH.public('img/logo.png'), 20, 20)
	.save(target);
```

### image.limit(type, value)

Type: `method`

Limits the resources the tool can use.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `type` | `String` | yes |  | The resource. |
| `value` | `Number|String` | yes |  | The limit, in the units of the tool. |

Returns: `Image` The same image, for chaining.

Adds `-limit type value`, for example `memory`, `map`, `disk`, `threads`. When you call it, `default_image_consumption` is not applied to this image.

Example: Two threads
```js
image.limit('threads', 2);
```

### image.minify()

Type: `method`

Removes the profiles and metadata.

Returns: `Image` The same image, for chaining.

Adds `+profile "*"`, which removes EXIF, ICC and other profiles and makes the file smaller. `save()`, `pipe()` and `stream()` call it on their own when the image has no operation.

Example: Strip metadata
```js
Image.load(filename).minify().save(filename);
```

### image.output(type)

Type: `method`

Sets the output format.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `type` | `String` | yes |  | For example `png`, `webp`, `.jpg`. |

Returns: `Image` The same image, for chaining.

Changes `image.outputType` (a leading dot is removed). `pipe()` and `stream()` use it as the format, and `res.image()` uses it for the `Content-Type`. `save()` takes the format from the extension of the target file.

Example: Convert a stream to PNG
```js
Image.load(stream).output('png').resize(100).pipe(res);
```

### image.make(fn)

Type: `method`

Calls a function with the image.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function(image)` | yes |  | Adds the operations. |

Returns: `Image` The same image, for chaining.

`fn` is called with the image as `this` and as the argument. Use it to reuse a set of operations.

Example: Reusable preset
```js
function preview(image) {
	image.resize(300).quality(70);
}

Image.load(filename).make(preview).save(target);
```

### image.command(key, [value], [priority], [escape])

Type: `method`

Adds any option of the tool.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `key` | `String` | yes |  | The option, for example `-sharpen`. |
| `value` | `String|Number` | no |  | The value of the option. |
| `priority` | `Number|Boolean` | no | `10` | Order of the option. |
| `escape` | `Boolean` | no | `false` | Quote the value. |

Returns: `Image` The same image, for chaining.

Use it for options without their own method, for example `-strip`, `-sharpen` or `-define`. The options run in the order of their priority (see the guide). `escape` puts the value in quotes and removes `'` from it; use it for values that come from users. With `priority` set to `true`, the priority is `0` and the value is escaped.

Example: Sharpen and interlace
```js
image.resize(800);
image.command('-sharpen', '0x1');
image.command('-interlace', 'Plane');
```

### image.clear()

Type: `method`

Removes all operations.

Returns: `Image` The same image, for chaining.

Empties `image.builder`. `save()` calls it after it runs, so one image object can be used again.

Example: Start again
```js
image.clear().resize(100);
```

### image.measure(callback)

Type: `method`

Reads the width and height of the source file.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `callback` | `Function(err, size)` | yes |  | `size` is `{ width, height }`. |

Returns: `Image` The same image, for chaining.

Reads the beginning of the file and measures it in JavaScript. It works only for an image loaded from a file name with the extension `.jpg`, `.png`, `.gif` or `.webp` (lower or upper case); other extensions, `.jpeg` included, give an error. For a stream or a buffer use `Image.measure()`, and for other formats `identify()`.

Example: Measure a file
```js
Image.load(PATH.public('img/a.jpg')).measure(function(err, size) {
	console.log(size.width, size.height);
});
```

### image.identify(callback)

Type: `method`

Reads the type and size of the source file with the external tool.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `callback` | `Function(err, info)` | yes |  | `info` is `{ type, width, height }`, for example `{ type: 'JPEG', width: 800, height: 600 }`. |

Returns: `Image` The same image, for chaining.

Runs `gm identify` (or `identify` for `im` and `magick`) and parses the first line. It needs an image loaded from a file name.

Example: Identify
```js
Image.load(filename).identify(function(err, info) {
	err && console.error(err);
	info && console.log(info.type, info.width, info.height);
});
```

### image.save([filename], [callback], [writer])

Type: `method`

Runs the operations and saves the result to a file.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `filename` | `String` | no | `the source file` | The target file. |
| `callback` | `Function(err, saved)` | no |  | `saved` is `true` on success and `false` on an error. |
| `writer` | `Function(stdin)` | no |  | Gets the input stream of the tool, to write the source yourself. |

Returns: `Image` The same image, for chaining.

Without `filename`, the source file is overwritten. The format follows the extension of `filename`. A registered `Image.middleware()` for the output type runs after the file is written. After the run, the operations are cleared.

Example: Save a copy as WebP
```js
Image.load(PATH.public('img/photo.jpg'))
	.resize(1024)
	.quality(75)
	.save(PATH.public('img/photo.webp'), function(err, saved) {
		if (err)
			console.error(err);
	});
```

### image.pipe(stream, [type], [options])

Type: `method`

Runs the operations and pipes the result into a writable stream.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `stream` | `Stream` | yes |  | A writable stream, for example a file stream or `res`. |
| `type` | `String` | no | `image.outputType` | Output format. |
| `options` | `Object` | no |  | Options of `pipe()`. |

Returns: `Image` The same image, for chaining.

`type` is the output format: `jpg`, `jpeg`, `png`, `gif`, `apng`, `heif`, `heic`, `webp` or `ico`. Another value or none uses `image.outputType`. `options` are passed to `stream.pipe()`. Errors of the tool are emitted as `error` events of `stream`.

Example: Write to a file stream
```js
var target = Fs.createWriteStream(PATH.temp('thumb.png'));
Image.load(buffer).resize(64, 64).pipe(target, 'png');
```

### image.stream([type], [writer])

Type: `method`

Runs the operations and returns the result as a readable stream.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `type` | `String` | no | `image.outputType` | Output format. |
| `writer` | `Function(stdin)` | no |  | Writes the source data. |

Returns: `ReadableStream` The output of the tool (through the middleware, if one is registered).

`type` works as in `pipe()`. `writer(stdin)` can write the source into the tool yourself.

Example: Upload a resized image
```js
var stream = Image.load(filename).resize(500).stream('jpg');
stream.pipe(Fs.createWriteStream(PATH.temp('small.jpg')));
```

### default_image_consumption : 0

Type: `config`

Limits the memory of GraphicsMagick or ImageMagick, as a percentage.

Returns: `Number`

When it is not `0`, each image without its own `limit()` gets `-limit memory` (15 × value) and `-limit map` (30 × value). It is disabled by default because some versions of GraphicsMagick fail with these limits. A `%` sign in the config file is allowed.

Default: `0` (disabled).

Example: Half of the default limits
```text
default_image_consumption : 50
```
