diff --git src/wp-admin/includes/file.php src/wp-admin/includes/file.php
index 12206b78b5..8cd45c4c7a 100644
--- src/wp-admin/includes/file.php
+++ src/wp-admin/includes/file.php
@@ -2553,3 +2553,41 @@ function wp_opcache_invalidate( $filepath, $force = false ) {
 
 	return false;
 }
+
+/**
+ * Wrapper for PHP filesize with filters and casting the result as an integer.
+ *
+ * @since 5.7.0
+ *
+ * @link https://www.php.net/manual/en/function.filesize.php
+ *
+ * @param string $path Path to the file.
+ * @return int The size of the file in bytes, or 0 in the event of an error.
+ */
+function wp_filesize( $path ) {
+	/**
+	 * Filters the result of wp_filesize before the PHP function is run.
+	 *
+	 * @since 5.7.0
+	 *
+	 * @param null|int $size The unfiltered value. Returning an int from the callback bypasses the filesize call.
+	 * @param string   $path Path to the file.
+	 */
+	$size = apply_filters( 'pre_wp_filesize', null, $path );
+
+	if ( is_int( $size ) ) {
+		return $size;
+	}
+
+	$size = (int) @filesize( $path );
+
+	/**
+	 * Filters the size of the file.
+	 *
+	 * @since 5.7.0
+	 *
+	 * @param int    $size The result of PHP filesize on the file.
+	 * @param string $path Path to the file.
+	 */
+	return apply_filters( 'wp_filesize', $size, $path );
+}
diff --git src/wp-admin/includes/image.php src/wp-admin/includes/image.php
index 4d6e99f749..5a6d380323 100644
--- src/wp-admin/includes/image.php
+++ src/wp-admin/includes/image.php
@@ -210,6 +210,9 @@ function _wp_image_meta_replace_original( $saved_data, $original_file, $image_me
 	// Store the original image file name in image_meta.
 	$image_meta['original_image'] = wp_basename( $original_file );
 
+	// Add image file size.
+	$image_meta['filesize'] = wp_filesize( $new_file );
+
 	return $image_meta;
 }
 
