Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Estimate the thumbnail image sizes #3881

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
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
26 changes: 22 additions & 4 deletions files/class-image.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,31 @@ public function get_mime_type() {
/**
* Returns the images filesize (in bytes).
*
* Since we don't actually create new files for different attachment sizes,
* the filesize on each entry in the attachment sizes array will just be that of the original.
*
* @return int
*/
public function get_filesize() {
return (int) $this->filesize;
$filesize = (int) $this->filesize;

// We don't actually create new files for different attachment sizes,
// so the filesize on each entry is instead an estimate based on pixel count difference.
if ( $this->is_resized ) {
$original_pixels = $this->original_height * $this->original_width;
$resized_pixels = $this->height * $this->width;

// Unsure if this is possible, but just in case :)
if ( $original_pixels <= 0 ) {
return 0;
}

// Example: Original 100mb image w/ 1000 pixels, cropped to 250 pixels in the thumbnail.
// The pixel diff is 25%. So estimated size is 100mb * .25 = 25mb.
$pixel_diff = $resized_pixels / $original_pixels;
$estimated_size = $filesize * $pixel_diff;

return (int) round( $estimated_size );
}

return $filesize;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion search/search-dev-tools/build/bundle.js

Large diffs are not rendered by default.

33 changes: 13 additions & 20 deletions tests/files/test-image-sizes.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ class A8C_Files_ImageSizes_Test extends WP_UnitTestCase {
*/
public $test_image = VIP_GO_MUPLUGINS_TESTS__DIR__ . '/fixtures/image.jpg'; //@todo: consider using `DIR_TESTDATA . '/images/canola.jpg';`

/**
* The test image's filesize in bytes.
*
* @var int
*/
public $test_image_filesize = 6941712;

/**
* The test PDF file.
*
Expand Down Expand Up @@ -227,37 +220,37 @@ public function get_data_for_resize() {
'width' => '150',
'height' => '150',
'crop' => '1',
'filesize' => $this->test_image_filesize,
'filesize' => '9267',
],
'medium' => [
'width' => '300',
'height' => '300',
'crop' => false,
'filesize' => $this->test_image_filesize,
'filesize' => '20882',
],
'medium_large' => [
'width' => '768',
'height' => '0',
'crop' => false,
'filesize' => $this->test_image_filesize,
'filesize' => '136652',
],
'large' => [
'width' => '1024',
'height' => '1024',
'crop' => false,
'filesize' => $this->test_image_filesize,
'filesize' => '242936',
],
'1536x1536' => [
'width' => '1536',
'height' => '1536',
'crop' => false,
'filesize' => $this->test_image_filesize,
'filesize' => '547239',
],
'2048x2048' => [
'width' => '2048',
'height' => '2048',
'crop' => false,
'filesize' => $this->test_image_filesize,
'filesize' => '972588',
],
],
];
Expand Down Expand Up @@ -291,7 +284,7 @@ public function test__resize( $data ) {
'width' => intval( $data['width'] ),
'height' => intval( $data['height'] ),
'mime-type' => 'image/jpeg',
'filesize' => $this->test_image_filesize,
'filesize' => intval( $data['filesize'] ),
];
$this->assertEquals( $expected_resize, $generate_sizes->invokeArgs( $image_sizes, [ $data ] ) );
}
Expand All @@ -310,42 +303,42 @@ public function get_expected_sizes_meta() {
'width' => 150,
'height' => 150,
'mime-type' => 'image/jpeg',
'filesize' => $this->test_image_filesize,
'filesize' => '9267',
],
'medium' => [
'file' => 'image.jpg?resize=300,169',
'width' => 300,
'height' => 169,
'mime-type' => 'image/jpeg',
'filesize' => $this->test_image_filesize,
'filesize' => '20882',
],
'medium_large' => [
'file' => 'image.jpg?resize=768,432',
'width' => 768,
'height' => 432,
'mime-type' => 'image/jpeg',
'filesize' => $this->test_image_filesize,
'filesize' => '136652',
],
'large' => [
'file' => 'image.jpg?resize=1024,576',
'width' => 1024,
'height' => 576,
'mime-type' => 'image/jpeg',
'filesize' => $this->test_image_filesize,
'filesize' => '242936',
],
'1536x1536' => [
'file' => 'image.jpg?resize=1536,865',
'width' => 1536,
'height' => 865,
'mime-type' => 'image/jpeg',
'filesize' => $this->test_image_filesize,
'filesize' => '547239',
],
'2048x2048' => [
'file' => 'image.jpg?resize=2048,1153',
'width' => 2048,
'height' => 1153,
'mime-type' => 'image/jpeg',
'filesize' => $this->test_image_filesize,
'filesize' => '972588',
],
],
],
Expand Down
39 changes: 19 additions & 20 deletions tests/files/test-image.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ class A8C_Files_Image_Test extends WP_UnitTestCase {
*/
public $test_image = VIP_GO_MUPLUGINS_TESTS__DIR__ . '/fixtures/image.jpg'; //@todo: consider using `DIR_TESTDATA . '/images/canola.jpg';`

/**
* The test image's filesize in bytes.
*
* @var int
*/
public $test_image_filesize = 6941712;

/**
* @var Automattic\VIP\Files\VIP_Filesystem
*/
Expand Down Expand Up @@ -159,9 +152,10 @@ public function get_data_for_generate_sizes() {
'crop' => '1',
],
[
'width' => 150,
'height' => 150,
'params' => [
'width' => 150,
'height' => 150,
'filesize' => 9267,
'params' => [
'resize' => '150,150',
],
],
Expand All @@ -173,9 +167,10 @@ public function get_data_for_generate_sizes() {
'crop' => false,
],
[
'width' => 300,
'height' => 169,
'params' => [
'width' => 300,
'height' => 169,
'filesize' => 20882,
'params' => [
'resize' => '300,169',
],
],
Expand All @@ -187,9 +182,10 @@ public function get_data_for_generate_sizes() {
'crop' => false,
],
[
'width' => 768,
'height' => 432,
'params' => [
'width' => 768,
'height' => 432,
'filesize' => 136652,
'params' => [
'resize' => '768,432',
],
],
Expand All @@ -201,9 +197,10 @@ public function get_data_for_generate_sizes() {
'crop' => false,
],
[
'width' => 1024,
'height' => 576,
'params' => [
'width' => 1024,
'height' => 576,
'filesize' => 242936,
'params' => [
'resize' => '1024,576',
],
],
Expand Down Expand Up @@ -234,6 +231,7 @@ public function test__image_resize( $size, $expected_resize ) {
$this->assertTrue( $image->is_resized(), 'Resized image is not marked as resized.' );
$this->assertEquals( $expected_resize['width'], $image->get_width(), 'Resized image does not have expected width.' );
$this->assertEquals( $expected_resize['height'], $image->get_height(), 'Resized image does not have expected height.' );
$this->assertEquals( $expected_resize['filesize'], $image->get_filesize(), 'Resized image does not have expected estimated filesize.' );
$this->assertEquals( 'image/jpeg', $image->get_mime_type(), 'Resized image does not have appropriate mime type.' );
$this->assertEquals( add_query_arg( $expected_resize['params'], 'image.jpg' ), $image->get_filename(), 'Resized image does not point to appropriate file.' );
}
Expand Down Expand Up @@ -263,7 +261,7 @@ public function test__get_size( $size, $expected_resize ) {
'width' => $expected_resize['width'],
'height' => $expected_resize['height'],
'mime-type' => 'image/jpeg',
'filesize' => $this->test_image_filesize,
'filesize' => $expected_resize['filesize'],
];
$this->assertEquals( $expected_size_array, $new_size_array, 'The size array does not match the expected one.' );
}
Expand Down Expand Up @@ -292,6 +290,7 @@ public function test__reset_to_original( $size ) {
$this->assertFalse( $image->is_resized(), 'Image is not marked as NOT resized.' );
$this->assertEquals( $postmeta['width'], $image->get_width(), 'Width has not been properly reset.' );
$this->assertEquals( $postmeta['height'], $image->get_height(), 'Height has not been properly reset.' );
$this->assertEquals( $postmeta['filesize'], $image->get_filesize(), 'Filesize has not been properly reset.' );
$this->assertEquals( 'image/jpeg', $image->get_mime_type(), 'Mime-type has not been properly reset' );
$this->assertEquals( 'image.jpg', $image->get_filename(), 'Image after reset does not point to appropriate file.' );
}
Expand Down