---
title: Images
project: Total.js V5
version: 0.0.20
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/total-js-v5/0-0-20/reference/images/
updated: 2026-11-18T16:57:34.578Z
---

# Images

> The Image global and every method of image objects.

The `Image` global loads, changes, measures and saves images with GraphicsMagick or ImageMagick. The guide [Images](../../guides/images/) explains how to use it with examples; this page is the exact reference.

## API

### Image

Type: `class`

The global image module.

`Image` is the image module of the framework. It has `Image.load()` to create image objects, the `Image.measure*()` functions that read the size from a file header, `Image.middleware()` and the constructor `Image.Image`. The operations run with the program set in `$imageconverter`.

Example: Resize a photo
```js
Image.load(PATH.public('photo.jpg')).resize(800).save(PATH.public('photo-800.jpg'));
```

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

Type: `method`

Creates an image object.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `source` | `String|Buffer|Stream` | yes |  | A file name, a Buffer or a readable stream. |
| `converter` | `String` | no | `CONF.$imageconverter` | The program: `gm`, `im` or `magick`. |
| `width` | `Number` | no |  | The known width, stored in `img.width`. |
| `height` | `Number` | no |  | The known height, stored in `img.height`. |

Returns: `Image` A new image object.

`source` is a file name, a `Buffer` or a readable stream. With a file name, `img.type` is its extension; otherwise it is `jpg`. `converter` is `gm`, `im` or `magick` and overrides `$imageconverter` for this image. `width` and `height` are only stored in `img.width` and `img.height`; they are not read from the file.

Example: Load from a file and a buffer
```js
var a = Image.load(PATH.public('photo.jpg'));
var b = Image.load(buffer, 'magick');
```

### F.image(source, [converter], [width], [height])

Type: `method`

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

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `source` | `String|Buffer|Stream` | yes |  | A file name, a Buffer or a readable stream. |
| `converter` | `String` | no | `CONF.$imageconverter` | The program: `gm`, `im` or `magick`. |
| `width` | `Number` | no |  | The known width. |
| `height` | `Number` | no |  | The known height. |

Returns: `Image` A new image object.

An alias of `Image.load()` on the framework object.

Example: Load an image
```js
F.image(filename).grayscale().save(target);
```

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

Type: `class`

The constructor of image objects.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `source` | `String|Buffer|Stream` | yes |  | A file name, a Buffer or a readable stream. |
| `converter` | `String` | no | `CONF.$imageconverter` | The program: `gm`, `im` or `magick`. |
| `width` | `Number` | no |  | The known width. |
| `height` | `Number` | no |  | The known height. |

`Image.load()` returns `new Image.Image(...)` with the same arguments. Use it when you want to test `img instanceof Image.Image` or add your own methods to `Image.Image.prototype`.

Example: Add a method
```js
Image.Image.prototype.avatar = function() {
	return this.resizecenter(128, 128).quality(90);
};

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

### Image.measure(type, buffer)

Type: `method`

Reads the width and height from the header of an image.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `type` | `String` | yes |  | An extension or a MIME type. |
| `buffer` | `Buffer` | yes |  | The beginning of the file. |

Returns: `Object` `{ width, height }`, `null` for a JPEG without the size marker, `undefined` for an unsupported type.

`type` is an extension with or without the dot (`jpg`, `.png`) or a MIME type (`image/webp`). Supported types are JPEG, PNG, GIF, WebP, SVG, BMP and PSD. The buffer needs only the beginning of the file. For other types it returns `undefined`. For a JPEG buffer without the size marker it returns `null`.

Example: Measure an uploaded buffer
```js
var size = Image.measure('image/png', buffer);
if (size && size.width > 2000)
	$.invalid('The image is too wide');
