From ba87a88987969005882af4a208400c18ae8c8f60 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Tue, 1 Dec 2026 19:44:51 +0000 Subject: [PATCH] REST API: Return the file size of the original image for a scaled attachment. For an image that was scaled down on upload, the attachments controller returned the original file name in `filename` but the stored size of the `-scaled` copy in `filesize`. The stored size is now used only when the original file is the attached file; otherwise the size of the original file is returned. Fixes #66892. --- .../class-wp-rest-attachments-controller.php | 23 +++++++---- .../rest-api/rest-attachments-controller.php | 40 +++++++++++++++++++ 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php index 2a14083968635..dcb1d5ee74db9 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php @@ -2531,17 +2531,24 @@ protected function get_attachment_filename( int $attachment_id ): ?string { * @phpstan-return non-negative-int|null */ protected function get_attachment_filesize( int $attachment_id ): ?int { - $meta = wp_get_attachment_metadata( $attachment_id ); + $original_path = wp_get_original_image_path( $attachment_id ); + $attached_file = get_attached_file( $attachment_id ); + $file_path = $original_path ? $original_path : $attached_file; - if ( isset( $meta['filesize'] ) && is_numeric( $meta['filesize'] ) && $meta['filesize'] > 0 ) { - return (int) $meta['filesize']; - } + /* + * The stored file size belongs to the attached file. For a scaled image, + * that is the scaled copy, not the original file the `filename` field names. + */ + if ( $file_path === $attached_file ) { + $meta = wp_get_attachment_metadata( $attachment_id ); - $original_path = wp_get_original_image_path( $attachment_id ); - $attached_file = $original_path ? $original_path : get_attached_file( $attachment_id ); + if ( isset( $meta['filesize'] ) && is_numeric( $meta['filesize'] ) && $meta['filesize'] > 0 ) { + return (int) $meta['filesize']; + } + } - if ( is_string( $attached_file ) && is_readable( $attached_file ) ) { - return wp_filesize( $attached_file ); + if ( is_string( $file_path ) && is_readable( $file_path ) ) { + return wp_filesize( $file_path ); } return null; diff --git a/tests/phpunit/tests/rest-api/rest-attachments-controller.php b/tests/phpunit/tests/rest-api/rest-attachments-controller.php index 56a62748a1c80..db34fef4ccaa0 100644 --- a/tests/phpunit/tests/rest-api/rest-attachments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-attachments-controller.php @@ -1148,6 +1148,46 @@ public static function data_invalid_filesize_meta(): array { ); } + /** + * Ensures `filesize` describes the same file as `filename` for a scaled image. + * + * The stored `filesize` belongs to the `-scaled` copy, while `filename` + * names the original file. + * + * @ticket 66892 + * + * @requires function imagejpeg + */ + public function test_get_item_returns_original_filesize_for_scaled_image() { + add_filter( + 'big_image_size_threshold', + static function () { + return 1000; + } + ); + + // test-image-large.jpg is 3000x2250, so it exceeds the threshold above. + $attachment_id = self::factory()->attachment->create_upload_object( DIR_TESTDATA . '/images/test-image-large.jpg' ); + $this->assertIsInt( $attachment_id, 'Uploading the image should succeed.' ); + + $original_file = wp_get_original_image_path( $attachment_id ); + $attached_file = get_attached_file( $attachment_id ); + $this->assertIsString( $original_file, 'The original image path should be known.' ); + $this->assertStringContainsString( '-scaled', wp_basename( $attached_file ), 'The attached file should be a scaled copy.' ); + + $meta = wp_get_attachment_metadata( $attachment_id ); + $this->assertIsArray( $meta, 'The attachment metadata should be stored.' ); + $this->assertNotSame( wp_filesize( $original_file ), $meta['filesize'], 'The stored file size should belong to the scaled copy.' ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/media/' . $attachment_id ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + + $this->assertSame( 200, $response->get_status(), 'Getting the attachment should succeed.' ); + $this->assertSame( wp_basename( $original_file ), $data['filename'], 'The file name should be the original file name.' ); + $this->assertSame( wp_filesize( $original_file ), $data['filesize'], 'The file size should be the original file size.' ); + } + /** * @requires function imagejpeg */