Skip to content
Hokusai2.2.2
ivantokar.com

Encoder position

jpeg(), png(), webp(), avif(), heif(), tiff(), gif(), pdf() and metadataPolicy(_:) are terminal configuration, not image operations. They do not change pixels, so their place in the chain does not matter, and the last one wins.

let image = try Hokusai(url: URL(fileURLWithPath: "scene.png"))
// The encoder is terminal configuration, not an image operation: where it appears does not matter,
// and the last one wins.
let early = try await image.jpeg(quality: 70).resize(width: 300).data()
let late = try await image.resize(width: 300).jpeg(quality: 70).data()
let replaced = try await image.png().webp(quality: 70).resize(width: 300).data()
print("encoder first == encoder last:", early.data == late.data)
print("png() then webp() →", replaced.info.format.rawValue)

The program printed:

  • encoder first == encoder last: true: jpeg(quality: 70).resize(width: 300) and resize(width: 300).jpeg(quality: 70) produced byte-identical output.
  • png() then webp() → webp: selecting a second encoder replaces the first.

By convention put the encoder last, where it reads as the end of the recipe. Put it early only when a helper returns a pipeline with its encoder chosen. A layer’s encoder and metadata settings are ignored when it is composed.