- PHP 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| classes | ||
| cli | ||
| docs/images | ||
| tests | ||
| blueprints.yaml | ||
| CHANGELOG.md | ||
| imageshrink.php | ||
| imageshrink.yaml | ||
| LICENSE | ||
| README.md | ||
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/pagestree synchronously and reports how many images it shrunk.Confirm Result 

Only accounts with the
api.media.writepermission (orapi.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
exifextension, which most hosts have. - An image too large to open within PHP's
memory_limitis 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_foldersmatches whole folders: excluding02.blogskips it and everything inside, but not03.blog-archive.- Requires the GD extension (near-universal on shared hosting; WEBP support
within GD depends on how PHP was compiled — if
imagewebp/imagecreatefromwebparen'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.
