Skip to content

optiq-image / react

react ​

optiq-image/react — React hook around compressImage. Requires React 18+ (optional peer dependency).

Interfaces ​

UseOptiqOptions ​

Extends ​

Properties ​

PropertyTypeDescriptionInherited from
allowFormatConversion?booleanDefault true when targetFormat is auto. When false, the output keeps the input format.CompressCallOptions.allowFormatConversion
allowLossyResize?booleanExplicitly permit resizing in lossless mode (the result is then no longer pixel-identical to the input).CompressCallOptions.allowLossyResize
autoFormats?OutputFormat[]Formats considered when targetFormat is auto. Default: jpeg, png, webp, avif. Remove avif if your audience includes browsers without AVIF decoding or encode time matters most.CompressCallOptions.autoFormats
compressor?CompressorUse a specific compressor (worker pool) instead of the shared one.-
enabled?booleanSet to false to keep the hook idle. Default true.-
markOutput?booleanMark lossy JPEG/WebP outputs with a small comment/chunk (~20 bytes) so a later run recognises them and does not re-encode them again (each lossy pass is checked against its own input, so repeated passes compound the loss). Default false in the library; the CLI enables it. AVIF and lossless outputs are not marked.CompressCallOptions.markOutput
maxCandidates?numberMaximum number of encodes (candidates) per image. Defaults: visually-lossless 18, balanced 10, lossless 4.CompressCallOptions.maxCandidates
maxInputBytes?numberReject inputs larger than this many bytes before reading them. Default 100 MiB.CompressCallOptions.maxInputBytes
maxInputPixels?numberReject inputs whose header declares more pixels than this (decompression-bomb guard). Default 50 MP.CompressCallOptions.maxInputPixels
maxOutputBytes?numberUpper bound for output size. Not a promise: reported in unmetConstraints if impossible.CompressCallOptions.maxOutputBytes
maxWidthOrHeight?numberDownscale so that the longest side is at most this. Never applied in lossless mode unless allowLossyResize.CompressCallOptions.maxWidthOrHeight
minQuality?numberMinimum SSIM (0..1) for perceptual modes. Defaults: visually-lossless 0.99, balanced 0.975. Ignored in lossless mode.CompressCallOptions.minQuality
minSavingsRatio?numberReturn the original when the best candidate saves less than this fraction. Default 0.02.CompressCallOptions.minSavingsRatio
mode?CompressionModeDefault balanced.CompressCallOptions.mode
onUnmetConstraints?"best-effort" | "throw"What to do when no candidate meets all constraints. Default best-effort (result lists unmetConstraints).CompressCallOptions.onUnmetConstraints
preserveAnimation?booleanDefault true. When false, animated inputs may be flattened to their first frame (with a warning).CompressCallOptions.preserveAnimation
preserveMetadata?booleanKeep EXIF/XMP and non-sRGB/sRGB ICC profiles. Default true. Setting false strips them (privacy: removes GPS etc).CompressCallOptions.preserveMetadata
reoptimize?booleanRe-encode inputs that carry the Optiq marker anyway. Default false: in perceptual modes a marked input is only optimised at container level (metadata), unless it is resized or converted to an explicitly requested format.CompressCallOptions.reoptimize
searchStrategy?SearchStrategybinary (default) searches quality per codec; fixed encodes each codec once at a preset quality.CompressCallOptions.searchStrategy
targetFormat?TargetFormatDefault auto.CompressCallOptions.targetFormat

UseOptiqState ​

Properties ​

PropertyTypeDescription
cancel() => voidAbort the running compression (terminates its worker).
error?unknownThe thrown value; usually an OptiqError with a code. The original file is always still usable.
progress?ProgressEvent-
result?CompressionResult-
statusUseOptiqStatus-

Type Aliases ​

UseOptiqStatus ​

ts
type UseOptiqStatus = "idle" | "compressing" | "done" | "error" | "cancelled";

Functions ​

useOptiq() ​

ts
function useOptiq(file, options?): UseOptiqState;

Compress file whenever it (or the options) change. Cancels the previous run automatically, and on unmount.

tsx
const { status, result, progress, cancel } = useOptiq(file, { mode: "balanced" });

Parameters ​

ParameterType
fileBlob | null | undefined
options?UseOptiqOptions

Returns ​

UseOptiqState