# Recompress PNG (recompress_png)

PNG-specific recompression. How the mod_pagespeed recompress_png filter works, when to use it, its risks and the Apache, nginx and IIS directives.

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

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

`recompress_png` re-encodes PNG images losslessly. The PNG optimizer re-encodes the image and keeps the result only when it is smaller, under the same `ImageLimitOptimizedPercent` margin as the other formats; a losing result is remembered. This filter never makes a PNG lossy: turning a photographic PNG into a JPEG or a WebP is the conversion filters' decision, and when that decision does not apply or loses, the lossless re-encode this filter controls is what runs.

## How it works

The recompression filters reduce image file size by re-encoding images at an optimal quality level. They preserve visual quality while removing encoding inefficiencies, and each one handles exactly the format its name says, so a format you do not want re-encoded can be turned off by name without touching the others.

### How each format is recompressed

`recompress_png` is also the path a resized PNG takes, because resizing re-encodes the file. In OptimizeForBandwidth mode the smaller bytes replace the original response in place, with the URL unchanged. The filter is a CoreFilter through `rewrite_images`.

`recompress_images` is a convenience filter that enables `recompress_jpeg`, `recompress_png`, and `recompress_webp` together with `convert_gif_to_png`, `convert_jpeg_to_progressive`, `convert_jpeg_to_webp`, `convert_png_to_jpeg`, `jpeg_subsampling`, `strip_image_color_profile`, and `strip_image_meta_data`.

## When to use it

- In CoreFilters, the default RewriteLevel: it runs unless you disable it.
- Part of OptimizeForBandwidth, the level that optimizes resources without changing their URLs.
- Also switched on by [`recompress_images`](https://modpagespeed.com/docs/filters/recompress_images/) and [`rewrite_images`](https://modpagespeed.com/docs/filters/rewrite_images/).
- Risk rating on these docs: **Generally safe**.

## Risks

Recompression is lossy. Setting quality too low produces visible artifacts. The default values are conservative and safe for most content. Photographic content can tolerate lower quality than screenshots or text-heavy images.

## Configuration

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

**Apache**

```apache
ModPagespeedEnableFilters recompress_png
```

**nginx**

```nginx
pagespeed EnableFilters recompress_png;
```

**IIS (pagespeed.config)**

```text
pagespeed EnableFilters recompress_png
```

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

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

| Parameter                   | Default | Description                                                                       |
| --------------------------- | ------- | --------------------------------------------------------------------------------- |
| `ImageRecompressionQuality` | 85      | General quality level for recompressed images (-1 to 100; -1 uses source quality) |
| `JpegRecompressionQuality`  | -1      | JPEG-specific quality; -1 uses `ImageRecompressionQuality`                        |
| `WebpRecompressionQuality`  | 80      | WebP quality level                                                                |

### 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/).

## Frequently asked questions

**What does the recompress_png filter do?**

`recompress_png` re-encodes PNG images losslessly. The PNG optimizer re-encodes the image and keeps the result only when it is smaller, under the same `ImageLimitOptimizedPercent` margin as the other formats; a losing result is remembered. This filter never makes a PNG lossy: turning a photographic PNG into a JPEG or a WebP is the conversion filters' decision, and when that decision does not apply or loses, the lossless re-encode this filter controls is what runs.

**Is recompress_png enabled by default?**

Yes. `recompress_png` is in CoreFilters, the default RewriteLevel, so it runs unless you turn it off with `DisableFilters`. It is also part of the OptimizeForBandwidth level.

**How do I turn off recompress_png?**

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

## Related filters

- [`recompress_images`](https://modpagespeed.com/docs/filters/recompress_images/): Recompress images
- [`recompress_jpeg`](https://modpagespeed.com/docs/filters/recompress_jpeg/): Recompress JPEG
- [`recompress_webp`](https://modpagespeed.com/docs/filters/recompress_webp/): Recompress WebP
- [`convert_gif_to_png`](https://modpagespeed.com/docs/filters/convert_gif_to_png/): Convert GIF to PNG
- [`convert_jpeg_to_avif`](https://modpagespeed.com/docs/filters/convert_jpeg_to_avif/): Convert JPEG to AVIF
- [`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

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