Skip to content

FIX Reset cached image backend when replacing file content - #725

Merged
michalkleiner merged 1 commit into
silverstripe:3.3from
lerni:pulls/3.3/reset-image-backend-on-file-replace
Nov 19, 2026
Merged

michalkleiner merged 1 commit into
silverstripe:3.3from
lerni:pulls/3.3/reset-image-backend-on-file-replace

Conversation

@lerni

@lerni lerni commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Description

ImageManipulation::getImageBackend() caches the Image_Backend on the File record. File::setFromLocalFile(), setFromStream() and setFromString() replace the underlying asset but keep that cached backend. If its image resource was already loaded, getWidth()/getHeight() keep returning the pre-replacement dimensions until the record is reloaded.

This is not driver-specific (GD and Imagick behave the same) and is visible in two places:

  • silverstripe/asset-admin 3.3 ImageEditor::editImage() calls getImageBackend() to render, then replaceOriginal() does setFromString() + write(). After a crop/rotate/resize the record still reports the old size.
  • onBeforeWrite() hooks that cache dimensions persist the stale values. For example jonom/focuspoint writes getWidth()/getHeight() into FocusPointWidth/FocusPointHeight when FileHash changes, and later crops computed from them fall outside the real image.

The fix clears $this->imageBackend in the three setFrom*() methods when the replacement succeeded, so the next getImageBackend() call builds a backend for the new hash. testSetFromMethodsResetImageBackend() covers all three methods.

Manual testing steps

  1. Load an Image and call getWidth() (this loads the backend and resource).
  2. Call $image->setFromString(file_get_contents($otherImage), $image->getFilename()) with an image of different dimensions.
  3. $image->getWidth() / getHeight() now return the new size (before: the old size).
  4. In the CMS, crop an image in the image editor and check the stored dimensions match the file.

Issues

#724

Pull request checklist

  • The target branch is correct
  • All commits are relevant to the purpose of the PR (e.g. no debug statements, unrelated refactoring, or arbitrary linting)
    • Small amounts of additional linting are usually okay, but if it makes it hard to concentrate on the relevant changes, ask for the unrelated changes to be reverted, and submitted as a separate PR.
  • The commit messages follow our commit message guidelines
  • The PR follows our contribution guidelines
  • Code changes follow our coding conventions
  • This change is covered with tests (or tests aren't necessary for this change)
  • Any relevant User Help/Developer documentation is updated; for impactful changes, information is added to the changelog for the intended release
  • CI is green

@michalkleiner

Copy link
Copy Markdown
Contributor

Thanks for your contribution @lerni.

The change looks good, I have just one follow up.

DBFile also uses ImageManipulation and its equivalent replacement methods do not clear their own backend cache. Direct DBFile image fields could therefore exhibit the same problem. It is outside the issue’s explicit File/Image reproduction steps, but it might be worth addressing at the same time, if you get a chance?

@lerni
lerni force-pushed the pulls/3.3/reset-image-backend-on-file-replace branch from 2f80e35 to 7bb9d31 Compare November 17, 2026 18:39
@lerni

lerni commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

Thank you @michalkleiner for the review. DBFile now added.

…DBFile)

ImageManipulation::getImageBackend() caches the Image_Backend instance on the File record ($this->imageBackend). When a caller loads the image resource on that backend and then replaces the underlying asset on the same record via File::setFromLocalFile(), setFromStream(), or setFromString() (for example AssetAdmin's ImageEditor::editImage()), $this->imageBackend continues to hold the pre-replacement resource and dimensions. Subsequent getWidth()/getHeight() calls on the record then return the stale dimensions. This also affects onBeforeWrite() hooks that cache them, e.g. jonom/focuspoint stores them in FocusPointWidth/FocusPointHeight, so the old size is persisted whenever the file is replaced on an already loaded record.
@michalkleiner
michalkleiner force-pushed the pulls/3.3/reset-image-backend-on-file-replace branch from 7bb9d31 to 75d43f3 Compare November 19, 2026 09:42

@michalkleiner michalkleiner 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.

👍 MOG

@michalkleiner
michalkleiner merged commit bc40de0 into silverstripe:3.3 Nov 19, 2026
8 checks passed
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.

2 participants