adachsoft / image-edit-tool
AI tool for editing and creating sandboxed images with model-consumable previews.
Requires
- php: ^8.3
- adachsoft/ai-tool-call: ^2.2
- adachsoft/image-editor-contracts: ^0.1.0
- adachsoft/image-editor-lib: ^0.1.0
- adachsoft/image-reader-contracts: ^0.1.0
- adachsoft/image-reader-lib: ^0.1.0
- adachsoft/sandbox-contracts: ^0.1
Requires (Dev)
- adachsoft/php-code-style: ^0.7.4
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpstan/phpstan-phpunit: ^2.0
- phpunit/phpunit: ^12.5
- rector/rector: ^2.9
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
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) orcanvas(object): provide exactly one.sourceis a project-relative image path.canvasrequires positivewidthandheightand accepts an optionalbackgroundcolor, defaulting to#FFFFFFFF.operations(optional array): ordered image operations; omitted means no operations. Supported types arecrop,resize,rotate,flip,grayscale,negate,brightness,contrast,colorize,replace_color,blur,text,rectangle, andarrow. Colors use#RRGGBBor#RRGGBBAA.output_path(required string): project-relative output path. Its extension selectspng,jpg/jpeg,webp, orgifoutput.overwrite(optional boolean, defaultfalse): allow atomic replacement of an existing regular output file.quality(optional integer, 1–100): output quality for lossy formats.include_preview(optional boolean, defaulttrue): 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,520max_input_pixels: 50,000,000max_output_pixels: 25,000,000max_operations: 20max_text_length: 500max_output_bytes: 20,971,520preview_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.