Scans every page folder for images and shrinks any that exceed a configurable maximum resolution. Grav plugin.
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-11-15 11:37:35 +00:00
classes 1.2.0: Grav version, homepage and issues links in the same layout as the other plugins 2026-11-15 11:37:35 +00:00
cli v1.1.0: orientation, animated WebP, memory guard, one scan at a time 2026-10-11 11:57:59 +00:00
docs/images v1.1.0: orientation, animated WebP, memory guard, one scan at a time 2026-10-11 11:57:59 +00:00
tests ImageShrink 1.1.2: memory-limit test works on PHP builds that count GD memory 2026-10-26 18:24:04 +00:00
blueprints.yaml 1.2.0: Grav version, homepage and issues links in the same layout as the other plugins 2026-11-15 11:37:35 +00:00
CHANGELOG.md 1.2.0: Grav version, homepage and issues links in the same layout as the other plugins 2026-11-15 11:37:35 +00:00
imageshrink.php v1.1.0: orientation, animated WebP, memory guard, one scan at a time 2026-10-11 11:57:59 +00:00
imageshrink.yaml Initial commit (v1.0.0) 2026-08-16 17:27:24 +00:00
LICENSE v1.1.1: author Rawrso.me - David Hall 2026-10-11 17:36:11 +00:00
README.md v1.1.0: orientation, animated WebP, memory guard, one scan at a time 2026-10-11 11:57:59 +00:00

Image Shrink

Grav 2.0 / Admin2 plugin. Scans every page folder — and any subfolder inside it (galleries, etc.) — and shrinks images larger than a configurable maximum resolution, in place, using PHP's built-in GD library (no Imagick, no Composer packages, so it runs fine on plain cPanel shared hosting).

Install

git clone forgejo@code.hall.me.uk:david/imageshrink.git user/plugins/imageshrink

Or copy this whole folder to user/plugins/imageshrink/ and enable it from Admin2's Plugins list (or drop it in via FTP and it'll show up disabled — enable it, then set your max width/height under its Configuration tab).

Use

  • Admin button: a "Shrink Images" button appears in the Admin2 top toolbar. Click it, confirm, and it scans the whole user/pages tree synchronously and reports how many images it shrunk.

    The Shrink Images button in the Admin2 toolbar

    Confirm Result
    The confirmation: originals are overwritten and this cannot be undone The summary after a scan

    Only accounts with the api.media.write permission (or api.super) can run it. The plugin checks that itself as well as relying on the API plugin, because API versions before the fix for GHSA-8mjx-xjfv-9c88 let any admin account run toolbar actions.

  • CLI: bin/plugin imageshrink scan — same scan, run over SSH. Useful if your host's PHP execution-time limit is too short for the admin button on a large media library.

  • Nightly scheduler (optional): turn on "Run a nightly scan automatically" in the plugin config. This only works if you've already set up Grav's Scheduler cron job on the server (* * * * * php bin/grav scheduler, per Grav's own Scheduler docs) — the plugin doesn't add that cron job for you.

Behavior / limitations

  • Only shrinks images bigger than the configured max width/height (aspect ratio preserved) — never upscales, and re-running the scan is safe/cheap since already-small images are skipped.
  • Overwrites the original file. There's no undo — back up user/pages (or your whole site) before the first run if you're not already using git or regular backups.
  • Supports jpg, png, gif, and webp. Animated GIFs and animated WEBPs are detected and skipped, since GD would flatten them to one frame.
  • Phone photos stored sideways with an EXIF orientation tag are turned upright before shrinking (GD drops the tag, so they'd otherwise show rotated). Needs PHP's exif extension, which most hosts have.
  • An image too large to open within PHP's memory_limit is skipped and counted as an error, instead of stopping the whole scan.
  • Each shrunk image is written to a temporary file that then replaces the original, so a failure part-way never leaves a half-written image.
  • Only one scan runs at a time. Starting another (from the admin, the CLI or the nightly job) while one is running reports that and does nothing.
  • Symlinked images are never rewritten, since they could point outside user/pages.
  • exclude_folders matches whole folders: excluding 02.blog skips it and everything inside, but not 03.blog-archive.
  • Requires the GD extension (near-universal on shared hosting; WEBP support within GD depends on how PHP was compiled — if imagewebp/ imagecreatefromwebp aren't available, webp files are safely skipped and counted as errors so you'll see it in the summary).

Why this looks different from older Grav plugin tutorials

Grav 2.0's Admin2 is a from-scratch Svelte SPA that talks only to a separate grav-plugin-api REST plugin — it does not fire the classic onAdminSave event family. So instead of a Twig task/button, this plugin adds its button via the onApiMenubarItems / onApiMenubarAction events, which is the current (2.0) way to hook a one-click action into the Admin2 toolbar.

Tests

GRAV_ROOT=/path/to/grav php tests/imageshrink-test.php

They run on a throwaway folder and cover shrinking, orientation, animated images, symlinks, the memory guard, exclusions and the one-scan-at-a-time lock.

Licence

MIT.