@@ -235,10 +238,11 @@ function wp_create_image_subsizes( $file, $attachment_id ) {
 
 	// Default image meta.
 	$image_meta = array(
-		'width'  => $imagesize[0],
-		'height' => $imagesize[1],
-		'file'   => _wp_relative_upload_path( $file ),
-		'sizes'  => array(),
+		'width'    => $imagesize[0],
+		'height'   => $imagesize[1],
+		'file'     => _wp_relative_upload_path( $file ),
+		'filesize' => wp_filesize( $file ),
+		'sizes'    => array(),
 	);
 
 	// Fetch additional metadata from EXIF/IPTC.
@@ -629,6 +633,11 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) {
 	// Remove the blob of binary data from the array.
 	unset( $metadata['image']['data'] );
 
+	// Capture file size for cases where it has not been captured yet, such as PDFs.
+	if ( ! isset( $metadata['filesize'] ) && file_exists( $file ) ) {
+		$metadata['filesize'] = wp_filesize( $file );
+	}
+
 	/**
 	 * Filters the generated attachment meta data.
 	 *
diff --git src/wp-admin/includes/media.php src/wp-admin/includes/media.php
index d9dcbd5d7d..89382a4f0f 100644
--- src/wp-admin/includes/media.php
+++ src/wp-admin/includes/media.php
@@ -3378,7 +3378,11 @@ function attachment_submitbox_metadata() {
 	if ( isset( $meta['filesize'] ) ) {
 		$file_size = $meta['filesize'];
 	} elseif ( file_exists( $file ) ) {
-		$file_size = filesize( $file );
+		$meta = wp_generate_attachment_metadata( $attachment_id, $file );
+
+		if ( is_array( $meta ) && isset( $meta['filesize'] ) ) {
+			$file_size = $meta['filesize'];
+		}
 	}
 
 	if ( ! empty( $file_size ) ) {
diff --git src/wp-includes/class-wp-image-editor-gd.php src/wp-includes/class-wp-image-editor-gd.php
index f175b90d20..c933368441 100644
--- src/wp-includes/class-wp-image-editor-gd.php
+++ src/wp-includes/class-wp-image-editor-gd.php
@@ -497,6 +497,7 @@ class WP_Image_Editor_GD extends WP_Image_Editor {
 			'width'     => $this->size['width'],
 			'height'    => $this->size['height'],
 			'mime-type' => $mime_type,
+			'filesize'  => wp_filesize( $filename ),
 		);
 	}
 
diff --git src/wp-includes/class-wp-image-editor-imagick.php src/wp-includes/class-wp-image-editor-imagick.php
index b7336fe5ae..0ffff73900 100644
--- src/wp-includes/class-wp-image-editor-imagick.php
+++ src/wp-includes/class-wp-image-editor-imagick.php
@@ -729,6 +729,7 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor {
 			'width'     => $this->size['width'],
 			'height'    => $this->size['height'],
 			'mime-type' => $mime_type,
+			'filesize'  => wp_filesize( $filename ),
 		);
 	}
 
diff --git src/wp-includes/media.php src/wp-includes/media.php
index c7ef8bab5d..8cda9a06fc 100644
--- src/wp-includes/media.php
+++ src/wp-includes/media.php
@@ -4046,7 +4046,11 @@ function wp_prepare_attachment_for_js( $attachment ) {
 	if ( isset( $meta['filesize'] ) ) {
 		$bytes = $meta['filesize'];
 	} elseif ( file_exists( $attached_file ) ) {
-		$bytes = filesize( $attached_file );
+		$meta = wp_generate_attachment_metadata( $attachment->ID, $attached_file );
+
+		if ( is_array( $meta ) && isset( $meta['filesize'] ) ) {
+			$bytes = $meta['filesize'];
+		}
 	} else {
 		$bytes = '';
 	}
diff --git tests/phpunit/tests/file.php tests/phpunit/tests/file.php
index 311ba30339..11dd63d156 100644
--- tests/phpunit/tests/file.php
+++ tests/phpunit/tests/file.php
@@ -262,4 +262,40 @@ class Tests_File extends WP_UnitTestCase {
 
 		return $keys;
 	}
+
+	/**
+	 * @ticket 49412
+	 */
+	function test_wp_filesize_with_nonexistent_file() {
+		$file = 'nonexistent/file.jpg';
+		$this->assertEquals( 0, wp_filesize( $file ) );
+	}
+
+	/**
+	 * @ticket 49412
+	 */
+	function test_wp_filesize() {
+		$file = DIR_TESTDATA . '/images/test-image-upside-down.jpg';
+
+		$this->assertEquals( filesize( $file ), wp_filesize( $file ) );
+
+		$filter = function() {
+			return 999;
+		};
+
+		add_filter( 'wp_filesize', $filter );
+
+		$this->assertEquals( 999, wp_filesize( $file ) );
+
+		$pre_filter = function() {
+			return 111;
+		};
+
+		add_filter( 'pre_wp_filesize', $pre_filter );
+
+		$this->assertEquals( 111, wp_filesize( $file ) );
+
+		remove_filter( 'wp_filesize', $filter );
+		remove_filter( 'pre_wp-filesize', $pre_filter );
+	}
 }
diff --git tests/phpunit/tests/image/editorGd.php tests/phpunit/tests/image/editorGd.php
index 781ff96cb4..5d967bdcdb 100644
--- tests/phpunit/tests/image/editorGd.php
+++ tests/phpunit/tests/image/editorGd.php
@@ -100,6 +100,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
 				'width'     => 50,
 				'height'    => 33,
 				'mime-type' => 'image/jpeg',
+				'filesize'  => wp_filesize( dirname( $file ) . '/waffles-50x33.jpg' ),
 			),
 		);
 
@@ -300,6 +301,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
 				'width'     => 10,
 				'height'    => 7,
 				'mime-type' => 'image/jpeg',
+				'filesize'  => wp_filesize( dirname( $file ) . '/waffles-10x7.jpg' ),
 			),
 
 			// #1
@@ -308,6 +310,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
 				'width'     => 75,
 				'height'    => 50,
 				'mime-type' => 'image/jpeg',
+				'filesize'  => wp_filesize( dirname( $file ) . '/waffles-75x50.jpg' ),
 			),
 
 			// #2
@@ -316,6 +319,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
 				'width'     => 30,
 				'height'    => 20,
 				'mime-type' => 'image/jpeg',
+				'filesize'  => wp_filesize( dirname( $file ) . '/waffles-30x20.jpg' ),
 			),
 
 			// #3
@@ -324,6 +328,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
 				'width'     => 45,
 				'height'    => 400,
 				'mime-type' => 'image/jpeg',
+				'filesize'  => wp_filesize( dirname( $file ) . '/waffles-45x400.jpg' ),
 			),
 
 			// #4
@@ -332,6 +337,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
 				'width'     => 50,
 				'height'    => 33,
 				'mime-type' => 'image/jpeg',
+				'filesize'  => wp_filesize( dirname( $file ) . '/waffles-50x33.jpg' ),
 			),
 
 			// #5
@@ -340,6 +346,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
 				'width'     => 55,
 				'height'    => 37,
 				'mime-type' => 'image/jpeg',
+				'filesize'  => wp_filesize( dirname( $file ) . '/waffles-55x37.jpg' ),
 			),
 
 			// #6
@@ -348,6 +355,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
 				'width'     => 83,
 				'height'    => 55,
 				'mime-type' => 'image/jpeg',
+				'filesize'  => wp_filesize( dirname( $file ) . '/waffles-83x55.jpg' ),
 			),
 
 			// #7
@@ -356,6 +364,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
 				'width'     => 90,
 				'height'    => 60,
 				'mime-type' => 'image/jpeg',
+				'filesize'  => wp_filesize( dirname( $file ) . '/waffles-90x60.jpg' ),
 			),
 
 			// #8
@@ -364,6 +373,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
 				'width'     => 105,
 				'height'    => 70,
 				'mime-type' => 'image/jpeg',
+				'filesize'  => wp_filesize( dirname( $file ) . '/waffles-105x70.jpg' ),
 			),
 
 			// #9
@@ -372,6 +382,7 @@ class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {
 				'width'     => 200,
 				'height'    => 133,
 				'mime-type' => 'image/jpeg',
+				'filesize'  => wp_filesize( dirname( $file ) . '/waffles-200x133.jpg' ),
 			),
 		);
 
diff --git tests/phpunit/tests/image/editorImagick.php tests/phpunit/tests/image/editorImagick.php
index e82486d828..02e0590a2e 100644
--- tests/phpunit/tests/image/editorImagick.php
+++ tests/phpunit/tests/image/editorImagick.php
@@ -90,6 +90,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
 				'width'     => 50,
 				'height'    => 33,
 				'mime-type' => 'image/jpeg',
+				'filesize'  => wp_filesize( dirname( $file ) . '/waffles-50x33.jpg' ),
 			),
 		);
 
@@ -289,6 +290,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
 				'width'     => 10,
 				'height'    => 7,
 				'mime-type' => 'image/jpeg',
+				'filesize'  => wp_filesize( dirname( $file ) . '/waffles-10x7.jpg' ),
 			),
 
 			// #1
@@ -297,6 +299,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
 				'width'     => 75,
 				'height'    => 50,
 				'mime-type' => 'image/jpeg',
+				'filesize'  => wp_filesize( dirname( $file ) . '/waffles-75x50.jpg' ),
 			),
 
 			// #2
@@ -305,6 +308,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
 				'width'     => 30,
 				'height'    => 20,
 				'mime-type' => 'image/jpeg',
+				'filesize'  => wp_filesize( dirname( $file ) . '/waffles-30x20.jpg' ),
 			),
 
 			// #3
@@ -313,6 +317,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
 				'width'     => 45,
 				'height'    => 400,
 				'mime-type' => 'image/jpeg',
+				'filesize'  => wp_filesize( dirname( $file ) . '/waffles-45x400.jpg' ),
 			),
 
 			// #4
@@ -321,6 +326,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
 				'width'     => 50,
 				'height'    => 33,
 				'mime-type' => 'image/jpeg',
+				'filesize'  => wp_filesize( dirname( $file ) . '/waffles-50x33.jpg' ),
 			),
 
 			// #5
@@ -329,6 +335,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
 				'width'     => 55,
 				'height'    => 37,
 				'mime-type' => 'image/jpeg',
+				'filesize'  => wp_filesize( dirname( $file ) . '/waffles-55x37.jpg' ),
 			),
 
 			// #6
@@ -337,6 +344,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
 				'width'     => 83,
 				'height'    => 55,
 				'mime-type' => 'image/jpeg',
+				'filesize'  => wp_filesize( dirname( $file ) . '/waffles-83x55.jpg' ),
 			),
 
 			// #7
