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

Conversation

WPprodigy
Copy link
Contributor

@WPprodigy WPprodigy commented Nov 11, 2022

Description

In WP 6.0, the filesize for attachment thumbnails was added to the attachment metadata. We added support for this here: #2991

Since we don't actually generate attachments, the filesize was just set to that of the original image. This has some unfortunate implications wherein a sized-down image will be seen as too large for certain tasks (like social media usage, seen here in yoast seo).

As a workaround to this, it was proposed that we can instead just estimate what the resized image will be. So this is what I attempted in this PR.

Problems

It's not very consistent >.<.

JPG Image

// Original size: 2192x2921
curl -sIXGET https://{redacted}/wp-content/uploads/2022/11/sample.jpg | grep -w content-length
content-length: 1033414

// Resized to 225x300
curl -sIXGET https://{redacted}/wp-content/uploads/2022/11/sample.jpg\?resize\=225,300 | grep -w content-length
content-length: 14703

// Resized to 768x1024
curl -sIXGET https://{redacted}/wp-content/uploads/2022/11/sample.jpg\?resize\=768,1024 | grep -w content-length
content-length: 114954

The estimates by the current logic in this PR (pretty close):

wp_get_attachment_metadata(8061);

'file' => string(23) "sample.jpg?resize=225,300"
'filesize' => int(10894)

'file' => string(23) "sample.jpg?resize=768,1024"
'filesize' => int(126930)

PNG Image

// Original size: 792x725
curl -sIXGET https://{redacted}/wp-content/uploads/2022/11/test.png | grep -w content-length
content-length: 71061

// Resized to 300x275
curl -sIXGET https://{redacted}/wp-content/uploads/2022/11/test.png\?resize\=300,275 | grep -w content-length
content-length: 57071

// Resized to 768x703
curl -sIXGET https://{redacted}/wp-content/uploads/2022/11/test.png\?resize\=768,703 | grep -w content-length
content-length: 388501

The estimates by the current logic in this PR (off by half):

wp_get_attachment_metadata(8060);

'file' => string(23) "test.png?resize=300,275"
'filesize' => int(102099)

'file' => string(23) "test.png?resize=768,703"
'filesize' => int(668167)

So as you can see, we could divide our current estimate by 2 and be pretty close for the PNG. But JPG would then be further off. This doesn't really account for webp conversions either (which I think is fine since those change based on browser support, though maybe we need to account for it if a webp image is uploaded as the original).

So what should we do? Alter the formula based on image type? Am I missing something?

@sonarcloud
Copy link

sonarcloud bot commented Nov 11, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@GaryJones
Copy link
Contributor

Presuming it's a one-off task on upload, could you actually do the resize, capture the bytesize, and then dump the thumbnail image?

@WPprodigy
Copy link
Contributor Author

WPprodigy commented Nov 14, 2022

Presuming it's a one-off task on upload, could you actually do the resize, capture the bytesize, and then dump the thumbnail image?

We generate attachment metadata "on the fly". One big benefit to this is that thumbnails don't need to be "regenerated" when new sizes are added by plugins/themes.

Actually resizing and capturing the bitesize for every attachment thumbnail size on every image could take a very large amount of time on many of our sites 🙃

@sjinks sjinks force-pushed the develop branch 2 times, most recently from deded3c to b235946 Compare November 14, 2022 20:51
@github-actions
Copy link
Contributor

This pull request has been marked stale because it has been open for 60 days with no activity. If there is no activity within 7 days, it will be closed.

This is an automation to keep pull requests manageable and actionable and is not a comment on the quality of this pull request nor on the work done so far. Closed PRs are still valuable to the project and their branches are preserved.

@sonarcloud
Copy link

sonarcloud bot commented Feb 24, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@codecov
Copy link

codecov bot commented Feb 24, 2023

Codecov Report

Merging #3881 (e584477) into develop (0b1a7dd) will increase coverage by 0.02%.
The diff coverage is 80.00%.

@@              Coverage Diff              @@
##             develop    #3881      +/-   ##
=============================================
+ Coverage      32.29%   32.31%   +0.02%     
- Complexity      3794     3796       +2     
=============================================
  Files            231      231              
  Lines          16987    16996       +9     
=============================================
+ Hits            5486     5493       +7     
- Misses         11501    11503       +2     
Impacted Files Coverage Δ
files/class-image.php 92.68% <80.00%> (-1.84%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants