Skip to content

Improve image filters API #8439

@bep

Description

@bep

Given:

{{ $img := $original.Resize "400x300 q55" }}
{{ $img = $img | images.Filter (images.GaussianBlur 6) (images.Pixelate 8) }}

There are currently some problems with the above:

  1. The image gets encoded twice
  2. There is no way to control the encoding (e.g. quality) for the second operation.
  3. If you want the end result encode to different formats, you need to repeat the image processing for each target format (e.g. JPEG and WEBP)

I suggest that we

  • add a target options argument to images.Filter which can be a string (e.g. webp) or a map (which also allows setting quality settings)
  • Export the resize functions (Resize, Fill, Fit) as filters.

This way you can do:

{{ $img = $img | images.Filter (images.Resize 400 300)  (images.GaussianBlur 6) (images.Pixelate 8) (images.EncodeTo "webp")  }}

I'm tempted to also use the same images.Filter to return a slice if multiple images.EncodeTo arguments is passed, e.g.:

{{ $imgages := $img | images.Filter (images.Resize 400 300)  (images.GaussianBlur 6) (images.Pixelate 8) (images.EncodeTo "webp")  (images.EncodeTo "jpeg")  }}

But it's probably best to just add a new function, e.g.:

{{ $imgages := $img | images.FilterAll (images.Resize 400 300)  (images.GaussianBlur 6) (images.Pixelate 8) (images.EncodeTo "webp")  (images.EncodeTo "jpeg")  }}

Feedback welcome, @jmooring @moorereason @onedrawingperday and gang.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions