adachsoft / image-read-tool
AI tool (adachsoft/ai-tool-call SPI) that reads an image from the sandbox and returns it to the model as media.
Requires
- php: ^8.3
- adachsoft/ai-tool-call: ^2.2
- 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_read is an SPI tool that reads a project-relative image and returns the prepared image to the model as media. Image bytes are never included in the structured KeyValueMap result.
The tool supports PNG, JPEG, GIF, and WebP images. Images larger than the requested dimension are resized before they are sent to the model.
Features
- Reads PNG, JPEG, GIF, and WebP images from the configured project directory only.
- Returns safe metadata (normalized relative path, MIME type, dimensions, byte counts, resize flag) in the structured result.
- Delivers the image to the model as a single media item instead of inlining base64 data into the result map.
- Resizes images that exceed the requested or configured maximum dimension.
- Enforces configurable limits for input bytes, prepared output bytes, decoded pixels, and maximum dimension.
- Rejects absolute paths, sandbox escapes, symlinks pointing outside the project, non-image files, and unreadable files.
- Implements the
ToolInterfaceSPI contract and ships aToolFactoryInterfaceimplementation for per-project configuration.
Parameters
path(required, string): image path relative to the configured project directory.max_dimension(optional, integer): maximum image width or height in pixels. It must be at least 64 and no greater than the configuredmax_dimension; when omitted, the configured maximum is used.include_image(optional, boolean, defaulttrue): whenfalse, the result contains metadata but no media item. The structured result always includes theimage_includedboolean.
Configuration
The base ToolConfig is config/tool_configs/base/image_read.json:
base_pathis required and is resolved from the project context; the tool never falls back to the process working directory.max_dimensiondefaults to 2048 and must be between 64 and 8192.max_input_bytesdefaults to 20,971,520 and limits the encoded source file size.max_output_bytesdefaults to 4,194,304 and limits the prepared image size.max_input_pixelsdefaults to 50,000,000 and limits decoded pixel count before GD decoding.
All size and pixel limits must be positive integers.
Sandbox and media handling
Every requested path is resolved through the configured sandbox. The tool also checks canonical paths to reject symlinks that point outside the project directory, and verifies that the target is a readable file. It checks the file size before loading image bytes.
The structured result contains only the normalized project-relative path, MIME type, prepared and original dimensions, byte counts, resize status, and image_included status. When include_image is true, the image is returned as a single media item with its MIME type, base64-encoded payload, and filename so the model can view it. When it is false, the media collection is empty.
Result metadata
The KeyValueMap result contains the following keys:
path- normalized project-relative image path.mime_type- MIME type of the prepared image.width,height- dimensions of the prepared image.original_width,original_height- dimensions of the source image.original_bytes- size of the encoded source file.bytes- size of the prepared image.resized- whether the prepared image differs in size from the source image.image_included- whether a media item was attached to the result.
Requirements
- PHP 8.3 or newer.
- The GD extension, because the default image reader is GD based. Inject a custom
ImageReaderInterfaceimplementation when another backend is needed. adachsoft/ai-tool-call(SPI),adachsoft/sandbox-contracts,adachsoft/image-reader-contracts, andadachsoft/image-reader-lib, installed automatically by Composer.
Installation
composer require adachsoft/image-read-tool
Usage
Create the tool through the factory and call it with a project-relative image path:
use AdachSoft\AiToolCall\SPI\Collection\ConfigMap;
use AdachSoft\AiToolCall\SPI\Collection\KeyValueMap;
use AdachSoft\AiToolCall\SPI\Dto\ToolCallRequestDto;
use AdachSoft\ImageReadTool\ImageReadToolFactory;
$tool = (new ImageReadToolFactory())->create(new ConfigMap([
'base_path' => '/absolute/path/to/project',
]));
$result = $tool->callTool(new ToolCallRequestDto(
'image_read',
new KeyValueMap(['path' => 'docs/screenshot.png', 'max_dimension' => 512]),
));
$metadata = iterator_to_array($result->result);
$media = iterator_to_array($result->media);
The result map never contains image bytes: it carries metadata only (see Result metadata). Media contains exactly one ToolMediaDto when include_image is true and is empty otherwise. Invalid input raises InvalidToolCallException, while read and preparation failures raise ToolExecutionException.
Integration
Register ImageReadToolFactory in the tool factory registry of your adachsoft/ai-tool-call integration. The registry calls getToolClass() to expose the tool definition and create() to build a tool bound to a single project:
use AdachSoft\AiToolCall\SPI\Collection\ConfigMap;
use AdachSoft\ImageReadTool\ImageReadToolFactory;
$factory = new ImageReadToolFactory();
$tool = $factory->create(new ConfigMap([
'base_path' => '/absolute/path/to/project',
'max_dimension' => 1024,
]));
The factory validates the configuration and throws ToolConfigurationException when base_path or any limit is invalid. Without an explicit argument the factory uses the built-in GD-based reader; pass your own ImageReaderInterface implementation to the constructor to replace it:
use AdachSoft\ImageReadTool\ImageReadToolFactory;
$factory = new ImageReadToolFactory($customImageReader);
The tool itself is a thin SPI adapter, so it can be called directly as well:
use AdachSoft\AiToolCall\SPI\Collection\KeyValueMap;
use AdachSoft\AiToolCall\SPI\Dto\ToolCallRequestDto;
$result = $tool->callTool(new ToolCallRequestDto(
'image_read',
new KeyValueMap(['path' => 'docs/screenshot.png', 'max_dimension' => 512]),
));
$metadata = iterator_to_array($result->result);
$media = iterator_to_array($result->media);
See docs/examples/ for complete examples.