# Optimize images (rewrite_images)

Master image optimization: recompress, convert, resize and inline (enables the sub-filters). The rewrite_images filter: what it does and its risks.

Canonical URL: https://modpagespeed.com/docs/filters/rewrite_images/

- Category: Image
- CoreFilters: Yes
- OptimizeForBandwidth: Partly
- Risk: Generally safe

`rewrite_images` is the compound image filter: one name switches on the whole optimization pipeline, covering recompression, format conversion, resizing to declared dimensions, metadata stripping, and inlining of small images. Every `<img>` on the page, and every image referenced from CSS, is a candidate. If optimization finishes within the rewrite deadline, the first view already gets the `.pagespeed.ic.` URL; otherwise that view keeps the original and later views get the optimized one.

## What it switches on

`rewrite_images` is a compound name. Enabling it enables these 13 filters; each can be disabled on its own.

- [`convert_gif_to_png`](https://modpagespeed.com/docs/filters/convert_gif_to_png/): Convert GIF to PNG
- [`convert_jpeg_to_progressive`](https://modpagespeed.com/docs/filters/convert_jpeg_to_progressive/): Progressive JPEG
- [`convert_jpeg_to_webp`](https://modpagespeed.com/docs/filters/convert_jpeg_to_webp/): Convert JPEG to WebP
- [`convert_png_to_jpeg`](https://modpagespeed.com/docs/filters/convert_png_to_jpeg/): Convert PNG to JPEG
- [`convert_to_webp_lossless`](https://modpagespeed.com/docs/filters/convert_to_webp_lossless/): Lossless WebP
- [`inline_images`](https://modpagespeed.com/docs/filters/inline_images/): Inline images
- [`jpeg_subsampling`](https://modpagespeed.com/docs/filters/jpeg_subsampling/): JPEG chroma subsampling
- [`recompress_jpeg`](https://modpagespeed.com/docs/filters/recompress_jpeg/): Recompress JPEG
- [`recompress_png`](https://modpagespeed.com/docs/filters/recompress_png/): Recompress PNG
- [`recompress_webp`](https://modpagespeed.com/docs/filters/recompress_webp/): Recompress WebP
- [`resize_images`](https://modpagespeed.com/docs/filters/resize_images/): Resize images
- [`strip_image_color_profile`](https://modpagespeed.com/docs/filters/strip_image_color_profile/): Strip image color profiles
- [`strip_image_meta_data`](https://modpagespeed.com/docs/filters/strip_image_meta_data/): Strip image metadata

## How it works

Live demo: [rewrite_images](https://modpagespeed.com/examples/rewrite_images/).

### When it helps and when it does not

The compound pays on sites whose images are uploaded as-is: photos exported at full resolution, opaque PNGs that would be far smaller as JPEG or WebP, files carrying camera metadata nobody reads. A site that already runs a disciplined image pipeline, with build-time resizing, modern formats, and stripped metadata, leaves it little to do, while the CPU cost of the first rewrite of every image still applies. It also does nothing for images on domains the configuration does not authorize, or for sources over `ImageResolutionLimitBytes` (default 33554432 bytes).

### How it decides

Each member filter applies its own test: resizing only happens when `width` and `height` are declared, WebP only when the browser advertises support, inlining only below `ImageInlineMaxBytes`. Every recompression or conversion is kept only when the result is smaller than the original, by the margin `ImageLimitOptimizedPercent` sets (default 100: any reduction). A losing rewrite is remembered, so a losing image is not re-encoded on every request. Some members are also part of OptimizeForBandwidth; the compound itself is a CoreFilter.

### Sub-filters enabled by default

When `rewrite_images` is active, the following sub-filters are enabled automatically:

- `recompress_images` (and its sub-filters: `recompress_jpeg`, `recompress_png`, `recompress_webp`)
- `convert_jpeg_to_progressive`
- `convert_jpeg_to_webp`
- `convert_png_to_jpeg`
- `convert_gif_to_png`
- `convert_to_webp_lossless`
- `strip_image_color_profile`
- `strip_image_meta_data`
- `jpeg_subsampling`
- `resize_images`
- `inline_images`

## When to use it

- In CoreFilters, the default RewriteLevel: it runs unless you disable it.
- Some of the filters it enables are part of OptimizeForBandwidth.
- Risk rating on these docs: **Generally safe**.

## Risks

- Recompression is lossy. The default quality levels are conservative, but check quality-critical imagery such as logos and screenshots with text after enabling.

- The first optimization of each image costs server CPU; `ImageMaxRewritesAtOnce` (default 8) bounds how many run in parallel on a cold cache.

- Verify with the `X-Mod-Pagespeed` response header and a `?PageSpeedFilters=-rewrite_images` comparison; [Is it working?](https://modpagespeed.com/docs/is-it-working/) has the steps.

## Configuration

Enable it in the module configuration, at server, virtual-host or location scope:

**Apache**

```apache
ModPagespeedEnableFilters rewrite_images
```

**nginx**

```nginx
pagespeed EnableFilters rewrite_images;
```

**IIS (pagespeed.config)**

```text
pagespeed EnableFilters rewrite_images
```

It is on by default under CoreFilters; to turn it off:

```text
ModPagespeedDisableFilters rewrite_images   # Apache
pagespeed DisableFilters rewrite_images;   # nginx
pagespeed DisableFilters rewrite_images   # IIS
```

```html
<!-- before -->
<img src="/photos/team.jpg" width="400" height="300" />

<!-- after, to a WebP-capable browser: recompressed, resized, cache-extended -->
<img src="/photos/400x300xteam.jpg.pagespeed.ic.HASH.webp" width="400" height="300" />
```

To disable a specific sub-filter while keeping `rewrite_images` active:

```apache
ModPagespeedEnableFilters rewrite_images
ModPagespeedDisableFilters convert_jpeg_to_webp
```

```nginx
pagespeed EnableFilters rewrite_images;
pagespeed DisableFilters convert_jpeg_to_webp;
```

### On the worker

The worker runs its own pipeline, configured by flags. Its equivalent of this filter is the [image pipeline](https://modpagespeed.com/docs/worker-configuration/#image-pipeline) transform. Always-on baseline under `pagespeed on;`; there is no per-transform off switch.

Scoping, `ForbidFilters` and the thresholds filters read: [Choosing filters](https://modpagespeed.com/docs/filter-selection/).

## Live example

See the before and after, with the source diff: [rewrite_images example](https://modpagespeed.com/examples/rewrite_images/).

## Frequently asked questions

**What does the rewrite_images filter do?**

`rewrite_images` is the compound image filter: one name switches on the whole optimization pipeline, covering recompression, format conversion, resizing to declared dimensions, metadata stripping, and inlining of small images. Every `<img>` on the page, and every image referenced from CSS, is a candidate. If optimization finishes within the rewrite deadline, the first view already gets the `.pagespeed.ic.` URL; otherwise that view keeps the original and later views get the optimized one.

**Is rewrite_images enabled by default?**

Yes. `rewrite_images` is in CoreFilters, the default RewriteLevel, so it runs unless you turn it off with `DisableFilters`. Some of its filters are also part of the OptimizeForBandwidth level.

**How do I turn off rewrite_images?**

Add `ModPagespeedDisableFilters rewrite_images` on Apache or `pagespeed DisableFilters rewrite_images;` on nginx. On IIS, add `pagespeed DisableFilters rewrite_images` to pagespeed.config.

## Related filters

- [`recompress_images`](https://modpagespeed.com/docs/filters/recompress_images/): Recompress images
- [`convert_jpeg_to_webp`](https://modpagespeed.com/docs/filters/convert_jpeg_to_webp/): Convert JPEG to WebP
- [`convert_jpeg_to_avif`](https://modpagespeed.com/docs/filters/convert_jpeg_to_avif/): Convert JPEG to AVIF
- [`resize_images`](https://modpagespeed.com/docs/filters/resize_images/): Resize images
- [`lazyload_images`](https://modpagespeed.com/docs/filters/lazyload_images/): Lazy-load images
- [`convert_gif_to_png`](https://modpagespeed.com/docs/filters/convert_gif_to_png/): Convert GIF to PNG
- [`convert_jpeg_to_progressive`](https://modpagespeed.com/docs/filters/convert_jpeg_to_progressive/): Progressive JPEG
- [`convert_png_to_jpeg`](https://modpagespeed.com/docs/filters/convert_png_to_jpeg/): Convert PNG to JPEG

This page is drawn from the [rewrite_images entry on Image filters](https://modpagespeed.com/docs/image-filters/#rewrite_images). Every filter in one table: [PageSpeed filters](https://modpagespeed.com/docs/filters/).
