> ## Documentation Index
> Fetch the complete documentation index at: https://hackmamba-3f164318.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Batch Resize and Optimize for Web

> Resize a folder of images for web delivery with format conversion, quality control, and file size constraints.

This guide covers batch-resizing images to a maximum width, converting to WebP or JPEG, and handling common edge cases like mixed orientations and already-small files.

Unlike the [step-by-step pipeline tutorial](/tutorials/build-image-processing-pipeline), this guide focuses on **production batch concerns**: preserving **subdirectory layout**, enforcing **JPEG file-size ceilings**, and **resize modes** (width-only, height-only, exact crop, letterbox padding) beyond a single “fit in a box” recipe.

## Resize and convert a single image

Constrain an image to a maximum width of 1200 pixels and output as WebP:

```bash theme={null}
magick input.jpg -resize '1200x>' -quality 85 output.webp
```

The **`1200x>`** geometry means “width at most 1200, never upscale”; see [Geometry syntax reference](/reference/geometry) for the full modifier table. The **`-quality`** number is documented under [-quality](/reference/common-options#quality); examples in this guide use **85** as a shared web default so commands stay consistent with the rest of the site.

## Batch a flat folder with a loop

An explicit **`for`** loop runs one `magick` command per file. You get readable logs, per-file error handling, and room to add guards or logging, which is why many teams prefer loops over `mogrify` in production scripts.

Resize every JPEG in `photos/` and write matching WebP files into `web/`:

```bash theme={null}
mkdir -p web
for f in photos/*.jpg; do
  [[ -e "$f" ]] || continue
  base=$(basename "$f" .jpg)
  magick "$f" -resize '1200x>' -quality 85 "web/${base}.webp"
done
```

Confirm the outputs look right:

```bash theme={null}
magick identify web/*.webp
```

You should see one line per file, each with **`WEBP`** in the format column and dimensions at or below your resize rule.

Same loop on **Windows PowerShell**:

```powershell theme={null}
New-Item -ItemType Directory -Force -Path web | Out-Null
Get-ChildItem photos\*.jpg | ForEach-Object {
  $out = "web\$($_.BaseName).webp"
  magick $_.FullName -resize "1200x>" -quality 85 $out
}
Get-ChildItem web\*.webp | ForEach-Object { magick identify $_.FullName }
```

## Batch a flat folder with mogrify

You can express the same flat-folder job as a single `mogrify` invocation when you already trust every file matched by the glob and you have set `-path` so originals are not overwritten.

```bash theme={null}
mkdir -p web
magick mogrify -resize '1200x>' -quality 85 -path web -format webp photos/*.jpg
```

<Info>
  After `mogrify`, run **`magick identify web/*.webp`** to confirm each output exists and shows the **`WEBP`** format and expected geometry.
</Info>

## Batch-convert with subdirectories

`mogrify` does not recurse into subdirectories or preserve folder structure. Use `find` with a loop:

<Tabs>
  <Tab title="Linux / macOS">
    ```bash theme={null}
    find photos -name '*.jpg' | while read file; do
      relpath="${file#photos/}"
      outdir="web/$(dirname "$relpath")"
      mkdir -p "$outdir"
      magick "$file" -resize '1200x>' -quality 85 \
        "$outdir/$(basename "$relpath" .jpg).webp"
    done
    ```
  </Tab>

  <Tab title="Windows (PowerShell)">
    ```powershell theme={null}
    Get-ChildItem -Recurse photos\*.jpg | ForEach-Object {
      $rel = $_.FullName.Substring((Resolve-Path photos).Path.Length + 1)
      $outDir = "web\" + (Split-Path $rel)
      New-Item -ItemType Directory -Force -Path $outDir | Out-Null
      $outFile = $outDir + "\" + $_.BaseName + ".webp"
      magick $_.FullName -resize "1200x>" -quality 85 $outFile
    }
    ```
  </Tab>
</Tabs>

The loop mirrors the source tree under `web/`: for example, `photos/shoes/boot1.jpg` becomes `web/shoes/boot1.webp`, preserving the `shoes/` segment between `photos/` and the filename.

## When one file fails mid-batch

`magick mogrify` applies the same operation to every path in the glob. If **one** input is corrupt or unreadable, **mogrify can exit and leave the rest untouched** (behavior depends on version and delegates; always read the terminal message).

When you need per-file control, a loop can log failures and continue with the remaining paths:

```bash theme={null}
mkdir -p web
for f in photos/*.jpg; do
  [[ -e "$f" ]] || continue
  base=$(basename "$f" .jpg)
  if magick "$f" -resize '1200x>' -quality 85 "web/${base}.webp"; then
    echo "ok  $f"
  else
    echo "ERR $f" >&2
  fi
done
```

If you intentionally want “best effort” without branching, append `|| true` after `magick` so a failed file does not abort the shell script. Use this only when silently skipping failures is acceptable:

```bash theme={null}
magick "$f" -resize '1200x>' -quality 85 "web/${base}.webp" || true
```

Inspect the exit status of the whole script with `$?` after the loop if you wrap this in automation.

## Fix rotated phone photos

Add **`-auto-orient`** (an **operator** that runs at its position in the command) before any resize operation to correct rotation flags from phone cameras. Pair it with **`-quality`** (a **setting** that applies to the next lossy write) as you already do elsewhere in this guide:

```bash theme={null}
magick input.jpg -auto-orient -resize '1200x>' -quality 85 output.webp
```

In a batch pipeline, place it immediately after the input:

```bash theme={null}
magick mogrify -auto-orient -resize '1200x>' -quality 85 -path web -format webp photos/*.jpg
```

## Enforce a file size ceiling (JPEG)

For JPEG output, `-define jpeg:extent` caps the file size. ImageMagick adjusts quality downward as needed:

```bash theme={null}
magick input.jpg -resize '1200x>' -define jpeg:extent=200KB output.jpg
```

On a typical large photo, `-define jpeg:extent` forces ImageMagick to lower effective quality until the file is at or under the limit (exact behavior depends on content and JPEG encoder settings). Confirm with `wc -c` or your file manager after running the command on your assets.

<Info>
  `jpeg:extent` is specific to JPEG output. WebP has no equivalent built-in ceiling. For WebP, control file size through `-quality`.
</Info>

## Resize to different constraints

### Constrain by width only

```bash theme={null}
magick input.jpg -resize '1200x>' output.jpg
```

The output width is at most 1200 pixels, and height scales to keep the aspect ratio (for example, a 2000×3500 portrait becomes 1200×2100).

### Constrain by height only

```bash theme={null}
magick input.jpg -resize 'x800>' output.jpg
```

The output height is at most 800 pixels, and width scales to keep the aspect ratio (for example, a 2000×3500 portrait becomes 457×800).

### Exact dimensions with center crop

Use this when the **destination must be an exact pixel size** (for example a **1200×800 hero slot**) and **cropping** edges is acceptable. Tall or wide sources are scaled until the box is filled, then overflow is cut off. That is ideal when the layout fixes width and height and you do not want letterboxing.

Fill the target dimensions and crop overflow from the center:

```bash theme={null}
magick input.jpg -resize '1200x800^' -gravity center -extent 1200x800 output.jpg
```

The output is exactly 1200x800, cropped from the center. See [Geometry syntax reference](/reference/geometry) for how `^` differs from other resize modifiers.

### Fit within a box with padding

Use this when the platform needs a **square (or fixed) canvas** but **must not crop** the subject (for example marketplace tiles that show the full product with **letterboxing** instead of cutting off limbs or labels). The image is scaled to fit inside the box, then the canvas is padded to the exact size with a background color.

Resize to fit inside 800x800, then pad the remaining space with a background color:

```bash theme={null}
magick input.jpg -resize '800x800>' -gravity center -background white -extent 800x800 output.jpg
```

A 4000x3000 landscape becomes an 800x800 image with the photo centered and white bars on top and bottom.

## Next steps

* Walk through a full resize → strip → watermark → WebP flow in the [image processing pipeline tutorial](/tutorials/build-image-processing-pipeline).
* Look up modifiers such as `>`, `^`, and `!` in the [Geometry syntax reference](/reference/geometry).
* Read flag-level details in the [common options reference](/reference/common-options).
* If commands fail or output looks wrong, use [Troubleshooting](/troubleshooting).
