Repository navigation
FIX Reset cached image backend when replacing file content - #725
Merged
michalkleiner merged 1 commit intoNov 19, 2026
Merged
michalkleiner merged 1 commit into
michalkleiner merged 1 commit into
Conversation
Contributor
|
Thanks for your contribution @lerni. The change looks good, I have just one follow up.
|
lerni
force-pushed
the
pulls/3.3/reset-image-backend-on-file-replace
branch
from
November 17, 2026 18:39
2f80e35 to
7bb9d31
Compare
Contributor
Author
|
Thank you @michalkleiner for the review. |
…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
force-pushed
the
pulls/3.3/reset-image-backend-on-file-replace
branch
from
November 19, 2026 09:42
7bb9d31 to
75d43f3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
ImageManipulation::getImageBackend()caches theImage_Backendon theFilerecord.File::setFromLocalFile(),setFromStream()andsetFromString()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-admin3.3ImageEditor::editImage()callsgetImageBackend()to render, thenreplaceOriginal()doessetFromString()+write(). After a crop/rotate/resize the record still reports the old size.onBeforeWrite()hooks that cache dimensions persist the stale values. For examplejonom/focuspointwritesgetWidth()/getHeight()intoFocusPointWidth/FocusPointHeightwhenFileHashchanges, and later crops computed from them fall outside the real image.The fix clears
$this->imageBackendin the threesetFrom*()methods when the replacement succeeded, so the nextgetImageBackend()call builds a backend for the new hash.testSetFromMethodsResetImageBackend()covers all three methods.Manual testing steps
Imageand callgetWidth()(this loads the backend and resource).$image->setFromString(file_get_contents($otherImage), $image->getFilename())with an image of different dimensions.$image->getWidth()/getHeight()now return the new size (before: the old size).Issues
#724
Pull request checklist