Turns subfolders of images inside a page into galleries, with Admin2 upload UI and automatic image optimization. Grav plugin.
  • PHP 76.7%
  • JavaScript 13.6%
  • CSS 7.5%
  • Twig 2.2%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-11-15 11:37:38 +00:00
classes 3.3.0: Grav version, homepage and issues links in the same layout as the other plugins 2026-11-15 11:37:38 +00:00
css AVIF and WebP tile copies, more sizes, blurred placeholders (3.0.0) 2026-10-27 21:12:56 +00:00
docs/images Story layout; lightbox zoom, swipe, counter and preloading 2026-10-27 03:09:34 +00:00
fonts Watermark and licence line (1.7.0) 2026-10-27 17:25:22 +00:00
js Photo map (2.0.0) 2026-10-27 17:46:11 +00:00
templates/subfolder-gallery AVIF and WebP tile copies, more sizes, blurred placeholders (3.0.0) 2026-10-27 21:12:56 +00:00
tests 3.2.0: photography pages' empty licence line means none; tests for PNG logos on JPEGs 2026-10-29 21:57:22 +00:00
vendor/leaflet Photo map (2.0.0) 2026-10-27 17:46:11 +00:00
blueprints.yaml 3.3.0: Grav version, homepage and issues links in the same layout as the other plugins 2026-11-15 11:37:38 +00:00
CHANGELOG.md 3.3.0: Grav version, homepage and issues links in the same layout as the other plugins 2026-11-15 11:37:38 +00:00
LICENSE v1.3.2: author Rawrso.me - David Hall 2026-10-11 17:36:15 +00:00
README.md 3.2.0: photography pages' empty licence line means none; tests for PNG logos on JPEGs 2026-10-29 21:57:22 +00:00
subfolder-gallery.php 3.2.0: photography pages' empty licence line means none; tests for PNG logos on JPEGs 2026-10-29 21:57:22 +00:00
subfolder-gallery.yaml 3.2.0: photography pages' empty licence line means none; tests for PNG logos on JPEGs 2026-10-29 21:57:22 +00:00

Subfolder Gallery

Automatically builds an image gallery from any subfolder of images inside a blog post's page folder. If a post has more than one such subfolder, each becomes its own gallery, positioned with a shortcode. Uploading is done entirely from Grav Admin2's own page editor - nothing to install or edit anywhere else, and the front end shows nothing but the rendered gallery, no extra buttons or links.

Built against Grav 2.0.17 and tested on Grav 2.2.4 with Admin2 + the API plugin. No third-party libraries, Composer packages, or custom JavaScript admin components - it uses Grav core's own documented self@:<subfolder> file upload destination and the API plugin's onApiBlueprintResolved event, so uploads go through Admin2's native drag-and-drop file field. Image resizing uses PHP's built-in GD extension (standard on cPanel).

Known limitation: the upload field doesn't preview images that were already uploaded in a previous save - you'll see an empty dropzone each time you open the editor, even if the folder already has images in it. This is a limitation of how Admin2 handles self@: subfolder destinations for upload fields specifically. To work around it, each gallery slot also gets a read-only Browse field showing current contents (thumbnails included). Deleting an unwanted upload isn't possible from the editor - a text-field-based attempt at this didn't work, because Admin2 doesn't track changes typed into a dynamically-added field for saving. Use FTP/file manager to remove an unwanted image for now.

Install

git clone forgejo@code.hall.me.uk:david/subfolder-gallery.git user/plugins/subfolder-gallery

Or copy the subfolder-gallery folder into user/plugins/ manually.

  1. In Admin2: Plugins → Subfolder Gallery → enable it.
  2. While you're there, check Gallery folder names under "Gallery upload slots" - this is the list of subfolder names you'll use across your site (default: gallery, gallery-2, gallery-3). Each name becomes its own upload field in every page's editor.

Uploading images

Open any post in Admin2's page editor:

  • Page-root images (no subfolder): use the editor's normal Media panel, exactly as you would for any other page - nothing special needed, and this one previews existing uploads normally.

  • Subfolder galleries: open the Galleries tab. Each configured slot gives you two fields:

    • Gallery: <slot> - drag images here to upload into that subfolder.
    • Browse: <slot> - read-only, shows what's currently in the folder.

    To remove an unwanted upload, delete it via FTP/file manager - there's no in-editor delete yet (a text-field-based attempt didn't work reliably, see the limitation above).

The Galleries tab: an upload field and a read-only browse field for each gallery slot

There is nothing to configure per-post beyond that - which slots have images in them is exactly what determines which galleries exist on that page.

How galleries are found on the front end

A page's own folder is scanned for images:

  • Directly in the page folder (no subfolder) → the page-root gallery, referenced as [gallery:_page], or picked up first by a bare [gallery].
  • Inside a subfolder (e.g. gallery/, gallery-2/) → a gallery named after that subfolder, e.g. [gallery:gallery], [gallery:gallery-2].

Folders that contain a .md file (i.e. are actual Grav subpages) are always ignored, as are any folder names in exclude_folders (default: assets, thumbs, thumbnails).

Positioning galleries with the shortcode

In the post's Markdown content:

Some intro text about the trip.

[gallery:gallery]

More text about the hike.

[gallery:gallery-2]

A bare [gallery] shortcode consumes galleries in folder order (alphabetical) each time it's used. Any gallery folders not placed via a shortcode are automatically appended to the bottom of the page (auto_append, on by default) - so if you don't add any shortcodes at all, uploaded galleries still show up, just at the end of the post.

Image sizes and formats

Gallery tiles show smaller copies of the photos, 320, 640, 960 and 1280 pixels wide, and the browser picks the one that suits the screen. Each size comes in three formats:

  • AVIF (formats.avif), about a quarter of the size of a JPEG that looks the same. Every current browser shows it. Made with PHP's GD if it was built with AVIF, otherwise with the Imagick extension; if the server has neither, AVIF is skipped.
  • WebP (formats.webp), about half the size of the JPEG, for browsers without AVIF.
  • The photo's own format (JPEG, PNG or WebP), for anything older.

The copies in the photo's own format are made when Grav builds the page. The AVIF and WebP copies are made the first time a browser asks for each one (AVIF takes a moment to make: about a third of a second for the largest), then kept. All of them are kept in images/subfolder-gallery/ and carry no metadata, so never the place a photo was taken. If the image cache is cleared, they are made again when next asked for. AVIF quality follows jpeg_quality, lower by 22 (82 gives 60), which looks the same as the JPEG.

Blurred placeholders (placeholders): while a tile's photo loads, the tile shows a blurred version of it instead of an empty box. It's a 16-pixel-wide image written into the page (about 150 bytes a photo), made once per photo. Not for PNGs and GIFs, which may be see-through.

Clicking a tile, and the download button, still give the original (or, with the watermark on, a marked full-size copy). Copies aren't made for photos that are already small. GIFs use Grav's own copies in their own format, and none for animated GIFs (they would lose their animation). On pages protected by Page Password or the Login plugin's access rules, public copies would be out of reach of the protection, so galleries there use their originals, unless the watermark is on (see below). If you protect a page whose gallery has already been shown to visitors, clear the image cache, so the public copies made before go.

You may want to clear the image cache once after updating to 3.0.0. Grav's old copies of gallery photos are no longer used, and clearing removes them.

Layouts

Every gallery renders in one of seven layouts, all with click-to-enlarge lightbox behaviour on top:

