- PHP 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
Declared dependency was grav >=1.7.0, but this plugin only works against Admin2 (onApiMenubarItems/onApiMenubarAction don't exist pre-2.0) — bumped to >=2.0.0 to match what it actually requires. Also swapped the yourname/grav-plugin-imageshrink GitHub placeholders for the real repo, and added the missing Licence section to the README. |
||
| classes | ||
| cli | ||
| 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. - 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.
- 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.
Licence
MIT.