PHPackages                             brocode/module-image-optimizer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Image &amp; Media](/categories/media)
4. /
5. brocode/module-image-optimizer

ActiveMagento2-module[Image &amp; Media](/categories/media)

brocode/module-image-optimizer
==============================

Magento2 Module to search for certain specified image files for conversion to other formats

1.1.0(1y ago)05433MITPHP

Since Jun 29Pushed 1y ago1 watchersCompare

[ Source](https://github.com/brosenberger/module-image-optimizer)[ Packagist](https://packagist.org/packages/brocode/module-image-optimizer)[ Fund](https://www.buymeacoffee.com/brosenberger)[ RSS](/packages/brocode-module-image-optimizer/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (3)Used By (3)

Image Optimizer Base - a Magento 2 setup for image optimizations
================================================================

[](#image-optimizer-base---a-magento-2-setup-for-image-optimizations)

This module should ease the way of adding new image formats to a Magento 2 shop without the need of adapting any templates or markups.

**Goals of this module:**

- Base scanner of folders for images which might need to be served in modern formats (e.g. /pub/media)
- Framework for adding various different converter to be extendable for any new future image formats

[!["Buy Me A Coffee"](https://camo.githubusercontent.com/9f44ce2dc3b3eecdd02598900866ffc518801df1932849703dae1e5ce5031070/68747470733a2f2f7777772e6275796d6561636f666665652e636f6d2f6173736574732f696d672f637573746f6d5f696d616765732f6f72616e67655f696d672e706e67)](https://www.buymeacoffee.com/brosenberger)

Installation
------------

[](#installation)

```
composer require brocode/module-image-optimizer
bin/magento module:enable BroCode_ImageOptimizer
bin/magento setup:upgrade

```

Idea on how to delivery optimized images in a Magento 2 shop (or any other system)
----------------------------------------------------------------------------------

[](#idea-on-how-to-delivery-optimized-images-in-a-magento-2-shop-or-any-other-system)

Magento 2 is slow when delivering anything where a PHP process is involved in comparison to a simple file transfer for any file directly servable via the web server. This can be utilized to separate the conversion of the optimized images and serving them.

1. The conversion takes place within the Magento2 environment (or any other) to determine which files need conversion and to which file they should be converted to.
2. The webserver utilizes internal rewrites and file checks which file needs to be served, based on the request of the user agent (browser Accept-Header).

Following .htaccess snippet can be used to deliver WEBP images for certain directories if they do exists in addition to the original one:

```
 ############################################
 ## if client accepts webp, rewrite image urls to use webp version
AddType image/webp .webp
RewriteCond %{HTTP_ACCEPT} image/webp
RewriteCond %{REQUEST_FILENAME} (.*)\.(png|gif|jpe?g)$
RewriteCond %{REQUEST_FILENAME}\.webp -f
RewriteRule ^ %{REQUEST_FILENAME}\.webp [L,T=image/webp]

```

Background information for this .htaccess entry:

- add a new mime type for webp images
- check if the browser accepts webp images
- check if the requested file is a png, gif or jpeg file
- check if a webp file exists for the requested file
- rewrite the request to the webp file

The same can be done with any other image format (e.g. for AVIF use the mime type image/avif).

Features
--------

[](#features)

### Cronjob for folder scanning of images

[](#cronjob-for-folder-scanning-of-images)

Scans all configured image folders, can be disabled via configuration.

**Configuration**

The cronjob can be disabled under `Stores -> Configuration -> Services -> BroCodeI ImageOptimizer`, it is enabled per default.

**Image Paths**

The ImagePathScannerService recieves via dependency injection `BroCode\ImageOptimizer\Api\Data\ImagePathProviderInterface` which can provide any directory to be scanned.

```

            BroCode\ImageOptimizer\Model\Data\XmlConfigurableImagePathProvider

```

One default path provider is implemented, which takes arguments via di.xml. The current setting is for the pub/media folder, the Magento base folder is added automatically to every entry given:

```

            pub/media

```

### CLI commands

[](#cli-commands)

Images Scanning (same function as cronjob + listing possibility of images to be optimized):

```
bin/magento images:optimize:scan

```

**Options:**

- `-l | --list`: List all images that need to be optimized, file is stored in `var/_image_optimizer.log`

### Conversion Hooks

[](#conversion-hooks)

This module provides an event hook for every image that needs to be converted. This is implemented with an default Magento 2 event and can be utilized with an observer listening on the event `brocode_convert_image`. The event has following data stored that can be used:

```
$this->eventManager->dispatch(
    'brocode_convert_image',
    [
        'image_path' => $file->getPathname(),
        'converter_id' => $imageConvertValidator->getConverterId()
    ]
);
```

**Convert Validator**

A convert validator checks if a given found image in any configured path needs conversion and which converter might be used for it. Every validator must implement `BroCode\ImageOptimizer\Api\Data\ImageConvertValidationInterface`. A base implementation for file checks is implemented in the abstract class `BroCode\ImageOptimizer\Model\Converter\AbstractImageConverter`.

These converter validator need to be contributed via di.xml to the `\BroCode\ImageOptimizer\Model\ImageConverterService`:

```

        ...

            BroCode\ImageAvifOptimizer\Model\Converter\AvifImageConverter

```

### Image conversion

[](#image-conversion)

There is currently no image conversion implemented in this module, this is done with following two basic modules:

- **brocode/module-image-optimizer-avif** (for AVIF generation)
- **brocode/module-image-optimizer-webp** (for WEBP generation)

Though there is the default `BroCode\ImageOptimizer\Observer\InstantConvertImageObserver` which catches the conversion event and try to convert the image with the help of any converter contributed to the `\BroCode\ImageOptimizer\Model\ImageConverterService`:

```

            BroCode\ImageAvifOptimizer\Model\Converter\AvifImageConverter

        ...

```

**This is done synchronously and slows down the according cron execution, especially if there are many images.**

Consider using the extensions for the usage of the Magento 2 queue system to asynchronously process image conversion:

- **brocode/module-image-optimizer-queue** (default MySQL queue for shops without active RabbitMQ installations)
- **brocode/module-image-optimizer-amqp** (extension to the queue module for configurations of the RabbitMQ services)

Change Log
----------

[](#change-log)

**1.1.0**

- Moved image path provider to service instead of cron job
- added CLI command to scan/optimize images + listing of images to be optimized

**1.0.0**

- Initial version

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance41

Moderate activity, may be stable

Popularity16

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 90.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~191 days

Total

2

Last Release

497d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9453d161c28a18e817351e00d7ebe81ada31842a249ffb3389830b3483584e60?d=identicon)[brosenberger](/maintainers/brosenberger)

---

Top Contributors

[![rosenberger-e-conomix](https://avatars.githubusercontent.com/u/47806105?v=4)](https://github.com/rosenberger-e-conomix "rosenberger-e-conomix (10 commits)")[![brosenberger](https://avatars.githubusercontent.com/u/2969243?v=4)](https://github.com/brosenberger "brosenberger (1 commits)")

---

Tags

image-optimisationmagento2-module

### Embed Badge

![Health badge](/badges/brocode-module-image-optimizer/health.svg)

```
[![Health](https://phpackages.com/badges/brocode-module-image-optimizer/health.svg)](https://phpackages.com/packages/brocode-module-image-optimizer)
```

###  Alternatives

[yireo/magento2-next-gen-images

Magento 2 module to add NextGen images support to the Magento frontend

471.1M2](/packages/yireo-magento2-next-gen-images)[smile/module-product-label

Smile - Extension of Magento2 to be able to contribute images associated to product attribute values.

5898.7k](/packages/smile-module-product-label)[elgentos/magento2-imgix

Imgix extension for Magento 2 to process images

1668.0k](/packages/elgentos-magento2-imgix)[dlambauer/magento2-module-backend-widget-imageupload

Simply add Images to your Widget

2331.4k](/packages/dlambauer-magento2-module-backend-widget-imageupload)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
