1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-23 14:02:47 +02:00

Fix bug in File::class

The missing call to rewind() resulted in the file being saved with empty
content if File::save() was called twice in succession.
This commit is contained in:
Oliver Vogel
2025-02-27 14:08:55 +01:00
parent 4592f17b5c
commit ebbb711871
2 changed files with 17 additions and 7 deletions

View File

@@ -70,7 +70,7 @@ class File implements FileInterface, Stringable
}
// write data
$saved = @file_put_contents($filepath, $this->pointer);
$saved = @file_put_contents($filepath, $this->toFilePointer());
if ($saved === false) {
throw new NotWritableException(
sprintf("Can't write image data to path (%s).", $filepath)
@@ -85,7 +85,7 @@ class File implements FileInterface, Stringable
*/
public function toString(): string
{
return stream_get_contents($this->pointer, offset: 0);
return stream_get_contents($this->toFilePointer(), offset: 0);
}
/**
@@ -107,7 +107,7 @@ class File implements FileInterface, Stringable
*/
public function size(): int
{
$info = fstat($this->pointer);
$info = fstat($this->toFilePointer());
return intval($info['size']);
}

View File

@@ -41,11 +41,21 @@ final class FileTest extends BaseTestCase
public function testSave(): void
{
$filename = __DIR__ . '/file_' . strval(hrtime(true)) . '.test';
$file = new File('foo');
$file->save($filename);
$this->assertTrue(file_exists($filename));
unlink($filename);
$filenames = [
__DIR__ . '/01_file_' . strval(hrtime(true)) . '.test',
__DIR__ . '/02_file_' . strval(hrtime(true)) . '.test',
];
foreach ($filenames as $name) {
$file->save($name);
}
foreach ($filenames as $name) {
$this->assertFileExists($name);
$this->assertEquals('foo', file_get_contents($name));
unlink($name);
}
}
public function testToString(): void