```

### Image.measureJPG(buffer)

Type: `method`

Reads the size of a JPEG image from a buffer.

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

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

It scans the markers until the frame header. Returns `null` when the buffer is not a JPEG or when the frame header is not in the buffer (a damaged file, a segment with an invalid length or too little data). Pass enough data: the framework reads 40 kB.

Example: Measure
```js
var size = Image.measureJPG(buffer);
console.log(size.width, size.height);
```

Example: A JPEG without the size
```js
var buffer = Buffer.from([0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00]);
var size = Image.measureJPG(buffer);
console.log(size); // null: no frame header in the buffer
```

### Image.measurePNG(buffer)

Type: `method`

Reads the size of a PNG image from a buffer.

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

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

It reads the IHDR header, so 24 bytes are enough.

Example: Measure
```js
var size = Image.measurePNG(buffer);
console.log(size.width, size.height);
```

### Image.measureGIF(buffer)

Type: `method`

Reads the size of a GIF image from a buffer.

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

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

It reads the logical screen size, so 10 bytes are enough.

Example: Measure
```js
var size = Image.measureGIF(buffer);
console.log(size.width, size.height);
```

### Image.measureWEBP(buffer)

Type: `method`

Reads the size of a WebP image from a buffer.

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

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

It reads the `VP8 `, `VP8L` and `VP8X` headers. Returns `undefined` for other data.

Example: Measure
```js
var size = Image.measureWEBP(buffer);
console.log(size.width, size.height);
```

### Image.measureSVG(buffer)

Type: `method`

Reads the size of a SVG image from a buffer.

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

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

It reads the first `width="..."` and `height="..."` attributes with whole numbers. Returns `undefined` when there is none.

Example: Measure
```js
var size = Image.measureSVG(buffer);
console.log(size.width, size.height);
```

### Image.measureBMP(buffer)

Type: `method`

Reads the size of a BMP image from a buffer.

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

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

It reads the BITMAPINFOHEADER.

Example: Measure
```js
var size = Image.measureBMP(buffer);
console.log(size.width, size.height);
```

### Image.measurePSD(buffer)

Type: `method`

Reads the size of a Photoshop (PSD) image from a buffer.

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

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

It reads the PSD file header.

Example: Measure
```js
var size = Image.measurePSD(buffer);
console.log(size.width, size.height);
```

### Image.middleware(type, fn)

Type: `method`

Registers a stream that post-processes results of one type.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `type` | `String` | yes |  | The image type, for example `png` or `.jpg`. |
| `fn` | `Function` | yes |  | Returns a Transform stream. |

`fn` must return a new Transform stream each time it is called. With `pipe()` and `writer()` the output of the program goes through it before it reaches you, and `type` is matched with the output format. With `save()` and a callback, the saved file is read through it and written again before the callback runs; there `type` is matched with `img.type` (the extension of the source or the value of `output()`). A leading dot in `type` is removed. One function per type; a new call replaces it.

Example: Pass PNG results through a stream
```js
var Transform = require('stream').Transform;

Image.middleware('png', function() {
	return new Transform({
		transform: function(chunk, encoding, next) {
			next(null, chunk);
		}
	});
});
```

### img.width

Type: `property`

The width passed to `Image.load()`.

Returns: `Number`

The third argument of `Image.load()`, or `undefined`. It is not read from the file; use `img.measure()` for that. `img.height` holds the fourth argument the same way.

Example: Known size
```js
var img = Image.load(file.path, null, file.width, file.height);
if (img.width > 1000)
	img.resize(1000);
