Skip to content

Latest commit

 

History

History
555 lines (360 loc) · 14.2 KB

directives.md

File metadata and controls

555 lines (360 loc) · 14.2 KB

Table of Contents

Output Directives

Directives

Background

Keyword: background
Type: string

This instructs various directives (e.g. the rotate) to use the specified color when filling empty spots in the image.

The argument will be parsed by the color-string library so all color values known from css like rgb, rgba or named colors can be used.

NOTE: This directive does nothing on it's own, it has to be used in conjunction with another directive.
You also cannot set multiple values.

Example:

import Image from 'foo.jpg?background=#FFFFFFAA'
import Image from 'foo.jpg?background=hsl(360, 100%, 50%)'
import Image from 'foo.jpg?background=rgb(200, 200, 200)'
import Image from 'foo.jpg?background=blue'

Blur

Keyword: blur
Type: float| boolean

Blurs the image. When no argument is provided it performs a fast blur. When an argument between 0.3 and 1000 is provided it performs a more accurate gaussian blur.

Example:

import Image from 'example.jpg?blur'
import Image from 'exmaple.jpg?blur=0.75'
import Image from 'example.jpg?blur=100'

Effort

Keyword: effort
Type: integer | "max" | "min"

Adjust the effort to spend encoding the image. The effect of effort varies per format, but a lower value leads to faster encoding.

The supported ranges by format:

  • png: 1 to 10 (default 7)
  • webp: 0 to 6 (default 4)
  • avif/heif: 0 to 9 (default 4)
  • jxl: 3 to 9 (default 7)
  • gif: 1 to 10 (default 7)

The keywords "min" and "max" apply the highest effort value for the given image format.

Search options.effort in sharp's Output options documentation for details.

Example:

import highestEffortWebp from 'example.jpg?format=webp&effort=max'
import quicklyGeneratingAvif from 'example.jpg?format=avif&effort=0'

Fit

Keyword: fit
Type: cover | contain | fill | inside | outside

When both a width and height are provided, this directive can be used to specify the method by which the image should fit.

Example:

import Image from 'example.jpg?fit=cover'

Flatten

Keyword: flatten
Type: boolean

This directive will remove the alpha channel of the image, reducing filesize. Transparent pixels will be merged with the color set by the background directive.


Flip

Keyword: flip
Type: boolean

Flip the image about the vertical axis. This step is always performed after any rotation.

Example:

import Image from 'example.jpg?flip'
import Image from 'exmaple.jpg?flip=true'

Flop

Keyword: flop
Type: boolean

Flop the image about the horizontal axis. This step is always performed after any rotation.

Example:

import Image from 'example.jpg?flop'
import Image from 'exmaple.jpg?flop=true'

Format

Keyword: format
Type: jxl| heif | avif | jpeg | jpg | png | tiff | webp | gif

Convert the image into the given format.

NOTE: Converting to the gif format requires libvips compiled with support for ImageMagick or GraphicsMagick See The sharp docs for details.

Example:

import Image from 'example.jpg?format=webp'
import Images from 'example.jpg?format=webp;avif;jxl'

Grayscale

Keyword: grayscale
Type: boolean

Converts the image to an 8-bit grayscale image.

This directive will convert the image to the b-w colorspace, meaning the resulting image will only have one channel.

Example:

import Image from 'example.jpg?grayscale'
import Image from 'exmaple.jpg?grayscale=true'

Hue

Keyword: hue
Type: integer

Adjusts the images hue rotation by the given number of degrees. Commonly used together with saturation and brightness.


Saturation

Keyword: saturation
Type: float

Adjusts the images saturation with the given saturation multiplier. Commonly used together with hue and brightness.


Brightness

Keyword: brightness
Type: float

Adjusts the images brightness with the given brightness multiplier. Commonly used together with hue and saturation.


Invert

Keyword: invert
Type: boolean

Produces a 'negative' of the image.

Example:

import Image from 'example.jpg?invert'
import Image from 'exmaple.jpg?invert=true'

Kernel

Keyword: kernel
Type: nearest| cubic | mitchell | lanczos2 | lanczos3

Use this directive to set a different interpolation kernel when resizing the image.


Lossless

Keyword: lossless
Type: boolean

Use lossless compression mode.

Formats that support this directive are: avif, heif, jxl, and webp

Example:

import losslessWebp from 'example.jpg?format=webp&lossless';
import losslessAvif from 'example.jpg?format=avif&lossless=true';

Median

Keyword: median
Type: float| boolean

Applies a median filter. This is commonly used to remove noise from images.

Example:

import Image from 'example.jpg?median'
import Image from 'exmaple.jpg?median=3'
import Image from 'example.jpg?median=50'

Normalize

Keyword: normalize
Type: boolean

'Normalizes' the image by stretching its luminance to cover the full dynamic range. This Enhances the output image contrast.

Example:

import Image from 'example.jpg?normalize'
import Image from 'exmaple.jpg?normalize=true'

Position

Keyword: position
Type: top| right top | right | right bottom | bottom | left bottom | left | left top | north | northeast | east | southeast | south | southwest | west | northwest | center | centre | cover | entropy | attention

When both width and height are provided AND the fit is one of fit of cover or contain, this directive can be used to set the position of the image.

See sharps resize options for a detailed explanation of each.


Quality

Keyword: quality
Type: integer

All formats (except gif) allow the quality to be adjusted by setting this directive.

The argument must be a number between 1 and 100.

