Skip to main content

filter · Image filters

Optimize images (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.

How it works

Live demo: 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? has the steps.

Configuration

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

Apache

ModPagespeedEnableFilters rewrite_images

nginx

pagespeed EnableFilters rewrite_images;

IIS (pagespeed.config)

pagespeed EnableFilters rewrite_images

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

ModPagespeedDisableFilters rewrite_images   # Apache
pagespeed DisableFilters rewrite_images;   # nginx
pagespeed DisableFilters rewrite_images   # IIS
<!-- 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:

ModPagespeedEnableFilters rewrite_images
ModPagespeedDisableFilters convert_jpeg_to_webp
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 transform. Always-on baseline under pagespeed on;; there is no per-transform off switch.

Scoping, ForbidFilters and the thresholds filters read: Choosing filters .

Live example

Recompresses images, resizes them to their display size, and inlines small ones.

Measured on the demo page, original against optimized:

  • Total bytes

    264.3 KB → 11.7 KB −96%

See the before and after, with the source diff →

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.

This page is drawn from the rewrite_images entry on Image filters. Every filter in one table: PageSpeed filters.

Search