Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
40 changes: 40 additions & 0 deletions tests/phpunit/tests/rest-api/rest-attachments-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Loading