WordPress plugin to optimize uploaded images using the Spatie image-optimizer library and binary files on your host system.
Find a file
2025-02-01 16:45:56 +01:00
inc added checks for proc_open, minor UI cleanup 2025-01-23 16:58:25 +01:00
optimizer-config added custom method to create optimizer chain (fix for svg) 2023-11-13 16:51:23 +01:00
tests added test results 2025-01-31 19:36:37 +01:00
vendor updated dependencies 2025-01-25 12:20:23 +01:00
.gitattributes cleanup 2023-11-13 18:53:51 +01:00
.gitignore added deploy script to sync git tag with wp/svn 2023-04-27 16:34:32 +02:00
.htaccess Deny direct access to plugin files 2023-08-04 10:45:12 +02:00
admin.php added checks for proc_open, minor UI cleanup 2025-01-23 16:58:25 +01:00
AnotherSimpleImageOptimizer.php fixed losing image metadata, fixed optimizing more than once 2025-01-31 18:49:33 +01:00
CHANGELOG.md added settings section to readme, updated changelog 2025-01-31 19:33:17 +01:00
composer.json cleanup 2023-08-04 15:42:36 +02:00
composer.lock updated dependencies 2025-01-25 12:20:23 +01:00
deploy.sh fixed wrong dir detection 2023-04-27 17:38:28 +02:00
LICENSE initial release 2023-03-17 12:36:48 +01:00
plugin.php bump version to 0.3.0 2025-01-31 19:39:35 +01:00
README.md cleanup 2025-02-01 16:45:56 +01:00
readme.txt another try with lists to display broken markdown in readme 2025-02-01 16:43:49 +01:00

Another simple image optimizer

WordPress plugin to automatically optimize uploaded images (jpg, png, gif, webp, svg, avif) using the Spatie image-optimizer library and binary files on your host system.

New uploaded images with all image sizes (thumbnails) will be optimized automatically.

Previously uploaded images can be updated in two ways:

  1. Use the command line interface (cli) and run wp media regenerate, which also triggers the optimization chain. If you don't have cli access, some other plugin, that regenerates existing thumbnails should work, too (not tested, yet).
  2. The media library has a new column "File size" in the list view, which displays the new and the old file size. If an image is not optimized, there is a link named "Optimize" to run the optimizer directly.

Warning: Images are replaced with the optimized ones. Create a backup before testing this plugin. If this is a deal-breaker for you, don't use it.

Important: If the needed binary files aren't installed, this plugin won't optimize anything. Don't use it, if you don't know, how to install them or if your web hoster doesn't provide them.

Notice: WordPress has no support for SVG files. Technically this plugin can optimize them, but I didn't run any tests with plugins, that add SVG support to WordPress. I was able to optimize svg files automatically in a local test setup (see prepare-and-run-tests.sh in the tests folder and search for enable_svg_avif_upload).

Requirements

  • PHP function proc_open must be enabled (some web hosts disable it via php.ini)

Settings

Quality settings can be adjusted with constants in wp-config.php:

# Set quality for resizing images (GD/Imagick)
define('ASIO_RESIZE_QUALITY_JPEG', 100); // default: 82
define('ASIO_RESIZE_QUALITY_WEBP', 100); // default: 86

# Set quality of optimizers
define('ASIO_QUALITY_JPEGOPTIM', 90); // default: 85
define('ASIO_QUALITY_PNGQUANT',  90); // default: 85
define('ASIO_QUALITY_CWEBP',     85); // default: 90
define('ASIO_QUALITY_AVIFENC',   70); // default: 63

Optimization tools

Spatie image-optimizer will use these optimizers if they are present on your system:

You can find information about how to install the binaries in the spatie image-optimizer README.

Why another image optimization plugin?

After some research the first search results pages were full of SEO spam. I don't need a CDN. I don't want to send uploaded images via limited API access to a third party provider (who uses the same binaries anyway). I just want to upload huge jpg files and let some magic in the background remove all meta data and drastically shrink that file. Also I don't like ads and bloatware.

So to avoid SEO spam I began to search for "wordpress spatie", "wordpress jpegoptim" and "wordpress optipng" to find good candidates. I found some, but they didn't match my expectations (e. g. no gif support, outdated, only cli...). So I wrote my own implementation with the goal to keep it simple.

Inspiration and similar projects

While searching existing plugins, that aren't bloated, I found the following interesting projects. If you don't like my plugin, maybe some of these fit your needs:

Installation

Copy this repository into wp-content/plugins and name it another-simple-image-optimizer or install it from the official plugin repository.

Notes

I use symfony/process v6.0.x for PHP 8.0 compatibility. Since v6.1 it requires PHP 8.1.

PHP 8.0 reaches EOL at Nov 2023 and symfony/process 6.0 is very outdated (from Feb 2022). So it felt wrong to stay backwards compatible anymore.

I decided against the psliwa/image-optimizer library, because it doesn't support webp. Also spatie/image-optimizer is easier to configure and I was already familiar with it, because I used that in a different project.

The plugin name/slug simple-image-optimizer was taken, so I chose another-simple-image-optimizer.

Choosing such a long slug was a bad idea. It is used as text domain for i18n strings. Template code became much more unreadable.

I used static methods, because I couldn't find a way to register classes/methods in hooks without loading them beforehand. So I cant't use $this or __construct(), but classes are only loaded, when needed to improve performance.

Development

Update dependencies

composer update --no-dev -o
# composer update --no-dev --ignore-platform-reqs -o

Publish new version

  • Update version number in plugin.php and in readme.txt
  • git tag like normal:
git tag -a 0.3.0 -m "bug fixes"
git push --follow-tags
  • run deploy.sh to publish latest git tag to WordPress Plugin Directory using SVN:
# chmod +x ./deploy.sh
./deploy.sh

# type y, Enter
# No ssh keys for WordPress/SVN :-(
# Use OS keyring or KeePassXC auto type with wordpress.org web login to enter password

Update trunk

# git checkout main
rm -r wp-svn/trunk && mkdir wp-svn/trunk
git archive HEAD | tar -x -C wp-svn/trunk
cd wp-svn
svn add trunk/* --force
svn commit -m "synced main branch to wp/svn trunk" --username raffaelj

Update only readme.txt

# git checkout main
cp readme.txt wp-svn/trunk/
cp readme.txt wp-svn/tags/0.1.3/
cd wp-svn
svn commit -m "Tested up to 6.3"

Submit initial version

git archive -o ../another-simple-image-optimizer.zip 0.1.1

License

Code: MIT, see LICENSE for more information.

Images (in tests/images): CC-BY Raffael Jesche.

Credits and third party resources

The following libraries are used in this project (Thanks!):

These optimizers are used under the hood: