From 75d43f32b64bb2a78367ab2d58110d0f78863bc9 Mon Sep 17 00:00:00 2001 From: Lukas Erni Date: Sat, 14 Nov 2026 10:26:45 +0100 Subject: [PATCH] FIX Reset cached image backend when replacing file content (File and 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. --- src/File.php | 3 +++ src/Storage/DBFile.php | 3 +++ tests/php/ImageTestBase.php | 38 ++++++++++++++++++++++++++++++++ tests/php/Storage/DBFileTest.php | 38 ++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+) diff --git a/src/File.php b/src/File.php index 95b66973..8b54ee78 100644 --- a/src/File.php +++ b/src/File.php @@ -1286,6 +1286,7 @@ public function setFromLocalFile($path, $filename = null, $hash = null, $variant // Update File record to name of the uploaded asset if ($result) { $this->setFilename($result['Filename']); + $this->imageBackend = null; } return $result; } @@ -1297,6 +1298,7 @@ public function setFromStream($stream, $filename, $hash = null, $variant = null, // Update File record to name of the uploaded asset if ($result) { $this->setFilename($result['Filename']); + $this->imageBackend = null; } return $result; } @@ -1308,6 +1310,7 @@ public function setFromString($data, $filename, $hash = null, $variant = null, $ // Update File record to name of the uploaded asset if ($result) { $this->setFilename($result['Filename']); + $this->imageBackend = null; } return $result; } diff --git a/src/Storage/DBFile.php b/src/Storage/DBFile.php index a0994a3c..b0845ddf 100644 --- a/src/Storage/DBFile.php +++ b/src/Storage/DBFile.php @@ -201,6 +201,7 @@ public function setFromLocalFile($path, $filename = null, $hash = null, $variant // Update from result if ($result) { $this->setValue($result); + $this->imageBackend = null; } return $result; } @@ -214,6 +215,7 @@ public function setFromStream($stream, $filename, $hash = null, $variant = null, // Update from result if ($result) { $this->setValue($result); + $this->imageBackend = null; } return $result; } @@ -227,6 +229,7 @@ public function setFromString($data, $filename, $hash = null, $variant = null, $ // Update from result if ($result) { $this->setValue($result); + $this->imageBackend = null; } return $result; } diff --git a/tests/php/ImageTestBase.php b/tests/php/ImageTestBase.php index a9713cab..ba74563d 100644 --- a/tests/php/ImageTestBase.php +++ b/tests/php/ImageTestBase.php @@ -580,6 +580,44 @@ public function getWidth(): int $this->assertEquals(99999, $image->getWidth()); } + public function testSetFromMethodsResetImageBackend(): void + { + $sourcePath = __DIR__ . '/ImageTest/landscape-to-portrait.jpg'; + + // Test setFromString() when the backend's image resource is already loaded + // (e.g. ImageEditor renders from getImageBackend()->getImageResource() then replaces the file) + $image = $this->objFromFixture(Image::class, 'imageWithTitle'); + $this->assertSame(300, $image->getWidth()); + $this->assertSame(300, $image->getHeight()); + $this->assertNotNull($image->getImageBackend()->getImageResource()); + $image->setFromString(file_get_contents($sourcePath), $image->getFilename()); + $this->assertSame(450, $image->getWidth()); + $this->assertSame(600, $image->getHeight()); + + // Test setFromLocalFile() + $image = $this->objFromFixture(Image::class, 'imageWithoutTitle'); + $this->assertSame(300, $image->getWidth()); + $this->assertNotNull($image->getImageBackend()->getImageResource()); + $image->setFromLocalFile($sourcePath, $image->getFilename()); + $this->assertSame(450, $image->getWidth()); + $this->assertSame(600, $image->getHeight()); + + // Test setFromStream() + $image = $this->objFromFixture(Image::class, 'imageWithoutTitleContainingDots'); + $this->assertSame(300, $image->getWidth()); + $this->assertNotNull($image->getImageBackend()->getImageResource()); + $stream = fopen($sourcePath, 'rb'); + try { + $image->setFromStream($stream, $image->getFilename()); + } finally { + if (is_resource($stream)) { + fclose($stream); + } + } + $this->assertSame(450, $image->getWidth()); + $this->assertSame(600, $image->getHeight()); + } + /** * @param $filename * @param $hash diff --git a/tests/php/Storage/DBFileTest.php b/tests/php/Storage/DBFileTest.php index 241dce34..ccc8c919 100644 --- a/tests/php/Storage/DBFileTest.php +++ b/tests/php/Storage/DBFileTest.php @@ -4,6 +4,7 @@ use Silverstripe\Assets\Dev\TestAssetStore; use SilverStripe\Assets\Storage\AssetStore; +use SilverStripe\Assets\Storage\DBFile; use SilverStripe\Assets\Tests\Storage\DBFileTest\TestFileWithPermissions; use SilverStripe\Control\Director; use SilverStripe\Dev\SapphireTest; @@ -59,6 +60,43 @@ public function testRender() ); } + public function testSetFromMethodsResetImageBackend(): void + { + $fish = realpath(__DIR__ . '/../ImageTest/test-image-high-quality.jpg'); + $replacement = realpath(__DIR__ . '/../ImageTest/landscape-to-portrait.jpg'); + $this->assertFileExists($fish); + $this->assertFileExists($replacement); + + $methods = [ + 'setFromLocalFile' => fn (DBFile $file) => $file->setFromLocalFile($replacement, 'replaced-local.jpg'), + 'setFromString' => fn (DBFile $file) => $file->setFromString( + file_get_contents($replacement), + 'replaced-string.jpg' + ), + 'setFromStream' => function (DBFile $file) use ($replacement) { + $stream = fopen($replacement, 'rb'); + try { + $file->setFromStream($stream, 'replaced-stream.jpg'); + } finally { + fclose($stream); + } + }, + ]; + + foreach ($methods as $name => $replace) { + $obj = new DBFileTest\TestObject(); + $obj->MyFile->setFromLocalFile($fish, "original-$name.jpg"); + + // Load the backend's image resource before replacing + $this->assertSame(300, $obj->MyFile->getWidth(), $name); + $this->assertNotNull($obj->MyFile->getImageBackend()->getImageResource(), $name); + + $replace($obj->MyFile); + $this->assertSame(450, $obj->MyFile->getWidth(), $name); + $this->assertSame(600, $obj->MyFile->getHeight(), $name); + } + } + public function testValidation() { $obj = new DBFileTest\ImageOnly();