How to compress Drupal 8 images and PDF

Drupal 8 Image and PDF compression script upload

The Drupal image and PDF compression is possible using a script. This script is compatible with all the servers that run PHP, all CMS, it's a standalone installation. It means that you just need to upload a folder of files on your server, then you can start to compress all images and PDF of this server.

The script can be downloaded here:


Note: Placing the files at the root of you Drupal installation is always a good idea because you'll need to browse folders and subfolders after install, to select what is going to be compressed.

Then download + unzip the archive and drop the folder on your server.



Run the installation

You can now call the folder path from your browser typing the URL: (default name). The install routine will be called, you need to define a password to secure folder access.



Setup your password, it can be anything, nothing related to your Drupal install. That should lead you to a success message.



Add API credentials to active Drupal compression

First, you'll need to add your API key and secret available on My account > API and Quota menu. 



Setup the Drupal image and PDF compression quality

Then you can select a type of compression per media type:

  • Best saving or Lossy: this is the best choice for a final usage, super compression yet not visible
  • Lossless: if you have plan to reuse images, you can use this setting, light compression but preserve all image data (like colors)
  • No compression: does not apply any compression on this media type

For more information about compression quality refer to this page:




Setup a cron task to make the compression automatic

A Cron task is a scheduled commands or tasks also known as “Cron Jobs" or "crontab".

A Cron URL is available in the script configuration to launch the Drupal image and PDF optimization automatically. Depending of the Cron task setup, you can choose when you want launch the compression on your server. For an automatic compression you need to add this URL on your hosting Cpanel, sech for "Cron task" or "Crontab".


Image Metadata, IPTC and Drupal compression

The Metadata settings are also available to preserve some of the meta information of your images. For example you may want to preserve location information to use your images with a photo locator module?



Exclude / Include folders from Drupal compression

Finally, you need to select where you want to run the optimization, which server folder will be compressed. This is usually at least the Drupal image folders but that can be any folder listed (even from other CMS).

All your server folders under the ImageRecycle install are listed here, click on the name to deploy the folder tree. All new media added in those folders will be checked and listed in the next step for optimization.



Run the Drupal image and PDF compression

Here we are, hit the top menu Images to list the optimizable media, if nothing is there hit the button SYNC ALL or try the filter field (top left).



Then use optimize button to check everything is OK, you should be able to compress media and do a "revert to original". The revert option is available only during the media backup time (1 month).

image optimization launch



You can also launch a global optimization of your media. It's a background process, so you can close the window, the process will still be executed anyway.



And you can stop the compression process when you want.