Skip to content
Hokusai2.2.2
ivantokar.com

Orientation and resize

A phone photo stores its pixels sideways and records the way to turn them in an EXIF tag. resize(width: 800) measures the stored width unless autoOrient() has already applied the tag.

let phone = try Hokusai(url: URL(fileURLWithPath: "phone.jpg")) // stored 1000 × 1600, orientation 6
let orientFirst = try phone.autoOrient().resize(width: 800)
let resizeFirst = try phone.resize(width: 800).autoOrient()
for (name, pipeline) in [("orient-first", orientFirst), ("resize-first", resizeFirst)] {
let output = try await pipeline.jpeg().data()
print(name, "→", output.info.width, "×", output.info.height)
try output.data.write(to: URL(fileURLWithPath: "\(name).jpg"))
}
  • The scene upright and 800 by 500 pixels.
    autoOrient → resizeJPEG · 800 × 500 · 27 KB
  • The scene upright but 1280 by 800 pixels: it was resized to 800 pixels wide before being turned.
    resize → autoOrientJPEG · 1280 × 800 · 51 KB
Order Size
autoOrient() then resize(width: 800) 800 × 500: the long side is 800 as intended
resize(width: 800) then autoOrient() 1280 × 800: the stored width (the short side) was set to 800, then the image was turned

Both are upright; only the first is the size you asked for. The same applies to extract, smartCrop, resize(fit: .cover) with both dimensions, and anything else that reads width or height. Put autoOrient() first (and before resize in particular, so JPEG shrink-on-load still applies when the pipeline starts with autoOrient).