JamesHeinrich / phpThumb

phpThumb() - The PHP thumbnail generator
Other
318 stars 98 forks source link

Suggestion: Surface chroma-subsampling options #121

Open badbadmonkey opened 6 years ago

badbadmonkey commented 6 years ago

Just a suggestion - at present phpThumb provides no control over chroma subsampling of JPEGs produced, relying on the defaults for sampling-factor with ImageMagick. I wonder if such control might be surfaced along with defaults.

For example I currently hack phpThumb.class.php as follows just above the final lines generating $commandline ~

if ($this->thumbnailFormat == 'jpg' || $this->thumbnailFormat == 'jpeg') {
    if ($this->thumbnailQuality <= 65) $commandline .= ' -sampling-factor 2x2';     // 4:2:0 commensurate with lower q
    elseif ($this->thumbnailQuality <= 70) $commandline .= ' -sampling-factor 2x1'; // 4:2:2 for medium q
    else $commandline .= ' -sampling-factor 1x1';       // 4:4:4 for higher q
}

Which hard-codes my own preferences but obviously options would be better. I think it's particularly useful nowadays with us having to deal with both low and high dpi client screens.

The other logic that would be useful to build in would be graceful degradation of the setting so that a higher subsampling wasn't used than the source JPG justified. This is where phpThumb has a role to play beyond the defaults from ImageMagick.

For example if a 1000px 4:2:0 JPG is resized to 500px, the thumbnail could be subsampled at 4:4:4 if the user settings desired it, but not if it's resized to say 800px. On the other hand a 4:4:4 source would justify the same at any downsize resolution.

JamesHeinrich commented 6 years ago

Since you're more familiar with the issue than I, could you apply whatever modifications you think appropriate and submit a pull request.

badbadmonkey commented 6 years ago

My hack above just implements what I think appropriate for my particular site. I wouldn't advocate it for all. I'm just throwing the idea here to try to put it on your radar as it's become more of an optimization issue lately.

What I would suggest is firstly adding core support with sensible defaults, but also surfacing control in the config or defaults, and adding an URL parameter at least just accessing -sampling-factor, to allow smart usage when it comes to using srcset and for varying screen densities. I lack the in-depth familiarity with phpThumb to do that in a satisfactory way sorry!

JamesHeinrich commented 6 years ago

If you would like to have it in phpThumb, I leave it to you (or someone) to suggest and submit an implementation. I'm essentially unfamiliar with the issue.