EWWW has a lot of filters, but we also currently provide two action hooks that you can use to alter an image before and after optimization (generally, changes should be made prior to optimization, so try not to modify the image contents with the latter hook):

  • ewww_image_optimizer_pre_optimization
  • ewww_image_optimizer_post_optimization

While action functions cannot permanently modify the parameters passed to them, we do pass two parameters to these two hooks: the filename, and the mime-type, which will be something like ‘image/png’. And while the parameters cannot be permanently modified, the most likely use case is that you want to modify the image prior to optimization. The code below is based on a snippet that Jamie Hill of Saint Cloud Records sent me a while back. They use this effect at http://iknowiknow.me, and it requires that the GD extension be installed for PHP, which you can check on the EWWW settings page in the Plugin Status section. So here’s the example:

 Plugin Name: Special Image Effects
 Version: .1
add_action( 'ewww_image_optimizer_pre_optimization', 'special_image_effects', 10, 2 );
function special_image_effects( $file, $type ) {
    $image = wp_load_image( $file );
    imagefilter( $image, IMG_FILTER_GRAYSCALE);
    imagefilter( $image, IMG_FILTER_BRIGHTNESS, +30);
    imagefilter( $image, IMG_FILTER_CONTRAST, -10);
    switch ($type) {
        case 'image/gif':
            imagegif( $image, $file );
        case 'image/png':
            imagepng( $image, $file );
        case 'image/jpeg':
            imagejpeg( $image, $file );

If you don’t care about the mimetype or the priority at which your function runs, you can omit the last two parameters for add_action(). The ewww_image_optimizer_post_optimization hook could be used to upload images to a CDN or something of that nature, but again, try to avoid modifying the image post-optimization. Especially as the default WP image routines will produce unoptimized images, so EWWW will be invoked again and you could end up in a recursive loop that never ends. Generally speaking, if your plugin/theme is using the WP_Image_Editor class and methods to manipulate images (like you should be), your images will be optimized anyway. That said, some folks like to write their own custom image manipulation code, so who am I to judge? If you have questions, give us a holler!.