Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
3 changes: 3 additions & 0 deletions src/Storage/DBFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
38 changes: 38 additions & 0 deletions tests/php/ImageTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions tests/php/Storage/DBFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Loading