See sharps Output options for default quality values.

Example:

import Image from 'example.jpg?format=webp&quality=100'
import Images from 'example.jpg?format=avif&quality=10;50;75'

Width

Keyword: w
Type: integer

Resizes the image to be the specified amount of pixels wide. If not given the height will be scaled accordingly.

Example:

import Image from 'example.jpg?w=200'
import Images from 'example.jpg?w=200;400;700'

Height

Keyword: h
Type: integer

Resizes the image to be the specified amount of pixels tall. If not given the width will be scaled accordingly.

Example:

import Image from 'example.jpg?h=200'
import Images from 'example.jpg?h=200;400;700'

Aspect

Keyword: aspect
Type: string | number

Resizes the image to be the specified aspect ratio. Aspect ratio can be defined with a string in the form 16:9 or a positive number representing the width divided by height (e.g., 1.5 for a 3:2 aspect ratio) If height and width are both provided, this will be ignored. If height is provided, the width will be scaled accordingly. If width is provided, the height will be scaled accordingly. If neither height nor width are provided, the image will be cropped to the given aspect ratio.

Example:

import Image from 'example.jpg?aspect=16:9'
import Image from 'example.jpg?aspect=16:9&h=200'
import Image from 'example.jpg?aspect=16:9&w=200'
import Images from 'example.jpg?aspect=16:9&h=200;400;700'

Without Enlargement

Keyword: withoutEnlargement
Type: boolean

Prevents the image from being resized if the specified or calculated width or height are greater than the original width or height. Must be passed with a width, height or aspect directive.

Example:

import Image from 'example.jpg?w=200;400&withoutEnlargement'
import Image from 'example.jpg?aspect=16:9&withoutEnlargement'
import Images from 'example.jpg?aspect=16:9&h=200;400;700&withoutEnlargement'

Without Reduction

Keyword: withoutReduction
Type: boolean

Prevents the image from being resized if the specified or calculated width or height are less than the original width or height. Must be passed with a width, height or aspect directive.

Example:

import Image from 'example.jpg?height=300;600;900&withoutReduction'
import Image from 'example.jpg?aspect=9:16&withoutReduction'
import Images from 'example.jpg?aspect=16:9&h=200;400;700&withoutEnlargement&withoutReduction'

Rotate

Keyword: rotate
Type: integer

Rotate the image by the specified number of degrees.

NOTE: You can change the background color the empty parts are filled with by setting the background directive.

Example:

import Image from 'example.jpg`rotate=90'
import Image from 'example.jpg`rotate=68'
import Images from 'example.jpg`rotate=90;180;270'

Tint

Keyword: tint
Type: string

Tints the image using the provided chroma while preserving the image luminance. If the image has an alpha channel it will be untouched.

Example:

import Image from 'example.jpg?tint=#ffaa22'
import Image from 'example.jpg?tint=rgba(10,33,127)'

Metadata

Keyword: metadata | meta
Type: boolean | string[]

Returns all information collected about the image as a JavaScript object. The directive optionally takes a list of arguments that act as a whitelist for the metadata object. You can use it to only import specific image attributes, keeping your bundle size small.

Example:

import Image from 'example.jpg?w=500;900;1200&avif&metadata'
import { height, format } from 'example.jpg?w=700&format=gif&as=meta:height;format'

Picture

Keyword: picture
Type: boolean

Returns information about the image necessary to render a picture tag as a JavaScript object.

Example:

import picture from 'example.jpg?w=500;900;1200&format=avif;webp;jpg&as=picture'

let html = '<picture>';
for (const [format, images] of Object.entries(picture.sources)) {
  html += `<source srcset={images.map((i) => `${i.src}`).join(', ')} type={'image/' + format} />`;
}
html += `<img src={picture.img.src} /></picture>`

Source

Keyword: source
Type: boolean

Returns information about the image necessary to render a source tag as a JavaScript object. This only takes the image width into consideration.

Example:

import avif from 'example.jpg?w=500;900;1200&format=avif&as=source'
import webp from 'example.jpg?w=500;900;1200&format=webp&as=source'
import fallback from 'example.jpg?w=700'

const html = `<picture>
    <source srcset="${avif.map(({src, w}) => `${src} ${w}w`).join(',')}" type="image/avif" />
    <source srcset="${webp.map(({src, w}) => `${src} ${w}w`).join(',')}" type="image/webp" />
    <img src="${fallback}" />
</picture>

Srcset

Keyword: srcset
Type: boolean

Returns a srcset string of the generated images to be used in a <picture> tag. This only takes the image width into consideration.

Example:

import avif from 'example.jpg?w=500;900;1200&format=avif&as=srcset'
import webp from 'example.jpg?w=500;900;1200&format=webp&as=srcset'
import fallback from 'example.jpg?w=700'

const html = `<picture>
    <source srcset="${avif}" type="image/avif" />
    <source srcset="${webp}" type="image/webp" />
    <img src="${fallback}" />
</picture>
`

URL

Keyword: url
Type: boolean

Returns a URL to the generated image. This is the default when your directives produce only one output image.

Example:

import Image from 'example.jpg?w=500' // the type of Image is a string and it's a URL to the transformed image

Inline

Keyword: inline
Type: boolean

Return base64 encoded inline image instead of URL to the generated image. This can be combined with some output directives like srcset or picture.

Example:

import inlineImage from 'example.jpg?format=webp&inline';

The returned inlineImage will start with data:image/webp;base64,...