Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

ipx

unjs1.6mMIT3.0.3TypeScript support: included

High performance, secure and easy-to-use image optimizer.

readme

🖼️ IPX

npm version npm downloads

[!NOTE] This is the active branch for IPX v2. Check out ipx/v1 for v1 docs.

High performance, secure and easy-to-use image optimizer powered by sharp and svgo.

Used by Nuxt Image and Netlify and open to everyone!

Using CLI

You can use ipx command to start server.

Using npx:

npx ipx serve --dir ./

Usin bun

bun x npx ipx serve --dir ./

The default serve directory is the current working directory.

Programatic API

You can use IPX as a middleware or directly use IPX interface.

import { createIPX, ipxFSStorage, ipxHttpStorage } from "ipx";

const ipx = createIPX({
  storage: ipxFSStorage({ dir: "./public" }),
  httpStorage: ipxHttpStorage({ domains: ["picsum.photos"] }),
});

Example: Using with unjs/h3:

import { listen } from "listhen";
import { createApp, toNodeListener } from "h3";
import {
  createIPX,
  ipxFSStorage,
  ipxHttpStorage,
  createIPXH3Handler,
} from "ipx";

const ipx = createIPX({
  storage: ipxFSStorage({ dir: "./public" }),
  httpStorage: ipxHttpStorage({ domains: ["picsum.photos"] }),
});

const app = createApp().use("/", createIPXH3Handler(ipx));

listen(toNodeListener(app));

Example: Using express:

import { listen } from "listhen";
import express from "express";
import {
  createIPX,
  ipxFSStorage,
  ipxHttpStorage,
  createIPXNodeServer,
} from "ipx";

const ipx = createIPX({
  storage: ipxFSStorage({ dir: "./public" }),
  httpStorage: ipxHttpStorage({ domains: ["picsum.photos"] }),
});

const app = express().use("/", createIPXNodeServer(ipx));

listen(app);

URL Examples

Get original image:

/_/static/buffalo.png

Change format to webp and keep other things same as source:

/f_webp/static/buffalo.png

Automatically convert to a preferred format (avif/webp/jpeg). Uses the browsers accept header to negotiate:

/f_auto/static/buffalo.png

Keep original format (png) and set width to 200:

/w_200/static/buffalo.png

Resize to 200x200px using embed method and change format to webp:

/embed,f_webp,s_200x200/static/buffalo.png

Config

You can universally customize IPX configuration using IPX_* environment variables.

  • IPX_ALIAS

    • Default: {}

Filesystem Source Options

(enabled by default with CLI only)

IPX_FS_DIR

  • Default: . (current working directory)

IPX_FS_MAX_AGE

  • Default: 300

HTTP(s) Source Options

(enabled by default with CLI only)

IPX_HTTP_DOMAINS

  • Default: []

IPX_HTTP_MAX_AGE

  • Default: 300

IPX_HTTP_FETCH_OPTIONS

  • Default: {}

IPX_HTTP_ALLOW_ALL_DOMAINS

  • Default: false

Modifiers

Property Docs Example Comments
width / w Docs /width_200/buffalo.png
height / h Docs /height_200/buffalo.png
resize / s Docs /s_200x200/buffalo.png
kernel Docs /s_200x200,kernel_nearest/buffalo.png Supported kernel: nearest, cubic, mitchell, lanczos2 and lanczos3 (default).
fit Docs /s_200x200,fit_outside/buffalo.png Sets fit option for resize.
position / pos Docs /s_200x200,pos_top/buffalo.png Sets position option for resize.
trim Docs /trim_100/buffalo.png
extend Docs /extend_{top}_{right}_{bottom}_{left}/buffalo.png Extend / pad / extrude one or more edges of the image with either the provided background colour or pixels derived from the image.
background / b _ /r_45,b_00ff00/buffalo.png
extract Docs /extract_{left}_{top}_{width}_{height}/buffalo.png Extract/crop a region of the image.
format / f Docs /format_webp/buffalo.png Supported format: jpg, jpeg, png, webp, avif, gif, heif, tiff and auto (experimental only with middleware)
quality / q _ /quality_50/buffalo.png Accepted values: 0 to 100
rotate Docs /rotate_45/buffalo.png
enlarge _ /enlarge,s_2000x2000/buffalo.png Allow the image to be upscaled. By default the returned image will never be larger than the source in any dimension, while preserving the requested aspect ratio.
flip Docs /flip/buffalo.png
flop Docs /flop/buffalo.png
sharpen Docs /sharpen_30/buffalo.png
median Docs /median_10/buffalo.png
blur Docs /blur_5/buffalo.png
gamma Docs /gamma_3/buffalo.png
negate Docs /negate/buffalo.png
normalize Docs /normalize/buffalo.png
threshold Docs /threshold_10/buffalo.png
tint Docs /tint_1098123/buffalo.png
grayscale Docs /grayscale/buffalo.png
animated - /animated/buffalo.gif Experimental

