=== MG Image Optimizer ===
Contributors: mobilegeeks
Tags: image optimization, webp, avif, compress images, lighthouse
Requires at least: 6.5
Tested up to: 7.1
Requires PHP: 8.0
Stable tag: 1.2.5
License: GPLv2 or later

Compress images on your own server, create WebP and AVIF, and serve them with <picture> tags. No API key, no monthly quota.

== Description ==

MG Image Optimizer compresses your images on your own server, creates WebP and AVIF copies, and serves them with `<picture>` tags. There is no API key, no account, no monthly quota and no external service: everything runs with Imagick or GD on your hosting.

Two things make it different from most image plugins:

* **It aims to pass Lighthouse "Improve image delivery".** Lighthouse flags a modern-format image when it uses more than 2/12 of a byte per pixel. Every WebP and AVIF copy is checked against a byte-per-pixel budget (default 0.14) and its quality is lowered step by step, never below a safe floor, until it fits.
* **It learns how wide your images really are.** Themes often write a sizes="" hint that is much bigger than the box an image fills, so phones download files that are too large. A small script measures the real width of each image in visitors' browsers and the plugin writes that width into sizes="" (see Privacy below).

Also included:

* JPEG and PNG compression, plus WebP and AVIF for the original and every thumbnail size.
* Heavy WebP uploads are re-compressed in place, with a backup.
* `<picture>` delivery that is safe with page caches and CDNs such as Cloudflare. `display:contents` keeps your layout unchanged.
* srcset and sizes added to page-builder images that have none.
* CSS background images in inline styles switch to WebP. Image preloads point to the format the page uses.
* WooCommerce gallery and variation images switch to WebP in place, so changing a variation's photo keeps working.
* New uploads are optimized in the background (Action Scheduler or WP-Cron). Bulk optimization with progress, stop and resume.
* Backups with one-click restore.
* Works alongside Imagify: re-uses existing photo.jpg.webp files, never compresses an already compressed JPEG/PNG twice, and can switch delivery over with one button.
* Clears only the affected pages in WP Rocket, LiteSpeed Cache and other supported caches.
* WP-CLI: `wp mgio status | optimize | restore | info | bulk | selftest`.
* Abilities API: `mgio/get-status`, `mgio/get-media-status`, `mgio/optimize-media`, `mgio/restore-media`, `mgio/bulk-optimize`, `mgio/get-settings`, `mgio/update-settings`, `mgio/run-tool`.

Add `?mgio=off` to a page address to see it without WebP/AVIF delivery.

= Privacy =

The plugin makes no requests to outside services and sends nothing to the plugin author.

When "Learn real image widths" is on (Settings tab, on by default), about 30% of page views load a small script that measures how wide each image is shown. It sends one short report to your own site's REST API (`/wp-json/mgio/v1/sizes`): the page type, whether the screen is narrower than 768 px, the screen width, and the width of each image. It contains no cookies, no IP address storage, no user IDs and nothing that identifies a visitor. Turn it off under Settings, or change the share of visits with the `mgio_sizes_sample_rate` filter.

The self-test and the background queue send requests from your server to your own site only.

== Installation ==

1. Plugins → Add New → Upload Plugin → mg-image-optimizer.zip → Activate.
2. Media → MG Image Optimizer → Tools → Run self-test.
3. Dashboard → Optimize all remaining images.
4. Clear the page cache (done automatically for WP Rocket when the bulk run finishes).

== Frequently Asked Questions ==

= What happens if I deactivate it? =
Compressed files stay compressed. WebP/AVIF files stay on disk but are no longer served. Backups stay in uploads/mgio-backups.

= Can I use it behind Cloudflare? =
Yes. Keep "Apache rewrite rules" off; the <picture> delivery is cache-safe.

== Changelog ==

= 1.2.5 =
* Files are written with the WordPress filesystem API and removed with wp_delete_file(). Optimized images still replace the old file in one step.
* The self-test pictures use the plugin's own repeatable number generator.
* Error messages are escaped. The current page address is sanitized before it is stored with found images.

= 1.2.4 =
* Clearing the homepage no longer wipes the whole WP Rocket cache. WP Rocket clears a page by deleting its cache folder with everything under it, and every page's folder sits inside the homepage's, so when the homepage measured new widths (at most hourly) every cached page was deleted while WP Rocket's preload still thought they were cached. The homepage now gets WP Rocket's homepage-only clean and is queued for preload; other pages are cleared as before.

= 1.2.3 =
* No more site-wide cache wipes from background work. New copies of found images used to clear every cached page (and Cloudflare's copies through WP Rocket) as often as every 10 minutes, and newly learned widths every two hours, so pages were rarely served from cache. Now only the pages that showed the image, or the page that measured the widths (at most hourly each), are cleared. Manual purges and settings changes still clear everything.

= 1.2.2 =
* The width-measuring script no longer forces a layout. It used getBoundingClientRect on every image, which made the browser lay out product grids that were not rendered yet (PageSpeed showed it as "Forced reflow", about 50 ms). It now uses an IntersectionObserver: widths come from the browser's normal rendering, images are measured as the visitor comes near them, and one beacon is sent when the page is hidden or after 30 seconds.

= 1.2.1 =
* Learned image widths are kept per placement, not per file: the same photo shown large in a gallery and small in a sticky bar or a list gets its own width for each. 1.2.0 mixed them, so a small copy could load the large file. Measurements from 1.2.0 are cleared once and learned again.

= 1.2.0 =
* Learned image widths: a tiny script (after load, on a share of visits) measures how wide each image really is on phones and on larger screens. sizes="" then uses those widths instead of the theme's guess, per page type. Fake or odd measurements can only make a file bigger, never blurry. Tools → "Reset learned image widths" starts over.
* Image preloads get the same learned sizes as their <img>, so the browser never downloads two files.

= 1.1.4 =
* Faster bulk runs: each batch starts the next one immediately with a background request (the scheduled run stays as a safety net), and batches run up to 50 seconds when PHP has no time limit.

= 1.1.3 =
* Works on pages already processed by a lazy-load plugin (WP Rocket cached pages): <source> tags get the matching data-lazy-sizes, so browsers no longer download the largest file; the missing-srcset fixer finds the real image behind a placeholder src.

= 1.1.2 =
* Preloads follow the format the page really serves for that file (or are left alone), so an excluded image or a WebP CSS background is never downloaded twice.

= 1.1.1 =
* Image preloads that already declare type="image/webp" now point to the AVIF copy too, so the hero image is not downloaded twice.

= 1.1.0 =
* Images outside the Media Library (files placed in wp-content/uploads by hand or by a theme) get WebP/AVIF copies automatically when a page uses them. Heavy WebP files are re-compressed with a backup; JPEG/PNG are never changed.

= 1.0.1 =
* Bulk run keeps going in the background when the dashboard and the queue start a batch at the same moment.

= 1.0.0 =
* First release.