```

### img.filename

Type: `property`

The source file name.

Returns: `String`

The file name given to `Image.load()`, or `null` when the source is a Buffer or a stream.

### img.stream

Type: `property`

The source Buffer or stream.

Returns: `Buffer|Stream`

The Buffer or readable stream given to `Image.load()`, or `null` when the source is a file name. It is written to the standard input of the program when the image is saved or piped.

### img.type

Type: `property`

The image type.

Returns: `String`

The extension of the source file in lower case without the dot (`jpg`, `png`…), or `jpg` for a Buffer or a stream. `output()` changes it. `pipe()` and `writer()` use it as the output format when you do not pass one, and `Image.middleware()` handlers are matched with it.

### img.shell

Type: `property`

The program that processes the image.

Returns: `String`

`gm`, `im` or `magick`: the second argument of `Image.load()`, otherwise `CONF.$imageconverter`. `gm` runs `gm convert`, `im` runs `convert` and `magick` runs `magick`. You can change it before you save the image.

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

Type: `method`

Resizes the image.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `w` | `Number|String` | no |  | The width in pixels, or a percentage like `'50%'`. Empty to keep the ratio. |
| `h` | `Number|String` | no |  | The height in pixels, or a percentage. Empty to keep the ratio. |
| `options` | `String` | no |  | A suffix of the size: `!` ignores the ratio, `^` fills the box, `>` only shrinks, `<` only enlarges. |

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

Adds `-resize WxH`. The image fits into the box and keeps its aspect ratio unless `options` says otherwise. With only `w` the height follows the ratio, with only `h` the width does.

Example: Resize
```js
img.resize(800, 600);   // into 800x600
img.resize(800);        // width 800
img.resize(null, 600);  // height 600
img.resize(200, 200, '!'); // exactly 200x200
img.resize('50%');      // half size
```

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

Type: `method`

Resizes the image for a small preview.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `w` | `Number|String` | no |  | The width in pixels, or a percentage like `'50%'`. Empty to keep the ratio. |
| `h` | `Number|String` | no |  | The height in pixels, or a percentage. Empty to keep the ratio. |
| `options` | `String` | no |  | A suffix of the size: `!` ignores the ratio, `^` fills the box, `>` only shrinks, `<` only enlarges. |

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

Adds `-thumbnail WxH`: a resize that also removes the profiles, so the file is smaller. The arguments are the same as in `resize()`.

Example: Thumbnail
```js
Image.load(filename).thumbnail(150, 150).save(target);
```

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

Type: `method`

Resizes the image quickly without filtering.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `w` | `Number|String` | no |  | The width in pixels, or a percentage like `'50%'`. Empty to keep the ratio. |
| `h` | `Number|String` | no |  | The height in pixels, or a percentage. Empty to keep the ratio. |
| `options` | `String` | no |  | A suffix of the size: `!` ignores the ratio, `^` fills the box, `>` only shrinks, `<` only enlarges. |

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

Adds `-scale WxH`. Faster than `resize()` but with lower quality. The arguments are the same as in `resize()`.

Example: Scale
```js
img.scale(64, 64);
```

### img.geometry([w], [h], [options])

Type: `method`

Adds the `-geometry` option.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `w` | `Number|String` | no |  | The width in pixels, or a percentage like `'50%'`. Empty to keep the ratio. |
| `h` | `Number|String` | no |  | The height in pixels, or a percentage. Empty to keep the ratio. |
| `options` | `String` | no |  | A suffix of the size: `!` ignores the ratio, `^` fills the box, `>` only shrinks, `<` only enlarges. |

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

Adds `-geometry WxH` with the optional suffix. The program uses it as the preferred size and offset.

Example: Geometry
```js
img.geometry(300, 200, '!');
```

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

Type: `method`

Cuts an area of the image.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `w` | `Number` | yes |  | The width of the area. |
| `h` | `Number` | yes |  | The height of the area. |
| `x` | `Number` | no | `0` | The horizontal offset. |
| `y` | `Number` | no | `0` | The vertical offset. |

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

Adds `-crop WxH+X+Y`. The offset is measured from the corner set by `align()` (top left by default).

Example: Crop
```js
img.crop(400, 300, 50, 20);