License

MIT

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

v3.0.3

compare changes

v3.0.2

compare changes

🩹 Fixes

  • Correctly handle format_auto,animated (#235)

📖 Documentation

  • Added jsdocs to exported functions and types (#222)

🏡 Chore

❤️ Contributors

v3.0.1

compare changes

v3.0.1-0

compare changes

🩹 Fixes

  • http: Properly respect ignoreCacheControl option (96a8489)

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)

v3.0.0

compare changes

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)

v2.1.0

compare changes

🚀 Enhancements

  • node-fs: Add support for multiple dirs (#203)

🩹 Fixes

  • Missing maxAge default (#197)
  • server: Set cache-control header only after processing image (#200)
  • Improve data parsing for unstorage (#204)

💅 Refactors

  • Jpeg progressive setting via sharpOptions (#198)

📦 Build

  • ipx-nightly release channel (#191)

🏡 Chore

🤖 CI

  • Fix nightly release job conditional (#195)

❤️ Contributors

v2.0.2

compare changes

v2.0.1

compare changes

🩹 Fixes

  • svgo: Handle javascript uris in removexss plugin (#186)

🏡 Chore

❤️ Contributors

v2.0.0

compare changes

🚀 Enhancements

  • http: Allow ignoring cache-control header via ignoreCacheControl (4690342)
  • Optimize + sanitize svg sources with svgo (#180)

🩹 Fixes

  • Respect global maxAge option as fallback (2abe014)
  • server: Improve 304 handling (06820b5)
  • server: Append vary header instead of overriding it (fb3cf1d)
  • server: Set headers only if not already set (ce0cf0e)

💅 Refactors

  • Upgrade to image-meta 0.2.x (1017deb)

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)

v2.0.0-1

compare changes

🚀 Enhancements

  • Use ofetch to improve http error handling (ac30512)

💅 Refactors

📖 Documentation

🏡 Chore

❤️ Contributors

v2.0.0-0

compare changes

🚀 Enhancements

  • cli: ⚠️ Rewrite with citty (#167)
  • Reimplement server with h3 (#168)

💅 Refactors

  • ⚠️ Rewrite storage system (#164)

📖 Documentation

🏡 Chore

⚠️ Breaking Changes

  • cli: ⚠️ Rewrite with citty (#167)
  • ⚠️ Rewrite storage system (#164)

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.3.0

compare changes

🚀 Enhancements

  • handlers: Support kernel modifier (#143)

🩹 Fixes

  • Provide options to sharp method (#138)
  • Add heic to the supported formats (#130)

📖 Documentation

  • Mention background modifier (#156)
  • Update background modifier example (2c6ad22)
  • Add h3 middleware example (#144)

📦 Build

  • Update exports field for type support (f43772b)

🏡 Chore

  • Fix internal typo (#148)
  • Update dependencies (5b34193)
  • Replace nodemon with listhen (a008152)

🎨 Styles

  • Format with prettier v3 (4c5e8bb)

🤖 CI

❤️ Contributors

v1.2.0

compare changes

🚀 Enhancements

  • Support experimental auto format (#85)
  • middleware: Add fallthrough option to handle error with next callback (#116)
  • Support working extract modifier` (#114)

🩹 Fixes

  • middleware: Sanitize double backslashes and quotes (#115)
  • middleware: Handle multple argument modifiers (e4ef303)

💅 Refactors

  • Enable strict typechecks (#133)

🏡 Chore

❤️ Contributors

v1.1.0

compare changes

🚀 Enhancements

  • Support sigma parameter for blur operation (#124)

🏡 Chore

❤️ Contributors

v1.0.1

compare changes

v1.0.0

compare changes

🏡 Chore

🎨 Styles

❤️ Contributors

  • Pooya Parsa (@pi0)

1.0.0-2 (2022-11-24)

1.0.0-1 (2022-11-23)

Bug Fixes

1.0.0-0 (2022-11-23)

Bug Fixes

  • use utc format for Last-Modified header (#89) (1cb0b6d)

0.9.11 (2022-09-03)

Features

  • middleware: add Content-Security-Policy header (#83) (d1edbf1)

Bug Fixes

  • use hasProtocol rather than checking if url starts with http (#80) (696ba5a)

0.9.10 (2022-07-07)

Bug Fixes

  • return promise from middleware (2fb644d)

0.9.9 (2022-06-22)

Bug Fixes

  • http: handle domains without protocol and port (a5b4614)

0.9.8 (2022-06-22)

Bug Fixes

  • http: use hostname to compare against domains (3aabc41)

0.9.7 (2022-06-22)

Bug Fixes

  • fs: fix windows path validation (c631a2b)

0.9.6 (2022-06-20)

0.9.5 (2022-06-20)

Features

Bug Fixes

  • improve path validation (resolves #56) (ec5c15d)
  • middleware: sanetize request and response strings (resolves #42) (1792d3a)
  • use response.arrayBuffer instead of deprecated res.buffer (b13a77e), closes #69

0.9.4 (2022-02-17)

Bug Fixes

0.9.3 (2022-02-15)

Features

Bug Fixes

  • allow resize operator with only width (4662f4e)

0.9.2 (2022-01-31)

Bug Fixes

  • use whatwg-url for parsing hostname (a5ee0b5)

0.9.1 (2021-11-05)

Bug Fixes

0.9.0 (2021-10-27)

⚠ BREAKING CHANGES

  • Sharp is being lazy loaded
  • Several dependencies changes for better ESM compatibility

Features

Bug Fixes

  • update image-meta import (a278cf1)
  • use ohmyfetch for cjs support (39e78b9)

0.8.0 (2021-09-05)

⚠ BREAKING CHANGES

0.7.2 (2021-07-26)

Features

0.7.1 (2021-07-02)

Bug Fixes

  • handle background number (2f82a56)
  • resize with width and hight (3ca70a0)
  • support background with rotate (b6c8f8c)

0.7.0 (2021-07-01)

⚠ BREAKING CHANGES

  • pkg: add exports field
  • move modifiers to path from query

Features

  • reqOptions and bypassDomain (fc8c7b5)
  • pkg: add exports field (394384f)
  • move modifiers to path from query (b7570d9)

Bug Fixes

  • don't prepend trailing slash to external id (01e151a)

0.6.7 (2021-07-01)

Bug Fixes

  • middleware: set res.body (d7dc146)

0.6.6 (2021-07-01)

0.6.5 (2021-07-01)

Features

Bug Fixes

  • filesystem: handle when input is not a file (9e1f7bf)

0.6.4 (2021-07-01)

Bug Fixes

  • enforce leadingSlash for alias resolution (3092e00)

0.6.3 (2021-06-30)

Bug Fixes

0.6.2 (2021-06-29)

Features

Bug Fixes

  • apply context modifiers first (resolves #33) (cf9effd)
  • default modifiers to empty object (00d5c1d)

0.6.1 (2021-05-26)

0.6.0 (2021-02-15)

⚠ BREAKING CHANGES

  • improved handlers and format support

Features

  • improved handlers and format support (f4f6e58)

0.5.8 (2021-02-08)

Bug Fixes

  • ipx: handle when modifiers not provided (4efebd8)

0.5.7 (2021-02-08)

Bug Fixes

  • override meta.type and meta.mimeType if format modifier used (820f1e2)

0.5.6 (2021-02-04)

Bug Fixes

0.5.5 (2021-02-04)

Bug Fixes

  • add public and s-maxage (bfd9727)

0.5.4 (2021-02-04)

Bug Fixes

0.5.3 (2021-02-04)

Bug Fixes

  • fix max-age cache header name (833272b)
  • types: optional ipxOptions (692ab1f)

0.5.2 (2021-02-03)

Features

  • support meta, content-type and svg handling (37592e7)

Bug Fixes

  • only giveup svg if no format modifier set (f5ce8b7)

0.5.1 (2021-02-03)

Bug Fixes

0.5.0 (2021-02-03)

⚠ BREAKING CHANGES

  • rewrite ipx

Features

0.4.8 (2020-12-23)

Bug Fixes

  • update allowList import (a26cae0)

0.4.7 (2020-12-23)

Bug Fixes

0.4.6 (2020-11-30)

0.4.5 (2020-11-30)

Bug Fixes

0.4.4 (2020-11-27)

Features

0.4.3 (2020-11-25)

Features

0.4.2 (2020-11-18)

Bug Fixes

  • support HttpAgent with remote input (#14) (699b671)

0.4.1 (2020-11-12)

Features

0.4.0 (2020-11-05)

Features

Bug Fixes