We run a mobile accessories shop in Lahore, mobilegeeks.pk, on WordPress and WooCommerce. Like most shop owners, we converted our product photos to WebP and expected PageSpeed Insights to stop complaining about images.

It didn't. Some images were still listed under "Improve image delivery", even though they were already WebP. This time the advice was "increase the compression".

So we read the code that makes the decision, wrote down the rules, and built two free things from them:

This post covers the rule, and three things we got wrong while building the plugin.

The rule: bytes per pixel, for every format

PageSpeed Insights, Lighthouse and the Chrome DevTools Performance panel share one check. Its source is ImageDelivery.ts in the DevTools repository. Reduced to what matters (we read it on 6 December 2026):

const TARGET_BYTES_PER_PIXEL_AVIF = 2 * 1 / 12; // ≈ 0.167 const BYTE_SAVINGS_THRESHOLD = 4096; // ignore savings under 4 KB const BYTE_SAVINGS_THRESHOLD_RESPONSIVE_BREAKPOINTS = 12288; 

What these mean in practice:

  1. Every image gets a budget of width × height × 0.167 bytes. That is about what a well-compressed AVIF needs, and the budget is the same whatever format the file is in.
  2. Over budget, the advice depends on the format. A JPEG or PNG gets "use a modern format". A WebP or AVIF gets "increase the compression". Converting to WebP does not get you a pass on its own.
  3. Savings under 4,096 bytes are ignored, so small icons rarely show up.
  4. Files bigger than their box are flagged too. The wasted share is 1 − displayed pixels ÷ file pixels. On a phone test, displayed pixels include the 1.75× screen density. If the image already has srcset or <picture>, it is only listed when the waste is above 12 KB.

A worked example:

  • The photo: 1200 × 800 pixels = 960,000 pixels.
  • The budget: 960,000 × 0.167 ≈ 160 KB.
  • A 230 KB JPEG is flagged: "use a modern format", about 70 KB to save.
  • A 135 KB WebP of the same photo passes.
  • A 300 KB WebP at 1600 × 1067 is flagged: "increase the compression". Its budget is about 285 KB.

The last case is the one that surprised us. A lot of "convert to WebP" plugins and CDNs use a fixed quality, such as 80 or 82, for every image. A detailed product photo at quality 82 can easily go over 0.167 bytes per pixel.

The tool: the rule in about 120 lines of JavaScript

The core is a small UMD file with no dependencies. It runs in the browser and in Node, and 18 plain-Node tests cover it.

var TARGET_BPP = 2 * 1 / 12; var budget = Math.round(width * height * TARGET_BPP); if (bytes / (width * height) > TARGET_BPP) { issues.push({ kind: isWebpOrAvif ? 'compression' : 'format', savings: bytes - budget }); } 

"Fix it" draws the image on a canvas at the width you need. It then searches for the highest quality whose file still fits a slightly lower target, 0.14 bytes per pixel, so small measurement differences don't push it back over the line. It tries AVIF if your browser can encode it, otherwise WebP. In Chrome, a 290 KB JPEG became a 129 KB WebP that passes. Told the image is shown 300 px wide, it made a 24 KB file.

Everything stays in the browser. On a privacy-sensitive page, "we never see your images" is easier to promise when there is no server code at all.

The plugin, and three things we got wrong

The plugin does the same job site-wide. It compresses on your own server (Imagick or GD) and makes WebP and AVIF copies of every thumbnail size. When a copy is over the budget, it lowers the quality step by step, never below a safe floor. Copies are served with <picture>, so it is safe behind page caches and Cloudflare.

The bytes-per-pixel part was the easy part. These three took longer.

1. The theme's sizes attribute was lying

On phones, our product grid images were shown much narrower than the theme's sizes hint said. The browser believed the hint, picked a large file from srcset, and PageSpeed reported the waste.

Rather than guess, the plugin now measures. On a share of visits (30% by default), a small script records how wide each image is actually shown and sends one short report to the site's own REST API. The plugin then writes that width into sizes, per page type and per placement. No cookies, no IP storage, nothing sent anywhere else.

Our first version measured with getBoundingClientRect() on every image. In PageSpeed that showed up as a "Forced reflow" of about 50 ms, which is the kind of thing the plugin is supposed to fix. Version 1.2.2 uses an IntersectionObserver instead: widths come from the browser's normal rendering, images are measured as the visitor scrolls near them, and one report is sent when the page is hidden.

2. The same photo, two widths

The same product photo can appear large in the gallery and small in a sticky "add to cart" bar. Version 1.2.0 stored one width per file, so the small placement sometimes got the large file. Version 1.2.1 stores the width per placement.

3. Clearing one page cleared every page

Whenever the plugin made a new copy, or learned new widths, it asked WP Rocket to clear the cached page. For the homepage, that meant deleting the homepage's cache folder. WP Rocket keeps every other page's cache folder inside the homepage's folder, so clearing the homepage deleted the whole site's cache, as often as every 10 minutes. Pages were rarely served from cache, which made the site slower, the opposite of the goal.

Versions 1.2.3 and 1.2.4 clear only the pages that showed the image. For the homepage they use WP Rocket's homepage-only clean. If your plugin clears caches from background jobs, check what "clear this URL" actually deletes.

Try them

Both are free. There is no account, no API key and no upsell. Issues and pull requests are welcome on GitHub. If PageSpeed flags an image that our tool says passes, I would like to see it: usually the site is sending a different file than the one that was tested, but if our rule is wrong I want to fix it.