Skip to content
Hokusai2.2.2
ivantokar.com

Getting Started

Hokusai turns a source image into an immutable processing pipeline. Configure the recipe synchronously, then use an asynchronous terminal to evaluate it.

Add Hokusai to your Package.swift:

dependencies: [
.package(url: "https://github.com/ivantokar/hokusai.git", from: "2.0.0")
]

Add Hokusai to the target that processes images. For complete platform setup, see Installation & Requirements.

import Hokusai
let output = try await Hokusai(url: URL(fileURLWithPath: "photo.jpg"))
.autoOrient()
.resize(width: 1200, height: 630, fit: .cover, position: .attention)
.webp(quality: 82)
.write(to: URL(fileURLWithPath: "social-card.webp"))
print("Wrote \(output.width) × \(output.height)")

resize, webp, and every other transform returns a new pipeline. Native work runs only when write(to:) or data() is called.

Use a Data input when an image already arrived in memory:

let image = try Hokusai(data: uploadData)
let jpeg = try await image
.resize(width: 800, height: 600, fit: .cover)
.jpeg(quality: 85)
.data()

The output contains the encoded bytes and image information. For HTTP request helpers and responses, continue to the Vapor integration.