Image optimizer tool

From MoodleDocs

Image optimizer is an admin tool that makes large images in Moodle smaller. It finds oversized images anywhere in the site's file storage, resizes them to a maximum width and height, and compresses them again. This saves disk space and makes pages load faster, especially for photos that were uploaded straight from a phone or camera.

Installation

Install the plugin from the Moodle plugins directory, or download the ZIP file and follow Installing plugins. The files go in the folder admin/tool/imageoptimizer. The plugin supports Moodle 5.0 to 5.3.

The plugin uses PHP's GD extension, which every Moodle site has. If the Imagick extension is installed, the plugin uses it instead, for better quality and more formats.

The plugin does nothing until you turn it on with Enable image optimization (see Settings).

How it works

A scheduled task, Optimize uploaded images, runs every 15 minutes by default. Each time, it looks for image files that are larger than the minimum size and have not been processed yet, and for each one it:

  1. resizes the image to fit within the maximum width and height, keeping its proportions (smaller images keep their size),
  2. compresses it again at the chosen quality, in its own format or converted to JPEG or WEBP,
  3. replaces the file's content, keeping its file name, owner, author, licence and source,
  4. records that the file has been processed, so it is not processed again.

Images whose declared size is over 40 megapixels are skipped, to protect the server from images built to use up its memory.

Why the file name never changes

Text in Moodle (pages, labels, forum posts, course summaries and so on) refers to its images by file name. If the plugin renamed photo.png to photo.jpg when converting it to JPEG, every place that shows the image would break. So the file name always stays the same, even when the format changes: a file called photo.png can then contain a JPEG image. Browsers show it correctly, because they look at the content, not the name, but it is worth knowing if you look at the files directly.

Settings

The plugin's settings.

In Site administration > Plugins > Admin tools > Image optimizer:

Setting What it does Default
Enable image optimization Turns the scheduled task's work on or off. Off
Minimum file size (KB) Only images larger than this are processed. 500
Maximum width (px) and Maximum height (px) Larger images are resized down to fit, keeping their proportions. 1920 and 1080
Compression quality (1-100) The quality used when saving JPEG and WEBP images. 80
Target format Keep original, or convert every processed image to JPEG or WEBP. Keep original

To change how often the task runs, edit Optimize uploaded images in Site administration > Server > Tasks > Scheduled tasks.

Capabilities

The plugin adds no capabilities. Its settings need the usual site administration permissions.

Privacy

For each image it processes, the plugin records the file's owner, file name, original and new size, and when it was processed. Because each record holds the owner's user ID, the records are included in that user's data exports and deletion requests. The scheduled task also removes the records of files that no longer exist. See Data privacy.

See also