From 16336e02a3703a29afd6e8197389cbf73fab844e Mon Sep 17 00:00:00 2001 From: tijmen Date: Mon, 27 Jul 2026 09:00:09 +0200 Subject: [PATCH 1/8] fix(image-size): clear memoized fs values after compression --- src/class-tiny-image-size.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/class-tiny-image-size.php b/src/class-tiny-image-size.php index e93040d5..b01e1600 100644 --- a/src/class-tiny-image-size.php +++ b/src/class-tiny-image-size.php @@ -53,9 +53,23 @@ public function add_tiny_meta( $response ) { if ( isset( $this->meta['start'] ) ) { $this->meta = $response; $this->meta['end'] = time(); + $this->reset_memoized_filesystem_values(); } } + /** + * Clears the memoized exists/file_size/mime_type values. + * + * Must be called whenever the file on disk changed after those were + * memoized, e.g. after compression overwrites it, so the next read + * reflects the new file instead of the stale cached one. + */ + private function reset_memoized_filesystem_values() { + $this->exists = null; + $this->file_size = null; + $this->mime_type = null; + } + public function add_tiny_meta_error( $exception ) { if ( isset( $this->meta['start'] ) ) { $this->meta = array( From 123aec8a025a0b5c141c968cf4b4b66b5357a4a0 Mon Sep 17 00:00:00 2001 From: tijmen Date: Mon, 27 Jul 2026 09:00:58 +0200 Subject: [PATCH 2/8] docs(image-size): add @return void to reset_memoized_filesystem_values --- src/class-tiny-image-size.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/class-tiny-image-size.php b/src/class-tiny-image-size.php index b01e1600..bae2f8c1 100644 --- a/src/class-tiny-image-size.php +++ b/src/class-tiny-image-size.php @@ -63,6 +63,8 @@ public function add_tiny_meta( $response ) { * Must be called whenever the file on disk changed after those were * memoized, e.g. after compression overwrites it, so the next read * reflects the new file instead of the stale cached one. + * + * @return void */ private function reset_memoized_filesystem_values() { $this->exists = null; From 6f8277a472c1773bd7ce3faed11f66aec68ea690 Mon Sep 17 00:00:00 2001 From: tijmen Date: Mon, 27 Jul 2026 09:06:31 +0200 Subject: [PATCH 3/8] docs(image-size): add docblocks --- src/class-tiny-image-size.php | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/class-tiny-image-size.php b/src/class-tiny-image-size.php index bae2f8c1..7ffa41f9 100644 --- a/src/class-tiny-image-size.php +++ b/src/class-tiny-image-size.php @@ -22,9 +22,25 @@ class Tiny_Image_Size { public $filename; public $meta = array(); - /* Used more than once and not trivial, so we are memoizing these */ + /** + * Whether the file exists on disk. + * + * @var bool|null $exists + */ private $exists; + + /** + * File size in bytes. + * + * @var int|null $file_size + */ private $file_size; + + /** + * MIME type of the file. + * + * @var string|null $mime_type + */ private $mime_type; private $duplicate = false; private $duplicate_of_size = ''; @@ -53,7 +69,7 @@ public function add_tiny_meta( $response ) { if ( isset( $this->meta['start'] ) ) { $this->meta = $response; $this->meta['end'] = time(); - $this->reset_memoized_filesystem_values(); + $this->reset_memoized_filesystem(); } } @@ -66,7 +82,7 @@ public function add_tiny_meta( $response ) { * * @return void */ - private function reset_memoized_filesystem_values() { + private function reset_memoized_filesystem() { $this->exists = null; $this->file_size = null; $this->mime_type = null; From 0ca2cd68d95e8c028f8dae9b88a34df1f87a4552 Mon Sep 17 00:00:00 2001 From: tijmen Date: Mon, 27 Jul 2026 09:10:36 +0200 Subject: [PATCH 4/8] format --- src/class-tiny-image-size.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/class-tiny-image-size.php b/src/class-tiny-image-size.php index 7ffa41f9..5c55a853 100644 --- a/src/class-tiny-image-size.php +++ b/src/class-tiny-image-size.php @@ -79,7 +79,7 @@ public function add_tiny_meta( $response ) { * Must be called whenever the file on disk changed after those were * memoized, e.g. after compression overwrites it, so the next read * reflects the new file instead of the stale cached one. - * + * * @return void */ private function reset_memoized_filesystem() { From 0fad83408bf7fb177f9a7b1ba49d69917a4094a8 Mon Sep 17 00:00:00 2001 From: tijmen Date: Mon, 27 Jul 2026 09:12:06 +0200 Subject: [PATCH 5/8] format --- src/class-tiny-image-size.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/class-tiny-image-size.php b/src/class-tiny-image-size.php index 5c55a853..a34a887e 100644 --- a/src/class-tiny-image-size.php +++ b/src/class-tiny-image-size.php @@ -42,7 +42,9 @@ class Tiny_Image_Size { * @var string|null $mime_type */ private $mime_type; - private $duplicate = false; + + private $duplicate = false; + private $duplicate_of_size = ''; public function __construct( $filename = null ) { From f83f2d5c71dbdd918e7c15aa50849d149fdb8291 Mon Sep 17 00:00:00 2001 From: tijmen Date: Mon, 27 Jul 2026 09:29:38 +0200 Subject: [PATCH 6/8] fix(image-size): clear stat cache on reset to avoid stale filesize --- src/class-tiny-image-size.php | 1 + test/unit/TinyImageSizeTest.php | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/class-tiny-image-size.php b/src/class-tiny-image-size.php index a34a887e..33efcc54 100644 --- a/src/class-tiny-image-size.php +++ b/src/class-tiny-image-size.php @@ -85,6 +85,7 @@ public function add_tiny_meta( $response ) { * @return void */ private function reset_memoized_filesystem() { + clearstatcache( true, $this->filename ); $this->exists = null; $this->file_size = null; $this->mime_type = null; diff --git a/test/unit/TinyImageSizeTest.php b/test/unit/TinyImageSizeTest.php index c77213ad..04f2a448 100644 --- a/test/unit/TinyImageSizeTest.php +++ b/test/unit/TinyImageSizeTest.php @@ -58,6 +58,25 @@ public function test_add_tiny_meta_should_add_end_time() { $this->assertEqualWithinDelta( time(), $this->large->meta['end'], 2 ); } + /** + * After compression overwrites the file on disk, add_tiny_meta() must clear + * the memoized file_size so the next filesize() call reflects the new file + * rather than returning the stale pre-compression value. + */ + public function test_add_tiny_meta_resets_memoized_filesize_after_compression() { + $this->assertEquals( 137856, $this->original->filesize(), 'expected filesize to be original' ); + + file_put_contents( $this->original->filename, str_repeat( 'a', 90000 ) ); + + $this->original->add_tiny_meta_start(); + $this->original->add_tiny_meta( array( + 'input' => array( 'size' => 137856 ), + 'output' => array( 'size' => 90000 ), + ) ); + + $this->assertEquals( 90000, $this->original->filesize(), 'should return refreshed filesize' ); + } + public function test_add_response_should_response() { $this->large->add_tiny_meta_start(); $this->large->add_tiny_meta( array( From 299f4cf18500a393a5b811463cff33f697826b19 Mon Sep 17 00:00:00 2001 From: tijmen Date: Mon, 27 Jul 2026 10:37:03 +0200 Subject: [PATCH 7/8] reflect actual metadata update --- test/unit/TinyImageSizeTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/TinyImageSizeTest.php b/test/unit/TinyImageSizeTest.php index 04f2a448..6015c823 100644 --- a/test/unit/TinyImageSizeTest.php +++ b/test/unit/TinyImageSizeTest.php @@ -63,12 +63,12 @@ public function test_add_tiny_meta_should_add_end_time() { * the memoized file_size so the next filesize() call reflects the new file * rather than returning the stale pre-compression value. */ - public function test_add_tiny_meta_resets_memoized_filesize_after_compression() { + public function test_add_tiny_meta_clears_memoized_data_after_compression() { + $this->original->add_tiny_meta_start(); $this->assertEquals( 137856, $this->original->filesize(), 'expected filesize to be original' ); file_put_contents( $this->original->filename, str_repeat( 'a', 90000 ) ); - $this->original->add_tiny_meta_start(); $this->original->add_tiny_meta( array( 'input' => array( 'size' => 137856 ), 'output' => array( 'size' => 90000 ), From c8e3a64684ea9e1740d08453c47d1195e21fa354 Mon Sep 17 00:00:00 2001 From: tijmen Date: Mon, 27 Jul 2026 10:41:50 +0200 Subject: [PATCH 8/8] rename method --- src/class-tiny-image-size.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/class-tiny-image-size.php b/src/class-tiny-image-size.php index 33efcc54..d10af65c 100644 --- a/src/class-tiny-image-size.php +++ b/src/class-tiny-image-size.php @@ -71,7 +71,7 @@ public function add_tiny_meta( $response ) { if ( isset( $this->meta['start'] ) ) { $this->meta = $response; $this->meta['end'] = time(); - $this->reset_memoized_filesystem(); + $this->clear_memoized_filesystem(); } } @@ -84,7 +84,7 @@ public function add_tiny_meta( $response ) { * * @return void */ - private function reset_memoized_filesystem() { + private function clear_memoized_filesystem() { clearstatcache( true, $this->filename ); $this->exists = null; $this->file_size = null;