Skip to content

Better image optimization #755

@aciccarello

Description

@aciccarello

Is your feature request related to a problem?

When testing out the media endpoint added in #700, I noticed that images I'd already processed through https://squoosh.app/ (e.g. this photo) were getting saved with a larger file size than the original. It's probably due to some extra optimization that aren't on by default in Sharp, but it seems like an opportunity for improvement.

Describe the solution you’d like

  1. A file size check before accepting a processed image over the original
  2. Additional options for JPEG encoding (or a more complex configuration option like Media endpoint: Support modifying images prior to uploading #394)

The downside of the file size check is that it could potentially lead to EXIF data unexpectedly not being removed and exposing location data. The Sharp JPEG encoding options could be exposed, though I'm not sure if always using that option would cause Sharp to always output jpeg format (even for PNGs).

Describe alternatives you’ve considered

I'm using a fork of the media endpoint code with some modifications to fix the issues I noticed. I have some logic about jpeg quality that probably is unique to my site, but otherwise the code could be a reference point for implementation.

https://github.com/aciccarello/ciccarello-indiekit/blob/3381efe087a99b4ceaed44af2bf8f80ca79e52a8/lib/endpoint-media/lib/media-transform.js#L14-L31

Additional context

On my site, images usually are shown with a max with of 1024, so I will often upload a 2048px wide image with lower jpeg quality for high DPI screens.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions