Search by

adachsoft / image-vectorizer-contracts

Arkadiusz Adach

Provider-independent contracts and value objects for raster-to-SVG image vectorization in PHP.

Package info

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

Issues

pkg:composer/adachsoft/image-vectorizer-contracts

Statistics

Installs: 4

Dependents: 1

Suggesters: 0

Stars: 0

v0.1.0 2026-11-29 14:51 UTC

This package is auto-updated.

Last update: 2026-11-29 13:51:24 UTC


README

A provider-independent PHP contract for converting raster images into SVG vector graphics. Implementations can wrap local command-line tools such as VTracer or remote vectorization APIs without coupling consumers to a specific provider.

Requirements

  • PHP 8.3 or later
  • adachsoft/collection ^3

Installation

composer require adachsoft/image-vectorizer-contracts

Supported image formats

RasterImage supports PNG (image/png), JPEG (image/jpeg), WebP (image/webp), and GIF (image/gif). MIME types are detected from magic bytes when creating an image from bytes or a file. File-backed images retain their original path and load their bytes only when requested.

Usage

Create a request for a PNG logo, ask an injected provider to vectorize it with a four-color palette hint, and save the resulting SVG:

<?php

declare(strict_types=1);

use AdachSoft\ImageVectorizerContracts\Model\RasterImage;
use AdachSoft\ImageVectorizerContracts\Model\VectorizeOptions;
use AdachSoft\ImageVectorizerContracts\Model\VectorizeRequest;
use AdachSoft\ImageVectorizerContracts\Service\ImageVectorizerInterface;

/** @var ImageVectorizerInterface $vectorizer Injected provider adapter. */
$vectorizer = $container->get(ImageVectorizerInterface::class);
$request = new VectorizeRequest(
    RasterImage::fromFile(__DIR__ . '/logo.png'),
    new VectorizeOptions(maxColors: 4),
);

$result = $vectorizer->vectorize($request);
$result->svg->save(__DIR__ . '/logo.svg');

VectorizeResult also exposes the provider name, elapsed milliseconds, and a list of non-fatal warnings. SvgDocument validates XML with an SVG root and provides parsed dimensions and a count of path and basic shape elements.

Model

  • RasterImage accepts either bytes or a readable file path. Use bytes(), contentType(), and path() to access its source.
  • VectorizeOptions provides common color mode, palette size, detail, speckle filtering, corner threshold, curve fitting, path precision, and scalar provider-specific options. Numeric options are nullable where the provider should choose its own default.
  • VectorizeRequest groups an image and its options.
  • SvgDocument represents validated SVG markup and can save it to a file.
  • VectorizeResult groups the SVG with provider metadata and warnings.

Implementing an adapter

Implement ImageVectorizerInterface and inject that adapter wherever vectorization is needed. name() returns a stable provider name, capabilities() advertises supported option families, and vectorize() maps the request to the provider API or command-line tool and returns a VectorizeResult.

Adapters should reject unsupported requested option values with UnsupportedVectorizeOptionException, report unavailable binaries or services with VectorizerUnavailableException, and use VectorizationFailedException for provider failures. Keep credentials and raw provider diagnostics out of exception messages. A file-backed RasterImage exposes its original path so command-line adapters can avoid copying the input when the tool supports path-based input.

Capabilities

VectorizerCapabilitySet is an immutable, type-safe collection of VectorizerCapabilityEnum values. Capabilities cover color and binary modes, maximum colors, speckle filtering, polygon curve fitting, and pixel curve fitting. Consumers can inspect a provider before selecting compatible options:

use AdachSoft\ImageVectorizerContracts\Model\VectorizerCapabilityEnum;

if ($vectorizer->capabilities()->supports(VectorizerCapabilityEnum::MAX_COLORS)) {
    // Palette-size options can be sent to this provider.
}

InMemoryImageVectorizer is provided for consumer tests. It records requests, accepts configurable capabilities and SVG output, and otherwise returns a rectangle using the source image dimensions when available or 100 by 100 units as a fallback.