WebP Auto Converter

WebP Auto Converter
# WebP Auto Converter for OpenCart 3.x OCMOD

Automatically converts product and category images to WebP format on the fly. Modern browsers receive WebP, old browsers receive the original format transparently. If the original image is already WebP - OpenCart handles it natively

Tested on **OpenCart 3.0.5.0**. Compatible with all OpenCart 3.x versions - the patched search strings in `catalog/model/tool/image.php` and `system/library/response.php` are unchanged across all 3.x releases according to the GitHub repository history.

---

## Features

- Automatically converts JPG, PNG and GIF images to WebP on first request - no manual steps required
- Uses OpenCart's native `Image` class for resizing, so WebP output is pixel-perfect and matches the original cache quality
- WebP quality follows OpenCart's default (90) - no separate configuration needed
- WebP files are stored in a separate `image/cache/cachew/` directory mirroring the original cache structure - existing cache is never modified
- Original file extension is encoded in the WebP filename (`product-228x228.jpg.webp`) - enabling lossless fallback reconstruction
- If the original image is already WebP - the mod skips conversion entirely and lets OpenCart handle it natively
- Automatically regenerates WebP when the original image is replaced - same logic OpenCart uses for its own cache
- Old browsers (no `image/webp` in Accept header) transparently receive original JPG/PNG URLs - no JavaScript or `<picture>` tags needed
- Fallback works across all URL contexts - `src`, `href`, `srcset`
- Admin area is excluded from fallback processing
- No OCMOD `replace` operations - only `before` additions, safe to use alongside other modifications

---

## Installation

1. Go to **Admin → Extensions → Installer**
2. Upload `webpauto.ocmod.zip`
3. Go to **Admin → Extensions → Modifications**
4. Click **Refresh** (top right)

That's it. No module settings, no database changes.

---

## How it works

When OpenCart resizes an image, this mod intercepts the process and creates a WebP version in a parallel cache directory `image/cache/cachew/`. The WebP file mirrors the full directory structure of the original cache:

```
image/cache/catalog/demo/macbook_1-228x228.jpg

image/cache/cachew/catalog/demo/macbook_1-228x228.jpg.webp
```

The original extension is preserved as part of the filename (double extension). This allows the fallback mechanism to reconstruct the exact original path for old browsers.

**If the original image is already WebP** - OpenCart handles it natively. This mod skips conversion entirely and does not interfere with the resize or URL output.

WebP files are regenerated automatically if the original image is newer than the cached WebP - no need to manually clear cache when replacing a product image.

---

## Browser fallback

The mod checks the `HTTP_ACCEPT` header on every response:

- **Modern browser** (sends `image/webp` in Accept) - receives WebP URLs as-is
- **Old browser** (no `image/webp` in Accept) - the mod rewrites all WebP URLs back to their original format before the page is sent

```
/image/cache/cachew/catalog/demo/macbook_1-228x228.jpg.webp
↓ (old browser only)
/image/cache/catalog/demo/macbook_1-228x228.jpg
```

This happens at the response level - no JavaScript or `<picture>` tags are needed.

---

## Files modified

### `catalog/model/tool/image.php`
Two operations, both `position="before"` - no replace logic.

**Operation 1** - inserted before the resize cache check:
Creates a WebP version using OpenCart's native `Image` class (`resize()` + `save()`). Skipped entirely if the original image is already WebP. Skipped if WebP already exists and is up to date.

**Operation 2** - inserted before the final URL return:
Checks if a WebP version exists in `cache/cachew/` and substitutes it into the returned URL. Skipped if the original image is already WebP.

### `system/library/response.php`
Two operations, both `position="before"` - no replace logic.

**Operation 1** - inserted before `compress()`:
Adds a private `webpFallback()` method to the Response class. Rewrites WebP URLs back to originals for browsers that do not support WebP. Skipped in admin area. Skipped if GD WebP support is unavailable.

**Operation 2** - inserted before the compress/output line:
Calls `webpFallback()` on the complete HTML output before it is gzip-compressed and sent to the browser.

---

## Requirements

- OpenCart 3.x (tested on 3.0.5.0)
- PHP with GD extension and WebP support (`imagewebp` function available)
- No third-party libraries

---

## Notes

- WebP files are stored in `image/cache/cachew/` - a separate directory mirroring `image/cache/`
- Clearing OpenCart's image cache also clears WebP cache - same refresh cycle
- Contains no OCMOD `replace` operations - only `before` additions, making it safe alongside other modifications




Download
  • Developed by OpenCart Community
  • Documentation Included

Rating

Compatibility
3.0.0.0, 3.0.1.0, 3.0.1.1, 3.0.1.2, 3.0.1.3, 3.0.2.0, 3.0.3.0, 3.0.3.1, 3.0.3.2, 3.0.3.3, 3.0.3.5, 3.0.3.6, 3.0.3.7, 3.0.3.8, 3.x.x.x, 3.0.3.9, 3.0.4.0, 3.0.4.1, 3.0.5.0

Last Update
31 Mar 2026

Created
31 Mar 2026
71 Downloads
1 Comments
Chizz
Chizz
Member since: 1 Feb 2013

View all extensions Get Support