Skip to content

Read image dimensions before decoding uploads for thumbnails - #315

Open
mraible wants to merge 15 commits into
masterfrom
bound-image-decoding
Open

mraible wants to merge 15 commits into
masterfrom
bound-image-decoding

Conversation

@mraible

@mraible mraible commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Thumbnail creation in JPAMediaFileManagerImpl now reads an image's width and height from its header with an ImageReader before decoding it, and decodes only what the thumbnail needs.

  • Images with more pixels than the new uploads.thumbnail.maxPixels setting (default 50,000,000) are stored without a thumbnail, and the skip is logged. The upload still succeeds and keeps the image's real width and height.
  • Other images are decoded with subsampling sized to about twice the thumbnail in both directions, so a large photo no longer has to be decoded at full size first. An image so much wider than tall (or taller than wide) that its thumbnail would be under a pixel across isn't decoded; such images never got a thumbnail.
  • Only JPEG, PNG, GIF and BMP content gets a thumbnail, whatever the file name or content type says. The TIFF reader reads a whole strip or tile at a time even when subsampling, so TIFF images are now stored without one. Their previews load the original, and show the file type icon in browsers that can't display TIFF.
  • A thumbnail request for an image without a thumbnail still answers with the original, so entries that embed the ?t=true address keep showing the image. For images over the limit, the media library and chooser cards, the upload result page and "create entry" check thumbnailSkipped themselves and show the file type icon or link the image directly.
  • Running out of memory while decoding leaves the image without a thumbnail instead of failing the upload after the file was saved.
  • When new content gets no thumbnail, including a replacement that isn't an image, the old content's thumbnail and size are removed. Content whose header can be read but whose pixels can't is stored without a size. A mistyped uploads.thumbnail.maxPixels (such as 50_000_000, a value over the int range, empty or 0) logs a warning and uses the default.

Tests: MediaFileThumbnailTest (DB-backed) covers an image over a configured limit, a normal image, a PNG whose header claims 50000x50000, a very wide image, a TIFF image (also under a .png name), an image that can't be decoded, and replacements by an over-limit image, unreadable content and a non-image. ThumbnailSubsamplingTest covers the subsampling step and the thumbnail size after a size change. MediaResourceThumbnailTest covers thumbnail requests. MediaFileTest still passes.

CHANGES lists the limit and the TIFF change under "Limits on uploads and fetched feeds".

Thumbnail creation now reads the image's width and height from its header with an ImageReader before decoding it. Images with more pixels than the new uploads.thumbnail.maxPixels setting (default 50,000,000) are stored without a thumbnail, and the skip is logged. Other images are decoded with subsampling sized to the thumbnail. The upload's content stream is now closed, and content that no image reader recognizes gets no thumbnail whatever content type the client sent.

@snoopdave snoopdave left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of 09480c48e: The header check and subsampled read are present. PR #327 has the same fix hunks; its diff differs only in branch context. The skip path needs changes for replaced files and for how oversized images appear in the UI.

The width and height read from an image's header are now saved before the pixel limit is checked, so an image too large for a thumbnail still shows its real size. When a file's new content gets no thumbnail, for any reason, the thumbnail left from its previous content is removed, so a replaced image no longer shows the old image's thumbnail.

MediaResourceServlet answers 404 to a thumbnail request for an image over uploads.thumbnail.maxPixels instead of sending the full image, so the media library and chooser show the file type icon. Other images without a thumbnail are still served in full at the thumbnail address. The upload result page shows the file type icon for such images, and inserting one into an entry uses the image's own address.
@mraible
mraible requested a review from snoopdave November 18, 2026 04:16
# Conflicts:
#	app/src/main/java/org/apache/roller/weblogger/ui/rendering/servlets/MediaResourceServlet.java
A value of uploads.thumbnail.maxPixels that is not a positive int (for example 50_000_000, 3000000000, an empty value or 0) used to throw NumberFormatException out of the media servlet and the media library pages, and silently stopped thumbnails being made on upload. The limit is now read in one place, MediaFile.thumbnailMaxPixels(), which falls back to the default and logs a warning once for each bad value.

When a file's content is replaced with something no image reader recognizes, or whose header can't be read, the stored width and height are reset to -1 (unknown), so the previous image's size no longer marks the new content as over the limit.

The derived overThumbnailPixelLimit getter is listed as transient in MediaFile.orm.xml like the other derived getters.

@snoopdave snoopdave left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of c945f86e0: The earlier review points are resolved, and the tests cover the upload skip and thumbnail response paths. One issue remains: thumbnail availability is inferred from the current pixel limit, which can differ from the limit used when the file was uploaded.

Earlier comments: 3 of 3 resolved.

Whether an image has a thumbnail was worked out from the current uploads.thumbnail.maxPixels setting, but the thumbnail was made or skipped at upload time, so changing the limit later could make a file with a thumbnail look like it had none, or the other way around. isOverThumbnailPixelLimit() is replaced by isThumbnailSkipped(), which is true for an image whose size was read on upload but which has no _sm file stored, whatever the limit is now.
The limit-change tests now end the session before reloading the file without its content, as the list views load it, so whether it has a thumbnail is looked up in the file store rather than read from the instance loaded during upload.
Most images on an upgraded blog have no _sm file, because they were carried over from older versions or by migrations, so treating every image without one as skipped would have taken away their previews. An image now counts as skipped only when it has no _sm file and its recorded size is over the current uploads.thumbnail.maxPixels; an image without a thumbnail under the limit is previewed from the original as before, and one with a thumbnail always gets it.
# Conflicts:
#	app/src/main/java/org/apache/roller/weblogger/ui/rendering/servlets/MediaResourceServlet.java
… to scale

The subsampling step now comes from whichever side needs the larger step, so both sides are decoded at about twice the thumbnail size. An image so much wider than tall, or taller than wide, that its thumbnail would be less than a pixel across is no longer decoded, since no thumbnail could be made from it anyway. Setting a media file's width or height also resets its cached thumbnail size, so a replaced image's thumbnail gets the new image's shape.
These are the formats whose image readers decode a subsampled image row by row. The TIFF reader reads a whole strip or tile into memory even when subsampling, so TIFF images, and any other format a reader is installed for, are now stored with their size and without a thumbnail. The format comes from the content, not the file name.
Running out of memory while decoding an image for its thumbnail is logged and leaves the image without a thumbnail, instead of failing the upload after the file was saved. An image whose header can be read but whose pixels can't is stored without a size, as it was before the size came from the header. A thumbnail that was saved is kept when a later step fails, and removing an old thumbnail also clears it from the media file and logs failures other than a missing file.
Entries that embed an image's thumbnail address showed a broken image once the image was stored without a thumbnail, because the thumbnail request answered 404. The thumbnail request now falls back to the original, as it does for any image without a thumbnail. The media library and chooser cards show the file type icon for such an image themselves, so they don't load the original as a preview.
…wn name

The invalid-limit test named its uploads by the length of the limit value, so two of them shared a name.
…hat isn't an image

Before, the replaced image's thumbnail was still served at the file's thumbnail address.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants