Compress Image to Target File Size

Compress an image to fit within a specific file size in bytes using quality-first compression.

Who this is for

Marketing teams and content platforms use this recipe to compress images for email attachments, CMS upload limits, or mobile app asset budgets. Specify a target file size in bytes and the API handles the quality-dimension tradeoff automatically.

Request
curl -X POST \
  https://api.iterationlayer.com/image-transformation/v1/transform \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "file": {
      "type": "url",
      "name": "photo.jpg",
      "url": "https://cdn.example.com/uploads/photo.jpg"
    },
    "operations": [
      {
        "type": "compress_to_size",
        "max_file_size_in_bytes": 500000
      }
    ]
  }'
Response
{
  "success": true,
  "data": {
    "buffer": "/9j/4AAQSkZJRgABAQAAAQ...",
    "mime_type": "image/jpeg"
  }
}
Request
import { IterationLayer } from "iterationlayer";
const client = new IterationLayer({ apiKey: "YOUR_API_KEY" });

const result = await client.transform({
  file: {
    type: "url",
    name: "photo.jpg",
    url: "https://cdn.example.com/uploads/photo.jpg",
  },
  operations: [
    {
      type: "compress_to_size",
      max_file_size_in_bytes: 500_000,
    },
  ],
});
Response
{
  "success": true,
  "data": {
    "buffer": "/9j/4AAQSkZJRgABAQAAAQ...",
    "mime_type": "image/jpeg"
  }
}
Request
from iterationlayer import IterationLayer
client = IterationLayer(api_key="YOUR_API_KEY")

result = client.transform(
    file={
        "type": "url",
        "name": "photo.jpg",
        "url": "https://cdn.example.com/uploads/photo.jpg",
    },
    operations=[
        {
            "type": "compress_to_size",
            "max_file_size_in_bytes": 500_000,
        },
    ],
)
Response
{
  "success": true,
  "data": {
    "buffer": "/9j/4AAQSkZJRgABAQAAAQ...",
    "mime_type": "image/jpeg"
  }
}
Request
package main

import il "github.com/iterationlayer/sdk-go"

func main() {
    client := il.NewClient("YOUR_API_KEY")

    result, err := client.Transform(il.TransformRequest{
        File: il.NewFileFromURL(
            "photo.jpg",
            "https://cdn.example.com/uploads/photo.jpg",
        ),
        Operations: []il.TransformOperation{
            il.NewCompressToSizeOperation(500_000),
        },
    })
    if err != nil {
        panic(err)
    }
}
Response
{
  "success": true,
  "data": {
    "buffer": "/9j/4AAQSkZJRgABAQAAAQ...",
    "mime_type": "image/jpeg"
  }
}

Related Recipes

Start building in minutes

Free trial credits included. No credit card required.