@@ -345,6 +353,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
 				'width'     => 90,
 				'height'    => 60,
 				'mime-type' => 'image/jpeg',
+				'filesize'  => wp_filesize( dirname( $file ) . '/waffles-90x60.jpg' ),
 			),
 
 			// #8
@@ -353,6 +362,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
 				'width'     => 105,
 				'height'    => 70,
 				'mime-type' => 'image/jpeg',
+				'filesize'  => wp_filesize( dirname( $file ) . '/waffles-105x70.jpg' ),
 			),
 
 			// #9
@@ -361,6 +371,7 @@ class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {
 				'width'     => 200,
 				'height'    => 133,
 				'mime-type' => 'image/jpeg',
+				'filesize'  => wp_filesize( dirname( $file ) . '/waffles-200x133.jpg' ),
 			),
 		);
 
diff --git tests/phpunit/tests/image/functions.php tests/phpunit/tests/image/functions.php
index 8cd42a13b5..594e52e919 100644
--- tests/phpunit/tests/image/functions.php
+++ tests/phpunit/tests/image/functions.php
@@ -174,6 +174,17 @@ class Tests_Image_Functions extends WP_UnitTestCase {
 	public function test_wp_save_image_file() {
 		$classes = $this->get_image_editor_engine_classes();
 
+		foreach ( $classes as $key => $class ) {
+			if ( ! call_user_func( array( $class, 'test' ) ) ) {
+				// If the image editor isn't available, skip it.
+				unset( $classes[ $key ] );
+			}
+		}
+
+		if ( ! $classes ) {
+			$this->markTestSkipped( sprintf( 'The image editor engine %s is not supported on this system.', 'WP_Image_Editor_GD' ) );
+		}
+
 		require_once ABSPATH . 'wp-admin/includes/image-edit.php';
 
 		// Mime types.
@@ -224,6 +235,17 @@ class Tests_Image_Functions extends WP_UnitTestCase {
 	public function test_mime_overrides_filename() {
 		$classes = $this->get_image_editor_engine_classes();
 
+		foreach ( $classes as $key => $class ) {
+			if ( ! call_user_func( array( $class, 'test' ) ) ) {
+				// If the image editor isn't available, skip it.
+				unset( $classes[ $key ] );
+			}
+		}
+
+		if ( ! $classes ) {
+			$this->markTestSkipped( sprintf( 'The image editor engine %s is not supported on this system.', 'WP_Image_Editor_GD' ) );
+		}
+
 		// Test each image editor engine.
 		foreach ( $classes as $class ) {
 			$img    = new $class( DIR_TESTDATA . '/images/canola.jpg' );
@@ -255,6 +277,17 @@ class Tests_Image_Functions extends WP_UnitTestCase {
 	public function test_inferred_mime_types() {
 		$classes = $this->get_image_editor_engine_classes();
 
+		foreach ( $classes as $key => $class ) {
+			if ( ! call_user_func( array( $class, 'test' ) ) ) {
+				// If the image editor isn't available, skip it.
+				unset( $classes[ $key ] );
+			}
+		}
+
+		if ( ! $classes ) {
+			$this->markTestSkipped( sprintf( 'The image editor engine %s is not supported on this system.', 'WP_Image_Editor_GD' ) );
+		}
+
 		// Mime types.
 		$mime_types = array(
 			'jpg'  => 'image/jpeg',
@@ -340,6 +373,11 @@ class Tests_Image_Functions extends WP_UnitTestCase {
 			$this->markTestSkipped( 'Image editor engines WP_Image_Editor_GD and WP_Image_Editor_Imagick are not supported on this system.' );
 		}
 
+		// Then, test with editors.
+		foreach ( $classes as $class ) {
+			$editor = new $class( DIR_TESTDATA );
+			$loaded = $editor->load();
+
 		return $classes;
 	}
 
@@ -479,40 +517,49 @@ class Tests_Image_Functions extends WP_UnitTestCase {
 
 		$this->assertNotEmpty( $attachment_id );
 
+		$metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
+
+		$temp_dir = get_temp_dir();
+
 		$expected = array(
-			'sizes' => array(
+			'sizes'    => array(
 				'full'      => array(
 					'file'      => 'wordpress-gsoc-flyer-pdf.jpg',
 					'width'     => 1088,
 					'height'    => 1408,
 					'mime-type' => 'image/jpeg',
+					'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf.jpg' ),
 				),
 				'medium'    => array(
 					'file'      => 'wordpress-gsoc-flyer-pdf-232x300.jpg',
 					'width'     => 232,
 					'height'    => 300,
 					'mime-type' => 'image/jpeg',
+					'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-232x300.jpg' ),
 				),
 				'large'     => array(
 					'file'      => 'wordpress-gsoc-flyer-pdf-791x1024.jpg',
 					'width'     => 791,
 					'height'    => 1024,
 					'mime-type' => 'image/jpeg',
+					'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-791x1024.jpg' ),
 				),
 				'thumbnail' => array(
 					'file'      => 'wordpress-gsoc-flyer-pdf-116x150.jpg',
 					'width'     => 116,
 					'height'    => 150,
 					'mime-type' => 'image/jpeg',
+					'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-116x150.jpg' ),
 				),
 			),
+			'filesize' => wp_filesize( $test_file ),
 		);
 
 		$metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
+
 		$this->assertSame( $expected, $metadata );
 
 		unlink( $test_file );
-		$temp_dir = get_temp_dir();
 		foreach ( $metadata['sizes'] as $size ) {
 			unlink( $temp_dir . $size['file'] );
 		}
@@ -549,41 +596,57 @@ class Tests_Image_Functions extends WP_UnitTestCase {
 
 		$this->assertNotEmpty( $attachment_id );
 
+		$metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
+
+		$temp_dir = get_temp_dir();
+
 		$expected = array(
-			'sizes' => array(
+			'sizes'    => array(
 				'full'      => array(
 					'file'      => 'wordpress-gsoc-flyer-pdf.jpg',
 					'width'     => 1088,
 					'height'    => 1408,
 					'mime-type' => 'image/jpeg',
+					'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf.jpg' ),
 				),
 				'medium'    => array(
 					'file'      => 'wordpress-gsoc-flyer-pdf-300x300.jpg',
 					'width'     => 300,
 					'height'    => 300,
 					'mime-type' => 'image/jpeg',
+					'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-300x300.jpg' ),
 				),
 				'large'     => array(
 					'file'      => 'wordpress-gsoc-flyer-pdf-791x1024.jpg',
 					'width'     => 791,
 					'height'    => 1024,
 					'mime-type' => 'image/jpeg',
+					'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-791x1024.jpg' ),
 				),
 				'thumbnail' => array(
 					'file'      => 'wordpress-gsoc-flyer-pdf-116x150.jpg',
 					'width'     => 116,
 					'height'    => 150,
 					'mime-type' => 'image/jpeg',
+					'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-116x150.jpg' ),
 				),
 			),
+			'filesize' => wp_filesize( $test_file ),
 		);
 
 		$metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
+
+		// Different environments produce slightly different filesize results.
+		foreach ( $metadata['sizes'] as &$item ) {
+			$item['filesize'] = round_to_nearest_thousand( $item['filesize'] );
+		}
+		$metadata['filesize'] = round_to_nearest_thousand( $metadata['filesize'] );
+
 		$this->assertSame( $expected, $metadata );
 
 		unlink( $test_file );
 		foreach ( $metadata['sizes'] as $size ) {
-			unlink( get_temp_dir() . $size['file'] );
+			unlink( $temp_dir . $size['file'] );
 		}
 	}
 
@@ -617,14 +680,24 @@ class Tests_Image_Functions extends WP_UnitTestCase {
 		add_image_size( 'test-size', 100, 100 );
 		add_filter( 'fallback_intermediate_image_sizes', array( $this, 'filter_fallback_intermediate_image_sizes' ), 10, 2 );
 
+		$metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
+
+		$temp_dir = get_temp_dir();
+
 		$expected = array(
 			'file'      => 'wordpress-gsoc-flyer-pdf-77x100.jpg',
 			'width'     => 77,
 			'height'    => 100,
 			'mime-type' => 'image/jpeg',
+			'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-77x100.jpg' ),
 		);
 
 		$metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
+
+		// Different environments produce slightly different filesize results.
+		$metadata['sizes']['test-size']['filesize'] = round_to_nearest_thousand( $metadata['sizes']['test-size']['filesize'] );
+		$this->assertSame( $metadata['sizes']['test-size'], $expected );
+
 		$this->assertArrayHasKey( 'test-size', $metadata['sizes'], 'The `test-size` was not added to the metadata.' );
 		$this->assertSame( $expected, $metadata['sizes']['test-size'] );
 
@@ -632,7 +705,6 @@ class Tests_Image_Functions extends WP_UnitTestCase {
 		remove_filter( 'fallback_intermediate_image_sizes', array( $this, 'filter_fallback_intermediate_image_sizes' ), 10 );
 
 		unlink( $test_file );
-		$temp_dir = get_temp_dir();
 		foreach ( $metadata['sizes'] as $size ) {
 			unlink( $temp_dir . $size['file'] );
 		}
diff --git tests/phpunit/tests/media/filesize.php tests/phpunit/tests/media/filesize.php
new file mode 100644
index 0000000000..57a0ec6592
--- /dev/null
+++ tests/phpunit/tests/media/filesize.php
@@ -0,0 +1,93 @@
+<?php
+
+/**
+ * @group media
+ * @group media_filesize
+ */
+class Tests_Image_Filesize extends WP_UnitTestCase {
+	function tearDown() {
+		$this->remove_added_uploads();
+
+		parent::tearDown();
+	}
+
+	/**
+	 * Check that filesize meta is generated for jpegs.
+	 *
+	 * @ticket 49412
+	 */
+	function test_filesize_in_jpg_meta() {
+		$attachment = $this->factory->attachment->create_upload_object( DIR_TESTDATA . '/images/33772.jpg' );
+
+		$metadata = wp_get_attachment_metadata( $attachment );
+
+		$this->assertEquals( wp_filesize( get_attached_file( $attachment ) ), $metadata['filesize'] );
+
+		foreach ( $metadata['sizes'] as $intermediate_size ) {
+			$this->assertTrue( ! empty( $intermediate_size['filesize'] ) && is_numeric( $intermediate_size['filesize'] ) );
+		}
+	}
+
+	/**
+	 * Check that filesize meta is generated for pngs.
+	 *
+	 * @ticket 49412
+	 */
+	function test_filesize_in_png_meta() {
+		$attachment = $this->factory->attachment->create_upload_object( DIR_TESTDATA . '/images/test-image.png' );
+
+		$metadata = wp_get_attachment_metadata( $attachment );
+
+		$this->assertEquals( wp_filesize( get_attached_file( $attachment ) ), $metadata['filesize'] );
+
+		foreach ( $metadata['sizes'] as $intermediate_size ) {
+			$this->assertTrue( ! empty( $intermediate_size['filesize'] ) && is_numeric( $intermediate_size['filesize'] ) );
+		}
+	}
+
+	/**
+	 * Check that filesize meta is generated for pdfs.
+	 *
+	 * @ticket 49412
+	 */
+	function test_filesize_in_pdf_meta() {
+		$attachment = $this->factory->attachment->create_upload_object( DIR_TESTDATA . '/images/wordpress-gsoc-flyer.pdf' );
+
+		$metadata = wp_get_attachment_metadata( $attachment );
+
+		$this->assertEquals( wp_filesize( get_attached_file( $attachment ) ), $metadata['filesize'] );
+	}
+
+	/**
+	 * Adds psd to allowed mime types for multisite testing.
+	 *
+	 * @param array $mimes Unfiltered mime types.
+	 * @return array Filtered mime types.
+	 */
+	public function allow_psd_mime_type( $mimes ) {
+		$mimes['psd'] = 'application/octet-stream';
+		return $mimes;
+	}
+
+	/**
+	 * Check that filesize meta is generated for psds.
+	 *
+	 * @ticket 49412
+	 */
+	function test_filesize_in_psd_meta() {
+		// PSD mime type is not allowed by default on multisite.
+		if ( is_multisite() ) {
+			add_filter( 'upload_mimes', array( $this, 'allow_psd_mime_type' ), 10, 2 );
+		}
+
+		$attachment = $this->factory->attachment->create_upload_object( DIR_TESTDATA . '/images/test-image.psd' );
+
+		$metadata = wp_get_attachment_metadata( $attachment );
+
+		$this->assertEquals( wp_filesize( get_attached_file( $attachment ) ), $metadata['filesize'] );
+
+		if ( is_multisite() ) {
+			remove_filter( 'upload_mimes', array( $this, 'allow_psd_mime_type' ) );
+		}
+	}
+}
