Ensure images are optimized
Google PageSpeed Optimizer automatically optimizes your store images to improve load times and overall site performance. However, for the process to work correctly, it’s important to ensure your Magento cron is running and all required image optimization libraries are properly installed.
Check that cron jobs are working
The image optimization process runs automatically through Magento’s default cron. Make sure that:
- Cron jobs are configured on your server.
- Cron is running without errors.
- Magento’s cron tasks are executed regularly (usually every minute).
If cron is not working properly, image optimization and WebP conversion will not run automatically.
Verify required tools
To check whether all optimization tools are installed and working correctly, run:
bin/magento mirasvit:optimize-image:validate
If you see messages about missing libraries, install them using your system package manager:
# Ubuntu
sudo apt-get install jpegoptim optipng gifsicle webp imagemagick
# CentOS
sudo yum install jpegoptim optipng gifsicle webp imagemagick
If Use AVIF images is enabled, also install avifenc. See How to serve AVIF images.
Run optimization manually
You can manually trigger image optimization at any time by running:
bin/magento mirasvit:optimize-image:optimize
The command optimizes a limited number of images per iteration, so you may need to run it multiple times to process all images.
Understand which images are optimized
The extension can use one of two optimization strategies:
| Strategy | Description |
|---|---|
| Filesystem | Queues the images Magento creates right away and finds other images in pub/media with a daily scan, then optimizes them in batches. |
| Webpages | Processes only images that appear on the website pages visited by users. This strategy prioritizes visible, frequently used images. |
Use the Webpages strategy for better performance and to optimize the most relevant images first.
Why some images may not be optimized
Some images cannot be converted to WebP or optimized because they fall under exceptions. These include:
- Images without
<img>tags or propersrcattributes - Images with lazy-loading classes:
lazyOwl,owl-lazy,swiper-lazy,mst-no-webp - Images loaded dynamically via AJAX
- Images added inline in HTML (for example, as
background-imagein a style attribute)
Unfortunately, these images cannot be optimized automatically by the extension.
Why an image has no AVIF copy
An image is not converted to AVIF when:
- It is a GIF. Only JPEG and PNG images get an AVIF copy.
- Its format is selected in Exclude image formats from optimization.
avifencfailed on it. The error is written tovar/log/system.log, and the image is shown as WebP (or in its original format) instead. The extension tries it again whenavifencon the server is updated, when you change AVIF quality, or after a week.
Handling “Command returned non-zero exit code” errors
During image optimization, you may sometimes encounter messages like:
Command returned non-zero exit code:
Such errors can occur for various reasons — for example, when the file extension does not match the actual file type.
When this happens, the system will display the exact command that failed, for example:
jpegoptim --all-progressive --strip-xmp --strip-com --strip-exif --strip-iptc "/path/to/pub/media/iopt/example-image.jpg" 2>&1
If you run this command manually in your terminal, you will see the specific error message and the reason for the failure. This helps identify whether the issue is caused by a corrupted image, a format mismatch, or a missing tool.
Re-run optimization if needed
If you notice that some images are still not optimized:
-
Run the command a few more times:
bin/magento mirasvit:optimize-image:optimize -
Visit different pages on your storefront if using the Webpages strategy — this helps the extension detect more images for optimization.
-
Verify that cron is running and no system resource limits prevent optimization tasks.
If you enable Use WebP images or Use AVIF images, give Magento cron some time to process and convert your images before checking the results in PageSpeed Insights.
Keep the image optimization table small
The extension keeps one record for every image it finds in the mst_optimize_image_file database table. The record links the original image to its optimized, WebP, and AVIF copies in pub/media/iopt/.
Why the table grows
When an image is deleted, its record and copies are removed only if Magento deletes it itself. For example, Flush Catalog Images Cache in System > Cache Management also removes the records of the resized product images. When images are deleted in other ways (by a deployment script, with rm -rf, or together with deleted products), their records stay in the table. With the Filesystem scan strategy, Magento then creates new resized images, and the extension records them again. On large catalogs the table can grow to millions of rows.
Since version 2.3.0, the image optimization cron removes the records of images that no longer exist on disk, together with their optimized, WebP, and AVIF copies. It works in batches and goes through the whole table at most once a day. Since version 2.3.1 the cleanup runs even when image optimization is disabled. This cleanup runs only with the Filesystem scan strategy and only when media files are stored on the local disk.
If the table is large, switch the Image scan strategy to Webpages. With this strategy, the extension records only the images that appear on the pages your visitors open, so the table stays much smaller. The cron does not clean up the table with this strategy, so run bin/magento mirasvit:optimize-image:cleanup from time to time.
Clean up the table
Choose one of the options below:
-
Remove records of deleted images. Run the command below. It removes records for images that no longer exist on disk and deletes the copies made from them. Other images keep their optimized copies.
bin/magento mirasvit:optimize-image:cleanupThe command does nothing when media files are stored remotely (for example, on Amazon S3) or in the database, because missing images cannot be detected there.
-
Reset all images. Run
bin/magento mirasvit:optimize-image:reset. It deletes all optimized, WebP, and AVIF copies and resets every record. On a very large table this command can take a long time. -
Fast full reset. It is safe to empty the table — the extension creates the records again on the next cron runs. Run the SQL query below, then delete the
pub/media/iopt/folder so no unused copies are left on disk, and flush the Magento cache (and Varnish or CDN cache, if used), so no cached page points to the deleted copies:TRUNCATE TABLE mst_optimize_image_file;rm -rf pub/media/iopt/
bin/magento cache:flushIf your database uses a table prefix, add it to the table name. If media files are stored remotely, delete the
iopt/folder in the storage instead.
After a full reset, the storefront shows the original images until cron optimizes them and creates new WebP and AVIF copies. On large stores this can take some time.