Optimize images
optimize_imagesOptimizes images and writes the results in place or to an output folder, replacing files only when quality checks pass and the new file is smaller.
Instructions
Optimises images and writes the results. If the user only wants to know how much could be saved, or has not agreed to changes yet, use analyze_images instead: it takes the same arguments and writes nothing. write: "in-place" overwrites each file and keeps its format and name, so references in code stay valid (atomic rename). write: "out-dir" writes to outDir, mirroring the folder structure, and may change formats; extensions change accordingly, so update references. A file is only replaced when the result passes the quality check and is smaller. Files Optiq already compressed lossily are skipped (unless reoptimize is true), so calling this twice is safe. Choosing parameters: mode visually-lossless for hero images, artwork and product photos, lossless for logos and UI assets that must stay exact, balanced otherwise; stripMetadata before publishing personal photos (GPS); formats without avif if old browsers matter. Takes the same time as analyze_images (typically 0.3-3 s per image).
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| mode | No | How strict the quality check is. lossless: decoded pixels verified identical (JPEG inputs only get metadata changes). visually-lossless: SSIM >= 0.99. balanced (default): SSIM >= 0.975. fast: same threshold as balanced, no AVIF, at most 5 encodes, about 3x faster. Use visually-lossless for hero images, artwork and product photos. | balanced |
| paths | Yes | Image files and/or folders. Folders are searched recursively for jpg, jpeg, png, webp, avif and gif; dot-folders are skipped. Use absolute paths; relative ones resolve against the server's working directory. | |
| write | Yes | "in-place" overwrites inputs; "out-dir" writes to outDir. | |
| outDir | No | Required when write is "out-dir". | |
| formats | No | Formats Optiq may choose when the format may change. Default: all four (fast mode: no avif). Drop avif if the site must support browsers older than Safari 16.4. JPEG is never chosen for images with transparency. | |
| maxSize | No | Downscale so the longest side is at most this many pixels, e.g. 2560 for camera photos on a website. Smaller images are not enlarged. Allowed in lossless mode too (the resize itself changes pixels). | |
| minSaving | No | Keep a file unchanged unless it gets at least this share smaller (0.05 = 5%). Default 0.02. Raise it to avoid churning files for tiny gains. | |
| keepFormat | No | Never change the file format (a .png stays PNG). Default: true for in-place writes and for analysis, false when writing to outDir. Set false in analysis to see what converting to WebP/AVIF would save. | |
| minQuality | No | Custom SSIM threshold for perceptual modes instead of the mode's default, e.g. 0.98. Higher keeps more detail and saves less. Ignored in lossless mode. | |
| reoptimize | No | Re-encode files Optiq already compressed lossily. Each lossy pass loses a little more quality; leave false unless the user asks. | |
| stripMetadata | No | Remove EXIF (including GPS location, camera serial) and XMP. Recommended before publishing personal photos. Colour profiles that matter (non-sRGB ICC) and image orientation are preserved either way. |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| files | Yes | ||
| dryRun | Yes | ||
| totals | Yes | ||
| omittedFiles | Yes | Files processed but not listed (the list is capped); totals include them. |