- PHP 90.3%
- Scheme 9.7%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
|
Some checks failed
PHP Unit Tests / test (push) Has been cancelled
Only resized variants got a "<name>_<ext>.webp" sibling: setFromString() bailed out unless the original was already public, setFromLocalFile() had its visibility check inverted (it converted protected uploads only, writing them to a public path), ImageManipulation writes through setFromStream() and was never covered at all, and moving a file from the protected to the public store (publishing) generated nothing. So originals and "no-op" variants (e.g. ScaleMaxWidth above the image width, which returns the original) had no sibling and srcset candidates fell back to JPEG/PNG. Hook into setFromStream() instead, which every core write path ends in, and generate the sibling from the file that was actually written, but only when it ended up in the public store. Publishing (publish(), swapPublish()) and renaming regenerate the siblings of the original and all of its public variants; protecting or deleting a file removes them again, so drafts no longer leave public WebP copies behind. EXIF orientation is baked in, animations are skipped and the source extensions are configurable. Add a "webp-backfill" task that writes the missing siblings for images that are already public. It is idempotent (siblings that are not older than their source are kept), skips the protected store, and refuses to run on the command line when the assets belong to another user, because the web server has to be able to overwrite the generated files. |
||
| .github/workflows | ||
| _config | ||
| src | ||
| templates/SilverStripe/Assets/Flysystem | ||
| tests | ||
| .gitignore | ||
| _config.php | ||
| composer.json | ||
| CONTRIBUTING.md | ||
| LICENSE | ||
| phpcs.xml.dist | ||
| phpunit.xml | ||
| phpunit.xml.dist | ||
| README.md | ||
silverstripe-webp-image
Overview
The silverstripe-webp-image module enhances your SilverStripe website's performance by automatically converting image assets to the efficient .webp format on demand. Learn more about WebP's benefits here. For additional details and insights, check out Tips for Optimizing Page Speeds.
Thanks
Thanks to nomidi/silverstripe-webp-image for developing the original version of this extension.
I've since forked it and modified it to use Imagick, added support for .gif, and made other optimizations.
Features
- ✅ Automatically generates
.webpversions of JPEG, PNG, and GIF images – both the originals and every resized variant. - ✅
webp-backfilltask to create the missing.webpsiblings for images that are already in the public asset store. - ✅ Significantly improves page load times with minimal configuration, making it SEO friendly.
- ✅ Designed for seamless integration with NGINX.
- ✅ Native support for Imagick.
How does it work?
Resized or altered versions of image assets are stored to disk for caching and optimization purposes. This extension intercepts calls for retrieving these cached and resized assets by automatically generating .webp counterparts of the same images. This usually occurs when leveraging Silverstripe's image manipulation features via its templating engine.
Afterwards, and depending on your server's configuration, NGINX will attempt to serve the .webp version of your resized image asset instead of the source version. If the .webp version does not exist, then it will instead serve the original version.
This extension provides conversion support for .jpeg, .png, and .gif assets using Imagick.
Backfilling existing images
Images that were uploaded before the module was installed have no .webp sibling yet. The webp-backfill task
writes the missing ones for every image in the public asset store:
vendor/bin/sake tasks:webp-backfill # --force rewrites existing siblings
The task is idempotent (siblings that are not older than their source file are left alone) and skips animations.
Because the generated files belong to the user running the task, run it through the browser
(/dev/tasks/webp-backfill) whenever the web server – not the shell user – owns the assets directory; on the command
line the task refuses to run in that case unless --ignore-owner is given.
Requirements
- SilverStripe
>= 4.2 - GD Library with WebP support enabled
- Imagick
Installation
Install the module using Composer:
composer require loveduckie/silverstripe-webp-image
Run the following to initialize:
vendor/bin/sake dev/build flush=1
Web Server Configuration
This module creates .webp images with filenames structured as file_name.<original_extension>.webp. To serve WebP images when supported, configure your web server as follows:
NGINX Configuration
Add the following to your NGINX configuration:
map $http_accept $webp_suffix {
default "";
"~*webp" ".webp";
}
location ~* /assets/.+\.(?<extension>jpe?g|png|gif|webp)$ {
gzip_static on;
gzip_types image/png image/x-icon image/webp image/svg+xml image/jpeg image/gif;
add_header Vary Accept;
expires max;
sendfile on;
try_files "${request_uri}${webp_suffix}" $uri =404;
}
This setup detects browsers that support WebP via the Accept header and serves the .webp version if available.
Apache .htaccess Configuration
For Apache users, add rules to prioritize .webp files in your .htaccess. For detailed instructions, see CSS-Tricks' guide.
Testing WebP Support
To verify WebP support in your environment, create a .php file with the following content and open it in your browser:
<?php
if (function_exists('imagewebp')) {
echo "WebP is available";
} else {
echo "WebP is not available";
}
Browser Support and Fallbacks
For browsers that don't support WebP, ensure your HTML or server configuration serves alternative formats like JPEG or PNG. Learn more about graceful degradation strategies on CSS-Tricks.
Roadmap
- Enhanced documentation
Imagick supportSiblings for original files and backfill command- PHP tests to validate WebP compatibility
- Automatic cleanup of unused
.webpfiles - Command-line functionality for flushing
.webpfiles
Contributing
Contributions are welcome! Please feel free to submit issues or pull requests to improve the module.