Skip to content

short-pixel-optimizer/drupal-shortpixel-plugin

Repository files navigation

ShortPixel – Drupal ImageAPI Optimize Processor

Overview

ShortPixel ImageAPI Optimize Processor is a custom Drupal module that integrates the ShortPixel POST-Reducer API as an optimization processor for the ImageAPI Optimize module.

The processor automatically optimizes images generated by Drupal Image Styles by uploading the generated file to ShortPixel and overwriting only the generated image style file, while keeping the original source image untouched.

The module fully respects Drupal’s image handling architecture and Drupal best practices.


Supported Compression Modes

The processor supports all official ShortPixel compression strategies:

  • Lossy – Best balance between image quality and file size. Recommended for most websites.
  • Glossy – High-quality compression with minimal visual loss. Ideal for photography and premium visuals.
  • Lossless – Pixel-perfect optimization with no visual changes. Best for technical images and graphics.

The compression mode can be selected directly from the ImageAPI Optimize processor configuration.


CDN Delivery Mode

The processor can also rewrite public image URLs to the ShortPixel Adaptive Images CDN format.

  • Enable Rewrite public image URLs to ShortPixel CDN
  • Set the CDN base URL to https://cdn.shortpixel.ai, https://no-cdn.shortpixel.ai, or your own CDN host
  • The module rewrites public image URLs to the Adaptive Images API format and keeps the selected compression mode in sync
  • When CDN delivery is enabled, local Post-Reducer uploads are skipped and the CDN handles optimization and format negotiation

How It Works

  1. Drupal generates an image using an Image Style
    (the image is stored under the /styles directory)
  2. ImageAPI Optimize invokes the ShortPixel processor
  3. The generated image is uploaded to ShortPixel via the POST-Reducer API
  4. The processor polls the API until optimization is complete
  5. The optimized image replaces the generated Image Style file
  6. The original image remains unchanged, as required by Drupal standards

For a deeper technical explanation, see the inline documentation in ShortPixel.php.


Implementation Details

  • Core logic is implemented in ShortPixel.php
  • Module structure follows ImageAPI Optimize processor conventions
  • Implementation is inspired by the existing TinyPNG processor
  • Extensive logging is used for debugging and validation
  • On any API or processing error, the original generated image is preserved to avoid rendering issues

Notes & Considerations

  • The first render of an image may be slower due to the external API call
  • AVIF or WebP images may occasionally be larger if the source image is already highly optimized
  • The processor is designed to fail gracefully and never block page rendering

Installation

  1. Place the module in
    /modules/custom/imageapi_optimize_shortpixel
  2. Ensure the ImageAPI Optimize module is installed and enabled
  3. Enable the module via the Drupal admin UI or Drush
  4. Configure the ShortPixel processor and API key under ImageAPI Optimize settings

License

GNU General Public License (GPL)
Same license as Drupal core and contributed modules.

About

Optimize your images via shortpixel in Drupal.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages