Skip to content

Guard crops against stale cached dimensions and clamp focus coordinates - #130

Open
lerni wants to merge 1 commit into
jonom:masterfrom
lerni:guard-stale-dimensions-and-clamp-coords
Open

lerni wants to merge 1 commit into
jonom:masterfrom
lerni:guard-stale-dimensions-and-clamp-coords

Conversation

@lerni

@lerni lerni commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

Problem

calculateCrop() uses the cached FocusPointWidth/FocusPointHeight, and applyCrop() applies the result to the real image. If the cache no longer matches the file, the crop box lies outside the image and the backend throws (vips: extract_area: bad extract area), so the page fails to render.

This is easy to hit with the image editor added in silverstripe/asset-admin 3.3 (CMS 6.3). It crops, rotates or flips an existing image in place, so the file's dimensions change. If the cached size is not refreshed, every FocusFill*() call on that image fails. The cause on the assets side is that the File keeps its cached image backend after the asset is replaced, so getWidth()/getHeight() return the old size and FocusPoint's onBeforeWrite() stores it again. That is reported in silverstripe/silverstripe-assets#724 and fixed in silverstripe/silverstripe-assets#725.

This PR makes FocusPoint safe regardless of how the cache went stale (other modules, external file replacement, DB copies, or an unfixed assets version).

Change

  • applyCrop(): if the backend size differs from the cached size, fall back to a centered croppedResize() instead of an out-of-bounds crop. Nothing is written to the DB, so it is safe on read paths.
  • getX()/getY(): clamp to [-1, 1]. The setters already do, but values hydrated from the DB did not.

The focus point is not remapped after an edit. That can't be done reliably from inside this module, so the fallback centers the crop until the dimensions are refreshed.

Tests

Added a clamp test and a stale-dimensions regression test, which fails with bad extract area without this change. testFocusFill and testFocusFillMax already fail on Intervention Image 4 because pickColor() no longer exists. That is unrelated to this PR.

applyCrop() computed its crop box from the cached FocusPointWidth/Height.
If these no longer match the file (e.g. after the file was replaced), the
box lay outside the image and the backend threw ("bad extract area").
Fall back to a centered crop when the backend size differs from the cached
size, and clamp getX()/getY() to [-1, 1] so DB values can never address a
point outside the image.
@lerni

lerni commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Update: the root cause is fixed in silverstripe-assets 3.3.4 (silverstripe/silverstripe-assets#725, which resets the cached image backend). This PR is still useful as a safety net for assets < 3.3.4 and for other ways the cached dimensions can go stale.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant