Skip to content

Cookbook

Dus Mamud edited this page Nov 9, 2026 · 1 revision

Cookbook

Twelve recipes for the jobs people actually do. Every pattern here runs in the playground or the consumer test — the docs site cookbook carries the extended versions.

1. Hit a byte budget

import { processImage } from '@dusrel/image';

const result = await processImage(file, {
  format: 'image/webp',
  maxSizeBytes: 200_000,
});
console.log(result.targetSizeMet);  // honest answer, either way

2. Convert to AVIF

import { processImage } from '@dusrel/image';
import { encode } from '@dusrel/image-avif';

await processImage(file, {
  format: 'image/avif',
  codecs: [{ mimeType: 'image/avif', encode: (d) => encode(d, { quality: 50, speed: 9 }) }],
});

3. Turn an iPhone HEIC into a JPEG

import { decodeHeic } from '@dusrel/image-heic';
import { encode } from '@dusrel/image-jpeg';

const pixels = await decodeHeic(heicBytes);       // native on Safari 17+, WASM elsewhere
const jpegBlob = await encode(pixels, { quality: 82 });

4. Strip metadata before upload (privacy)

import { stripMetadata } from '@dusrel/image-metadata';

const noGps = await stripMetadata(bytes, { mode: 'gps-only' });
const clean = await stripMetadata(bytes, { mode: 'all' });

5. Read EXIF and GPS

import { readMetadata } from '@dusrel/image-metadata';

const info = await readMetadata(bytes);
console.log(info.format, info.exif?.make, info.exif?.gps);  // { latitude, longitude }

6. Add a text watermark

import { pipeline, imageDataPlugin } from '@dusrel/image';
import { drawTextWatermark } from '@dusrel/image-watermark';

await pipeline(file)
  .use(imageDataPlugin({
    name: 'watermark',
    transform: (d) => drawTextWatermark(d, '© Dusrel', { position: 'bottom-right' }),
  }))
  .run();

7. Generate placeholders (ThumbHash / BlurHash)

import { blobToImageData } from '@dusrel/image';
import { encodeThumbHash, thumbHashToBase64, encodeBlurHash } from '@dusrel/image-placeholder';

const data = await blobToImageData(file);
const thumb = thumbHashToBase64(encodeThumbHash(data));
const blur = encodeBlurHash(data);

8. Encode an animated GIF

import { encode } from '@dusrel/image-gif';

const gif = await encode([{ data: frame1 }, { data: frame2, delay: 120 }], { repeat: 0 });

9. High-quality WASM resize

import { blobToImageData } from '@dusrel/image';
import { resize } from '@dusrel/image-resize';

const data = await blobToImageData(file);
const small = resize(data, { width: 800, height: 600, filter: 'lanczos3' });

10. Optimise a PNG losslessly

import { optimize } from '@dusrel/image-oxipng';

const smaller = await optimize(pngBytes, { level: 3 });  // → ArrayBuffer

11. Process a batch

import { processBatch } from '@dusrel/image';

const results = await processBatch(files, { format: 'image/webp', quality: 0.8 });

For worker pools, retries and memory caps, see @dusrel/image-batch (runBatch, createWorkerPool) in the Package Guide.

12. Optimise an SVG

import { optimizeSvg } from '@dusrel/image-svg';

const optimized = await optimizeSvg(svgText);  // SVGO, multipass by default

Next: Package Guide · Codec Guide

Clone this wiki locally