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.
- convert_gif_to_png — Convert GIF to PNG
- convert_jpeg_to_progressive — Progressive JPEG
- convert_jpeg_to_webp — Convert JPEG to WebP
- convert_png_to_jpeg — Convert PNG to JPEG
- convert_to_webp_lossless — Lossless WebP
- inline_images — Inline images
- jpeg_subsampling — JPEG chroma subsampling
- recompress_jpeg — Recompress JPEG
- recompress_png — Recompress PNG
- recompress_webp — Recompress WebP
- resize_images — Resize images
- strip_image_color_profile — Strip image color profiles
- strip_image_meta_data — Strip image metadata
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_progressiveconvert_jpeg_to_webpconvert_png_to_jpegconvert_gif_to_pngconvert_to_webp_losslessstrip_image_color_profilestrip_image_meta_datajpeg_subsamplingresize_imagesinline_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-Pagespeedresponse header and a?PageSpeedFilters=-rewrite_imagescomparison; Is it working? has the steps.
Configuration
Enable it in the module configuration, at server, virtual-host or location scope:
Apache
ModPagespeedEnableFilters rewrite_imagesnginx
pagespeed EnableFilters rewrite_images;IIS (pagespeed.config)
pagespeed EnableFilters rewrite_imagesIt 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_webppagespeed 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%
Frequently asked questions
- What does the rewrite_images filter do?
rewrite_imagesis 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_imagesis in CoreFilters, the default RewriteLevel, so it runs unless you turn it off withDisableFilters. Some of its filters are also part of the OptimizeForBandwidth level. - How do I turn off rewrite_images?
- Add
ModPagespeedDisableFilters rewrite_imageson Apache orpagespeed DisableFilters rewrite_images;on nginx. On IIS, addpagespeed DisableFilters rewrite_imagesto pagespeed.config.
Related filters
- recompress_images — Recompress images
- convert_jpeg_to_webp — Convert JPEG to WebP
- convert_jpeg_to_avif — Convert JPEG to AVIF
- resize_images — Resize images
- lazyload_images — Lazy-load images
- convert_gif_to_png — Convert GIF to PNG
- convert_jpeg_to_progressive — Progressive JPEG
- convert_png_to_jpeg — Convert PNG to JPEG
This page is drawn from the rewrite_images entry on Image filters. Every filter in one table: PageSpeed filters.