Search by

adachsoft / image-edit-tool

Arkadiusz Adach

AI tool for editing and creating sandboxed images with model-consumable previews.

Package info

gitlab.com/a.adach/image-edit-tool

Issues

pkg:composer/adachsoft/image-edit-tool

Statistics

Installs: 17

Dependents: 0

Suggesters: 0

Stars: 0

v0.1.0 2026-11-28 03:38 UTC

This package is auto-updated.

Last update: 2026-11-28 02:39:42 UTC


README

image_edit edits a project image or creates an image on a blank canvas, applies operations in order, saves the result inside the project, and optionally returns a bounded preview as model-consumable media. The tool delegates image processing to ImageEditorInterface; edited image bytes are never included in the structured result.

Requirements

  • PHP 8.3 or later.
  • The GD extension when using the default image editor and image reader.
  • A project sandbox directory with pre-existing output directories for files to be written.

Usage

Use the image_edit tool to edit an existing project image or create an image on a blank canvas. Supply exactly one of source or canvas, then provide an output_path inside the configured project sandbox. The tool can apply ordered transformations, validate configured resource limits, save output atomically, and optionally provide a bounded preview as media.

Parameters

  • source (string) or canvas (object): provide exactly one. source is a project-relative image path. canvas requires positive width and height and accepts an optional background color, defaulting to #FFFFFFFF.
  • operations (optional array): ordered image operations; omitted means no operations. Supported types are crop, resize, rotate, flip, grayscale, negate, brightness, contrast, colorize, replace_color, blur, text, rectangle, and arrow. Colors use #RRGGBB or #RRGGBBAA.
  • output_path (required string): project-relative output path. Its extension selects png, jpg/jpeg, webp, or gif output.
  • overwrite (optional boolean, default false): allow atomic replacement of an existing regular output file.
  • quality (optional integer, 1–100): output quality for lossy formats.
  • include_preview (optional boolean, default true): return one resized preview media item when enabled.

Example JSON call:

{
  "source": "assets/source.png",
  "operations": [
    {"type": "resize", "width": 800},
    {"type": "text", "text": "Preview", "x": 20, "y": 40, "size": 24, "color": "#FFFFFFFF"}
  ],
  "output_path": "artifacts/edited.png",
  "overwrite": false,
  "include_preview": true
}

To create a new image, replace source with a canvas object, for example {"width": 800, "height": 600, "background": "#FFFFFFFF"}.

Configuration and limits

The base ToolConfig is config/tool_configs/base/image_edit.json. It requires the project-provided base_path and supplies these defaults:

  • max_input_bytes: 20,971,520
  • max_input_pixels: 50,000,000
  • max_output_pixels: 25,000,000
  • max_operations: 20
  • max_text_length: 500
  • max_output_bytes: 20,971,520
  • preview_max_dimension: 1,024

Configured limits must be positive integers; preview_max_dimension must be between 64 and 8,192. Preview preparation enforces the configured pixel and byte limits.

Sandbox and output handling

All input and output paths are resolved relative to the configured project sandbox. Source files must be readable regular files whose canonical paths remain inside the project. The source size is checked before reading. Output parent directories must already exist and their canonical paths must remain inside the project; output symlinks and symlinked directories that escape the project are rejected.

Output is written to a temporary file in the validated output directory and atomically renamed into place. Existing files are rejected unless overwrite is true. The structured result contains only a relative path, format and MIME type, dimensions, byte count, applied-operation count, overwrite status, and preview status. Preview bytes are returned only through the media collection, never in the KeyValueMap.

Installation

Install the package with Composer:

composer require adachsoft/image-edit-tool

The default image editor and image reader use GD. Provide a GD-enabled PHP runtime when using the default implementations.

Integration

Create a tool for the project sandbox and register the returned ToolInterface instance with your AI tool-call host. Pass the project root as base_path; the tool resolves image paths relative to that directory.

use AdachSoft\AiToolCall\SPI\Collection\ConfigMap;
use AdachSoft\ImageEditTool\ImageEditToolFactory;

$factory = new ImageEditToolFactory();
$tool = $factory->create(new ConfigMap([
    'base_path' => $projectRoot,
]));

// Register $tool with the host's tool-call integration.

The factory accepts optional ImageEditorInterface and ImageReaderInterface implementations when constructing it. Each create() call binds a tool instance to the supplied project's sandbox and image limits.