Search by

adachsoft / image-vectorizer-vtracer

Arkadiusz Adach

VTracer CLI adapter for the AdachSoft image vectorization contract.

Package info

gitlab.com/a.adach/image-vectorizer-vtracer

Issues

pkg:composer/adachsoft/image-vectorizer-vtracer

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

v0.1.0 2026-11-29 15:22 UTC

This package is auto-updated.

Last update: 2026-11-29 14:22:40 UTC


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.1
  • adachsoft/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 optionVTracer optionMapping
colorMode--colormodeCOLOR → color; BINARY → binary.
curveFit--modeSPLINE → spline; POLYGON → polygon; PIXEL → none.
maxColors--color_precisionceil(log2(maxColors)), clamped to 1–8 bits. This is an approximation because VTracer accepts precision bits rather than an exact palette size.
speckleFilterPx--filter_speckleUsed directly when supplied; zero disables filtering.
cornerThresholdDegrees--corner_thresholdUsed directly when supplied.
pathPrecision--path_precisionUsed directly when supplied.
detail--filter_speckle, --gradient_stepSupplies defaults only when a corresponding explicit option is absent.
VtracerSettings::hierarchical--hierarchicalDefaults to stacked; cutout is also supported.

The detail defaults increase retained detail by lowering both thresholds:

DetailSpeckle filter (px)Gradient step
11020
2918
3816
4714
5612
6510
748
836
924
1012

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.