adachsoft / image-vectorizer-vtracer
VTracer CLI adapter for the AdachSoft image vectorization contract.
Package info
gitlab.com/a.adach/image-vectorizer-vtracer
pkg:composer/adachsoft/image-vectorizer-vtracer
Requires
- php: ^8.3
- adachsoft/command-executor-lib: ^2.4
- adachsoft/image-vectorizer-contracts: ^0.1
Requires (Dev)
- adachsoft/php-code-style: ^0.8.0
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13
- rector/rector: ^2.9
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
adachsoft/image-vectorizer-vtracer implements the provider-independent image-vectorization contract with the local VTracer command-line program. It converts PNG, JPEG, WebP, and GIF raster images into validated SVG documents without network access.
Requirements
- PHP 8.3 or later
adachsoft/image-vectorizer-contracts^0.1adachsoft/command-executor-lib^2.4- VTracer CLI available on
PATH, or an explicit executable path
Install the PHP adapter with Composer:
composer require adachsoft/image-vectorizer-vtracer
Installation
Download a prebuilt binary archive from the VTracer GitHub releases, such as vtracer-x86_64-unknown-linux-musl.tar.gz, extract the vtracer executable into ~/.local/bin, and ensure that directory is on PATH:
mkdir -p ~/.local/bin
# Extract the downloaded release archive into ~/.local/bin.
export PATH="$HOME/.local/bin:$PATH"
Alternatively, install the CLI with Rust:
cargo install vtracer
Use VtracerSettings::binaryPath when the executable is installed at a non-standard path.
Usage
<?php
declare(strict_types=1);
use AdachSoft\ImageVectorizerContracts\Model\ColorModeEnum;
use AdachSoft\ImageVectorizerContracts\Model\RasterImage;
use AdachSoft\ImageVectorizerContracts\Model\VectorizeOptions;
use AdachSoft\ImageVectorizerContracts\Model\VectorizeRequest;
use AdachSoft\ImageVectorizerVtracer\VtracerImageVectorizer;
use AdachSoft\ImageVectorizerVtracer\VtracerSettings;
$settings = new VtracerSettings(
binaryPath: 'vtracer',
timeoutSeconds: 120,
hierarchical: 'stacked',
);
$vectorizer = VtracerImageVectorizer::create($settings);
$request = new VectorizeRequest(
RasterImage::fromFile(__DIR__ . '/logo.png'),
new VectorizeOptions(
colorMode: ColorModeEnum::COLOR,
maxColors: 8,
detail: 7,
),
);
$result = $vectorizer->vectorize($request);
$result->svg->save(__DIR__ . '/logo.svg');
VtracerImageVectorizer also accepts an injected CommandExecutorInterface, allowing unit tests to use a fake executor without installing or launching VTracer. VtracerAvailability::check($settings) checks the configured executable with --version; results are cached by each checker instance.
Option mapping
| Contract option | VTracer option | Mapping |
|---|---|---|
colorMode | --colormode | COLOR → color; BINARY → binary. |
curveFit | --mode | SPLINE → spline; POLYGON → polygon; PIXEL → none. |
maxColors | --color_precision | ceil(log2(maxColors)), clamped to 1–8 bits. This is an approximation because VTracer accepts precision bits rather than an exact palette size. |
speckleFilterPx | --filter_speckle | Used directly when supplied; zero disables filtering. |
cornerThresholdDegrees | --corner_threshold | Used directly when supplied. |
pathPrecision | --path_precision | Used directly when supplied. |
detail | --filter_speckle, --gradient_step | Supplies defaults only when a corresponding explicit option is absent. |
VtracerSettings::hierarchical | --hierarchical | Defaults to stacked; cutout is also supported. |
The detail defaults increase retained detail by lowering both thresholds:
| Detail | Speckle filter (px) | Gradient step |
|---|---|---|
| 1 | 10 | 20 |
| 2 | 9 | 18 |
| 3 | 8 | 16 |
| 4 | 7 | 14 |
| 5 | 6 | 12 |
| 6 | 5 | 10 |
| 7 | 4 | 8 |
| 8 | 3 | 6 |
| 9 | 2 | 4 |
| 10 | 1 | 2 |
VectorizeOptions::providerOptions accepts only VTracer option names from this allowlist: colormode, hierarchical, mode, filter_speckle, color_precision, gradient_step, corner_threshold, segment_length, splice_threshold, path_precision, and preset. Keys may include or omit the leading --. Provider options override the corresponding common-option mapping. Input and output paths are managed by the adapter and cannot be overridden.
Security and process handling
Every executable and argument token is escaped individually with escapeshellarg; values supplied by callers are never interpolated into an unescaped shell command. In-memory images and SVG output use temporary files in the configured working directory, and those files are removed after both successful and failed conversions. Process execution uses the configured timeout and captures standard output and standard error separately.
The shared contract exceptions intentionally expose generic provider-level messages rather than raw process diagnostics. Install VTracer from the sources above when the adapter reports that the executable is unavailable.
Running tests
Run unit and integration tests:
vendor/bin/phpunit
Run only unit tests:
vendor/bin/phpunit --testsuite default
Run the separate integration suite:
vendor/bin/phpunit --testsuite integration
Integration tests generate a 64×64 PNG with GD and are skipped when VtracerAvailability cannot find a working VTracer executable. Unit tests use a fake command executor and do not require the VTracer binary or network access.