// The centre
img.align('center').crop(400, 300);
```

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

Type: `method`

Sets the size of the canvas.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `w` | `Number` | no |  | The width of the canvas. |
| `h` | `Number` | no |  | The height of the canvas. |
| `x` | `Number` | no |  | The horizontal offset, can be negative. |
| `y` | `Number` | no |  | The vertical offset, can be negative. |

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

Adds `-extent WxH` with an optional offset. A larger canvas is filled with the background color, a smaller one cuts the image. The position follows `align()`.

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

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

Type: `method`

Fits the image into a box and fills the rest.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `w` | `Number` | yes |  | The width. |
| `h` | `Number` | yes |  | The height. |
| `color` | `String` | no | `'white'` | The color of the free area. |
| `filter` | `String` | no | `'Hamming'` | The resize filter. |

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

A shortcut for `thumbnail(w, h).background(color).align('center').extent(w, h)`. The result is exactly `w`x`h` and shows the whole image. `filter` is the resize filter of `thumbnail()`.

Example: Product photo
```js
Image.load(filename).miniature(300, 300, '#F0F0F0').save(target);
```

### img.resizecenter(w, h, [color])

Type: `method`

Fills a box and crops the centre.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `w` | `Number` | yes |  | The width. |
| `h` | `Number` | yes |  | The height. |
| `color` | `String` | no | `'white'` | The background color. |

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

A shortcut for `resize(w, h, '^').background(color).align('center').crop(w, h)`. The result is `w`x`h` and the image covers it completely.

Example: Cover
```js
Image.load(filename).resizecenter(1200, 630).save(target);
```

### img.resizealign(w, h, [align], [color])

Type: `method`

Fills a box and crops at a side.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `w` | `Number` | yes |  | The width. |
| `h` | `Number` | yes |  | The height. |
| `align` | `String` | no | `'center'` | Where to crop: `top`, `bottom`, `left`, `center`… |
| `color` | `String` | no | `'white'` | The background color. |

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

A shortcut for `resize(w, h, '^').background(color).align(align).crop(w, h)`. `align` takes the values of `align()`.

Example: Avatar
```js
Image.load(filename).resizealign(200, 200, 'top').save(target);
```

### img.align(type)

Type: `method`

Sets the gravity for crop and extent.

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

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

Adds `-gravity`. Accepted values: `'top left'` (NorthWest), `'top'` (North), `'top right'` (NorthEast), `'left'` (West), `'center'` or `'middle'` (Center), `'right'` (East), `'bottom left'` (SouthWest), `'bottom'` (South), `'bottom right'` (SouthEast). Word order does not matter (`'left top'`), and `'center left'` means `left`. Other values are passed as they are, for example `'NorthEast'`.

Example: Align
```js
img.align('bottom right').crop(400, 300);
```

### img.gravity(type)

Type: `method`

The same method as `align()`.

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

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

An alias of `align()`.

Example: Gravity
```js
img.gravity('NorthWest');
```

### img.quality([percentage])

Type: `method`

Sets the compression quality.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `percentage` | `Number` | no | `80` | The quality. |

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

Adds `-quality`. For JPEG and WebP it is the quality from 1 to 100; an empty value means 80.

Example: Quality
```js
img.quality(70);
```

### img.rotate([deg])

Type: `method`

Rotates the image.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `deg` | `Number` | no | `0` | The angle in degrees. |

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

Adds `-rotate`. The angle is in degrees clockwise. For angles other than multiples of 90 the corners are filled with the background color.

Example: Rotate
```js
img.background('white').rotate(45);
```

### img.flip()

Type: `method`

Mirrors the image vertically.

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

Adds `-flip` (top to bottom).

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

### img.flop()

Type: `method`

Mirrors the image horizontally.

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

Adds `-flop` (left to right).

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

### img.blur(radius)

Type: `method`

Blurs the image.

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

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

Adds `-blur`. The value is the radius, or `radiusxsigma` as a string (`'0x8'`).

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

### img.grayscale()

Type: `method`

Converts the image to gray.

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

Adds `-colorspace Gray`.

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

### img.sepia()

Type: `method`

Gives the image a sepia tone.

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

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

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

### img.normalize()

Type: `method`

Stretches the contrast.

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

Adds `-normalize`: the colors are spread over the full range.

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

### img.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 object, for chaining.

Adds `-colors`.

Example: Palette
```js
img.output('png').colors(64);
```

### img.bitdepth(value)

Type: `method`

Sets the bit depth.

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

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

Adds `-depth`.

Example: Depth
```js
img.bitdepth(8);
```

### img.background(color)

Type: `method`

Sets the background color.

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

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

Adds `-background color` and then `-extent 0x0`, so the color is set before the canvas is extended. The color fills new areas made by `extent()`, `rotate()` and the resize helpers. Any color of the program works: `white`, `#FF0000`, `none` for transparent.