Grid Masonry Justified
Grid layout Masonry layout Justified layout
Portrait Mosaic Carousel
Portrait layout Mosaic layout Carousel layout
Story
Story layout
  • Grid — even, cropped thumbnails in a responsive grid (the classic look).
  • Masonry — natural aspect ratio, Pinterest-style flowing columns.
  • Justified — equal-height rows sized to each photo's natural aspect ratio (like Grid and Masonry's photography-portfolio cousin) - a mix of portrait and landscape photos wrap into rows without anything being cropped or looking oddly stretched next to the others.
  • Portrait — fixed, taller grid cells that never crop; a photo that doesn't fill the cell is letterboxed instead of having its edges cut off. Good when a gallery is mostly portrait-orientation shots.
  • Mosaic — a grid with occasional larger "feature" tiles mixed among regular ones, magazine-style. Like Grid, images are cropped to fit their cell - this one's about visual rhythm and variety, not preserving every aspect ratio.
  • Carousel — a horizontal scrolling slider with prev/next buttons.
  • Story — one photo per row, the full width of the text, each at its own shape (never cropped), with its camera details underneath. A tall photo is held to the window's height. For photo essays.

Set a site-wide default in the plugin config (default_layout), and override it per gallery by adding the layout name as a third part of the shortcode:

[gallery:gallery]              " uses the site default layout
[gallery:gallery:masonry]      " forces masonry for this gallery
[gallery:gallery-2:carousel]
[gallery:beach:justified]
[gallery:portraits:portrait]
[gallery:trip:mosaic]
[gallery:walk:story]
[gallery::carousel]            " next unplaced gallery, forced to carousel
[gallery:masonry]              " also works: shorthand for [gallery::masonry]
                                " when no folder is actually named "masonry"

The columns setting controls the column count for grid/masonry/portrait/ mosaic and the number of visible slides in the carousel. Justified doesn't use a fixed column count - row height adapts responsively instead (220px on desktop, narrower on smaller screens).

The lightbox

Click any thumbnail to open it full size, on a near-black background, with "3 / 12" in the corner. Arrow keys, the side buttons or a swipe (a drag with the mouse) move through that gallery only; the next and previous photos are fetched ahead, so moving on is instant. To look closer, pinch, double-tap, double-click, use Ctrl + scroll or the + and - keys, then drag to look around; Escape zooms back out, and a second Escape (or the close button, or a click on the dark area) closes it. Keyboard focus returns to the thumbnail you opened, and screen readers announce it as an image viewer.

The lightbox

Under each photo the lightbox shows its camera details, when the photo has them: camera, lens, focal length, aperture, shutter speed, ISO and the date it was taken (camera_details, on by default). Never the location.

The lightbox with a line of camera details under the photo

Blog listings

If a post's summary includes its gallery (short posts often do), the gallery shows on the listing page too, with working links, styles and lightbox. Each post's galleries open in their own lightbox group, so paging through one post's photos never runs into the next post's.

Grav cuts a summary off at a fixed length, which can slice through a gallery and leave one thumbnail and a "…" on the listing. To choose what the listing shows, end the summary yourself with Grav's summary delimiter (=== on a line of its own) before the first gallery.

Automatic image resizing

After you save a page in Admin2, the plugin checks every image in that page's galleries and shrinks any that are wider or taller than the configured max_width / max_height (default 2000 × 2000px), re-saving at jpeg_quality (default 82) for JPEG/WebP or png_compression (default 6) for PNG. Images already within bounds are left untouched, so they aren't repeatedly re-compressed on every save. Requires PHP's GD extension (standard on cPanel); if it's unavailable, resizing is silently skipped.

The resize is careful with your originals:

  • Phone photos stay upright. Photos stored sideways with an EXIF orientation tag are turned upright before resizing (GD would otherwise drop the tag and the photo would show rotated). Needs PHP's exif extension, which most hosts have; without it, photos are resized as stored.
  • Animated GIFs are left alone, since GD would keep only the first frame.
  • Images too big for PHP's memory limit are skipped with a warning in Grav's log, rather than failing the save. Shrink those before uploading, or raise memory_limit.
  • No half-written files. The resized image is written to a temporary file that then replaces the original, so a failure part-way leaves the original as it was.

There's no undo for a resize that succeeds, so keep your originals elsewhere.

Visitor downloads

When allow_downloads is on (default), each thumbnail shows a small download button, and the lightbox has one too. These use the HTML download attribute, so images open a "Save As" dialog rather than navigating to the raw image.

Photo location and details

Phones and many cameras record where a photo was taken, and the original file, with that location in it, is what the lightbox shows and visitors download. So the first time the plugin sees a photo (when it's uploaded to the page in Admin2, when the page is saved, or when the page is next built after a photo is copied in by FTP) it:

  1. reads its camera details and location into a private record in user/data/subfolder-gallery/photos/ (one small JSON file per photo, which the web server never serves; the location is kept there for the site's own use, such as a map of where photos were taken);
  2. removes the location from the photo file itself (remove_metadata).

Only the metadata changes: the picture data is copied byte for byte, never re-compressed. With Location only (the default), the GPS position is cleared from the EXIF, and the XMP and IPTC blocks, which can repeat it or name the place, are dropped; the camera details and orientation stay in the file. Everything but the orientation leaves only which way up the photo goes. Nothing leaves photos as they are. JPEG, PNG and WebP are handled; a photo that can't be read safely is left alone.

This changes the photo files on the server, so the location can't be had back from them later; keep your own copies. Reading needs PHP's exif extension; without it nothing is recorded, though the location is still removed.

Photo map

Put [photo-map] on a line of its own in a page for a map of where its gallery photos were taken, or [photo-map:section] for that page and every page below it (for example on the front page of a photography section). Each marker shows how many photos were taken there; click it for the photos. On the photos' own page a photo opens in the viewer; on a section map it links to its post. Markers that would overlap share one, which zooms in when clicked.

The places come from the photos' private records (see above), so they work even though the location has been removed from the files. They are moved to the middle of a grid square about 1 km across (or 10 km, setting Map precision) before they reach the page, and the map can't be zoomed in closer than that, so the exact place never leaves the server. Only photos in gallery subfolders are mapped, not a page's own images. A section map leaves out pages that are unpublished, not routable, or protected by Page Password or the Login plugin, and everything below them. A section map on a page that is itself protected shows nothing; put it on an open page. A page with no located photos shows no map.

The map is drawn with Leaflet 1.9.4, which comes with the plugin (in vendor/leaflet/) and is fetched only on pages with a map. The map pictures come from OpenStreetMap by default (Map tiles and Map credit to change it); the site's Content-Security-Policy must allow that host for images, as Anti-Asteroid's does for tile.openstreetmap.org. Check the tile provider's usage policy for a busy site.

A photography section: albums and tags

There's nothing extra to install. Make one page the section's front page with Grav's usual listing set-up (with Anti-Asteroid or Quark, a blog.md page whose front matter has content: items: '@self.children'), and make each album a post below it with its photos in a gallery subfolder:

08.photography/
    blog.md                 # "Where the photos were taken" and [photo-map:section]
    01.edinburgh/
        item.md             # taxonomy: tag: [scotland, city]
        gallery/            # the album's photos
  • Album tiles: each post's tile on the listing shows its first gallery photo (the watermarked copy, when the watermark is on). With Anti-Asteroid 2.7 and later, a post locked by Page Password shows no picture until it's unlocked, instead of a broken one.
  • Tags: give each album tags in its front matter (taxonomy: tag: [...]). The theme shows them on the tile and the post, each linking to the listing filtered to that tag (/photography/tag:scotland).
  • Map: [photo-map:section] on the front page maps every album; [photo-map] in an album maps just that one.

Settings of their own

Photography pages can behave differently from blog posts. In Admin2 (Plugins → Subfolder Gallery, Photography pages), list the photography sections (/photography by default): every page below one of them, and the section's own page, uses the settings in that part instead of the ordinary ones. Each is Same as other pages (or empty) until you set it, except the licence line:

  • Layout and columns: the default layout and number of columns. A shortcode's own layout ([gallery:beach:masonry]) still wins.
  • Viewer and downloads: the lightbox, the download button and camera details.
  • Protection: the watermark (on or off, text, logo, strength, logo width), the licence line, the photographer and the two licence pages given to search engines. The licence line is the photography pages' own: left empty, they have none, whatever other pages show.
  • Quality and metadata: the maximum photo size, JPEG and PNG quality, and what is removed from photo files.

For example, photography pages can use the Story layout with downloads off and a stronger watermark, while blog posts keep a grid with downloads on and no watermark. Some changes only reach photos the plugin handles from then on: what is removed from a file, and the size it's cut down to on upload. After changing a watermark, clear Grav's image cache as usual.

Search engines

With SEO Manager 1.4 or later, each gallery photo is described in the page's structured data, which Google can show with the photo in image search:

  • Photographer (photographer): your name, as the photo's creator and credit.
  • Licence line (licence): as its copyright notice.
  • Licence page (licence_url): a page setting out how the photos may be used.
  • How to buy or ask to use a photo (licence_page): for example a contact page.
  • The date each photo was taken, from its private record.

The location is never included, and a protected page lists no photos. The photos also go in SEO Manager's image sitemap (by address only: Google no longer reads anything else there).

Licence line (licence): a line such as "© David Hall. All rights reserved." shown under each gallery and under the photo in the viewer. Leave it empty for none.

Watermark (watermark.enabled, off by default): every copy of a gallery photo (one in a gallery subfolder) that a visitor can get carries a mark, and the original is never sent. That covers:

  • the gallery tiles (the plugin's copies, see Image sizes and formats);
  • the photo in the viewer, the download button, and anyone who goes to a photo's own address (a marked full-size copy, saved under the photo's own name);
  • Grav's own copies of photos the plugin has seen, made by the theme for blog tiles, a page's hero picture or its share image. These are marked as Grav saves them.

A page's own images (those in its folder, not in a subfolder: its cover, share image and pictures in the post) aren't marked, even when they show as the page's root gallery. Grav's copies carry only the photo's name, so a page's own image with the same name as a gallery photo is marked as well; give them different names.

The mark is a line of text in the bottom-right corner (watermark.text, default © {site}, where {site} is the site's title and {year} this year) and, if you upload one, a logo drawn faintly in the middle (watermark.logo: PNG with transparency, JPEG, WebP or SVG, up to 2 MB; an SVG needs PHP's Imagick extension, and without it only the text is drawn). Watermark strength (0.03 to 0.6) sets how strongly the logo shows; the text is drawn stronger so it stays readable. Logo width is the logo's width as a share of the photo's. Both are sized to each photo, so a small copy carries the same mark as the full one. The text uses DejaVu Sans, which comes with the plugin.

Things to know:

  • The original must not be reachable another way. The photos stay unmarked on the server and the plugin answers for them through the page's address. This needs Grav's pages.media_route_urls: true, and the .htaccess rule that blocks /user/pages/, which are also what Page Password needs. Keep your own copies of the originals.
  • After changing the mark, clear Grav's image cache (the images option of Admin2's clear-cache button, or bin/grav clearcache --images-only). The plugin's own copies are remade automatically, because their addresses change with the mark. Grav's copies (blog tiles and the like) keep their names, so they are only remade after a clear. A copy that has gone missing is made again when it's next asked for.
  • On pages protected by Page Password or the Login plugin's access rules, tile copies are served through the page's address (photo.jpg?sfg=640, photo.jpg?sfg=640.avif) so the protection still applies, with Cache-Control: private.
  • The full-size marked copies are kept in cache/subfolder-gallery/, out of the web server's reach. Making one takes a moment the first time a photo is opened.
  • GIFs aren't marked (marking would lose their animation); Grav serves them as they are. Keep photos you want marked as JPEG, PNG or WebP.
  • A photo too big to open within PHP's memory limit can't be marked, and isn't shown at all (the server answers "not available" rather than send the original). With the default 2000-pixel resize limit this doesn't happen.
  • A watermark discourages copying and shows whose photo it is. It won't stop someone determined to remove it.

Configuration

Set in Admin2: Plugins → Subfolder Gallery, or in user/config/plugins/subfolder-gallery.yaml:

Option Default Description
enabled true Enable/disable the plugin
columns 3 Columns (grid/masonry) or visible slides (carousel)
default_layout grid grid, masonry, justified, portrait, mosaic, carousel or story — overridable per shortcode
lightbox true Click-to-enlarge lightbox with prev/next/keyboard
allow_downloads true Show a download button on thumbnails and the lightbox
auto_append true Append galleries not placed via shortcode
gallery_slots gallery, gallery-2, gallery-3 Subfolder names that get an upload field in the editor
image_extensions jpg, jpeg, png, gif, webp Which files count as images
exclude_folders assets, thumbs, thumbnails Folder names to never treat as galleries
max_width 2000 Max width (px) before an image is resized
max_height 2000 Max height (px) before an image is resized
jpeg_quality 82 JPEG/WebP save quality (1-100)
png_compression 6 PNG compression level (0-9, lossless either way)
formats.avif true AVIF copies for gallery tiles, where the server can make them
formats.webp true WebP copies for gallery tiles
placeholders true A blurred stand-in in each tile while its photo loads
remove_metadata location Removed from photo files: location, all (keeps only the orientation) or none
camera_details true Camera details under each photo in the lightbox
map.precision 1 Photo map grid in km: 1 or 10
map.height 420 Photo map height in pixels
map.tile_url OpenStreetMap Map tiles, with {z}, {x} and {y}
map.attribution OpenStreetMap's credit Credit shown in the map's corner
photographer '' Creator of the photos, for search engines
licence_url '' Page setting out how the photos may be used
licence_page '' Page saying how to get permission to use a photo
licence '' Licence line under each gallery and in the viewer
watermark.enabled false Watermark every copy visitors get (see above)
watermark.text © {site} Text in the bottom-right corner; {site}, {year}
watermark.logo none Logo drawn faintly in the middle (uploaded in Admin2)
watermark.opacity 0.15 How strongly the logo shows (0.03-0.6)
watermark.logo_width 0.3 Logo width as a share of the photo's (0.1-0.8)
photography.sections [/photography] Sections whose pages use the photography.* settings
photography.<setting> empty The photography pages' own columns, default_layout, lightbox, allow_downloads, camera_details, remove_metadata, licence, photographer, licence_url, licence_page, watermark.*, max_width, max_height, jpeg_quality or png_compression; empty for the same as other pages (on/off ones take 'on' or 'off'), except licence, where empty means none

Notes / limitations

  • Gallery slot names are a small, site-wide configured list rather than freely typed per post - this is what lets uploads use Admin2's native file field with no custom code. If you need a one-off gallery name outside your configured slots, you can still create the subfolder and drop images in via FTP/file manager; the front end will pick it up as a gallery automatically (it just won't have its own upload field in the editor unless you add that name to gallery_slots).
  • The upload field not previewing already-uploaded files (see the top of this file) is the main rough edge - everything else (upload, resize, front-end display, downloads, layouts) works normally.
  • Images are served as static files directly from the page folder (the same mechanism Grav already uses for images referenced in Markdown), so no extra webserver configuration is needed on cPanel.

Tests

php tests/metadata-test.php        # reading details, removing the location (JPEG, PNG, WebP)
php tests/watermark-test.php       # drawing the mark, copy sizes and orientation, photos it can't mark
php tests/image-copy-test.php      # WebP and AVIF copies, transparency, no metadata, placeholders
php tests/fixtures.php <folder> 3 1600x1067 55.95,-3.19  # three test photos with camera details, taken at that place

Licence

MIT.