Example: Background
```js
img.background('#FFFFFF').extent(800, 800);
```

### img.fill(color)

Type: `method`

Sets the fill color.

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

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

Adds `-fill`. Used by operations that draw, for example `-colorize` or your own `command()` options.

Example: Fill
```js
img.fill('red').command('-colorize', '30');
```

### img.filter(type)

Type: `method`

Sets the resize filter.

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

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

Adds `-filter`, for example `'Lanczos'`, `'Hamming'`, `'Box'` or `'Triangle'`. It is always placed before `-resize`, `-thumbnail` and `-scale`, so it applies to them whether you call it before or after `resize()`, `thumbnail()` or `scale()`.

Example: Filter
```js
img.resize(400).filter('Lanczos');
```

Example: The command
```js
Image.load('photo.jpg').filter('Lanczos').resize(200, 200).cmd('photo.jpg', 'small.jpg');
// gm convert 'photo.jpg' -filter 'Lanczos' -resize '200x200' 'small.jpg'
```

### img.define(value)

Type: `method`

Passes a coder option.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String` | yes |  | The definition `format:key=value`. |

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

Adds `-define`, for example `'jpeg:extent=200kb'` or `'png:compression-level=9'`.

Example: Define
```js
img.define('jpeg:extent=200kb');
```

### img.minify()

Type: `method`

Removes the profiles and metadata.

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

Adds `+profile '*'`, which removes EXIF, ICC and other profiles. `save()`, `pipe()` and `writer()` call it when the image has no operations.

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

### img.limit(type, value)

Type: `method`

Limits a resource of the program.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `type` | `String` | yes |  | The resource. |
| `value` | `Number|String` | yes |  | The limit, for example `256MB`. |

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

Adds `-limit type value`, for example `limit('memory', '256MB')`. Types and units are those of the program (`memory`, `map`, `disk`…). A number without a unit is in bytes.

Example: Limit memory
```js
img.limit('memory', '256MB').limit('map', '512MB');
```

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

Type: `method`

Draws another image over the picture.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `filename` | `String` | yes |  | The file of the watermark. |
| `x` | `Number` | no | `0` | The horizontal offset. |
| `y` | `Number` | no | `0` | The vertical offset. |
| `w` | `Number` | no | `0` | The width of the watermark. |
| `h` | `Number` | no | `0` | The height of the watermark. |

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

Adds `-draw 'image over x,y w,h filename'`. The offset is measured from the top left corner. A width and height of 0 keep the size of the watermark. Single quotes are removed from the file name.

Example: Logo in the corner
```js
Image.load(filename)
	.watermark(PATH.public('img/logo.png'), 20, 20, 120, 40)
	.save(target);
```

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

Type: `method`

Adds any option of the program.

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

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

Adds `key value` to the command. `priority` decides the position (lower first, default 10; sizes have 1, gravity 3, crop 4, quality 5, rotate 8). `escape` puts the value in quotes; passing `true` as `priority` also turns on `escape` (with priority 10).

Example: Custom options
```js
img.command('-sharpen', '0x1');
img.command('-strip');
img.command('-label', 'My photo', true);
```

### img.make(fn)

Type: `method`

Applies a function to the image.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function` | yes |  | Gets the image. |

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

Calls `fn(img)` with `this` set to the image. Use it to reuse a set of operations.

Example: Preset
```js
function avatar(img) {
	img.resizecenter(128, 128).quality(90);
}

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

### img.clear()

Type: `method`

Removes all operations.

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

Empties the list of operations. `save()` calls it after the program finishes.

Example: Clear
```js
img.clear().resize(100);
```

### img.output(type)

Type: `method`

Sets the image type.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `type` | `String` | yes |  | The type, for example `png` or `.webp`. |

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

Sets `img.type` (a leading dot is removed). `pipe()` and `writer()` produce this format when you do not pass one, and `Image.middleware()` is matched with it. `save()` takes the format from the extension of the target file.

Example: Buffer to PNG
```js
Image.load(buffer).output('png').resize(100).pipe($.res);
```

### img.save([filename], [callback])

Type: `method`

Runs the program and writes the result to a file.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `filename` | `String` | no | `img.filename` | The target file. |
| `callback` | `Function` | no |  | `function(err, success)`. |

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

Builds the command and runs it. The format comes from the extension of `filename`. Without `filename` the source file is overwritten. A Buffer or stream source is sent to the standard input. The callback gets `(err, success)`, where `success` is `true` or `false`. The operations are cleared afterwards.

Example: Save
```js
Image.load(file.path).resize(1024).quality(80).save(PATH.public('photos/' + id + '.jpg'), function(err) {
	if (err)
		$.invalid(err);
	else
		$.success();
});
```

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

Type: `method`

Runs the program and sends the result to a stream.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `stream` | `Stream` | yes |  | A writable stream, for example `$.res`. |
| `type` | `String` | no | `img.type` | The output format. |
| `options` | `Object` | no |  | Options of `stream.pipe()`. |

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

Writes the output of the program to `stream`. `type` is the output format: `jpg`, `jpeg`, `png`, `gif`, `apng`, `webp`, `heif`, `heic` or `ico`; other values use `img.type`. `options` goes to `stream.pipe()` (for example `{ end: false }`); you can pass it as the second argument. Messages of the program on the standard error are emitted as `error` events on `stream`.

Example: Send to the response
```js
function preview($) {
	$.res.writeHead(200, { 'content-type': 'image/png' });
	Image.load(PATH.public('photo.jpg')).resize(300).pipe($.res, 'png');
}
```

### img.writer([type])

Type: `method`

Runs the program and returns the output stream.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `type` | `String` | no | `img.type` | The output format. |

Returns: `Stream` A readable stream with the result.

Starts the program and returns its readable output (passed through the `Image.middleware()` of the type, when there is one). `type` takes the same values as in `pipe()`.

Example: Collect into a Buffer
```js
var chunks = [];
var stream = Image.load(buffer).resize(100, 100).writer('webp');
stream.on('data', chunk => chunks.push(chunk));
stream.on('end', function() {
	var output = Buffer.concat(chunks);
});
```

### img.measure(callback)

Type: `method`

Reads the size of the source file.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `callback` | `Function` | yes |  | `function(err, size)`. |

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

Reads the header of the source file and calls `callback(err, size)` with `{ width, height }`. For a `.jpg` file without the frame header in its first 40 kB, `size` is `null`. The source must be a file name with the extension `.jpg`, `.png`, `.gif` or `.webp`; other extensions give an error. It does not use the external program.

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

### img.cmd(from, to)

Type: `method`

Returns the command line without running it.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `from` | `String` | yes |  | The source file or `-`. |
| `to` | `String` | yes |  | The target file or `-`. |

Returns: `String` The command line.

Builds the command used by `save()`: the program, the quoted source, the sorted operations and the quoted target. Use `'-'` for the standard input or output. Useful for debugging.

Example: Show the command
```js
var img = Image.load('photo.jpg').resize(200, 100);
console.log(img.cmd('photo.jpg', 'small.jpg'));
// gm convert 'photo.jpg' -resize '200x100' 'small.jpg'
```
