Skip to content

Seamless Texture — PHP source

Make images tile seamlessly or generate tileable noise textures — entirely in your browser. Nothing uploads.

This is the PHP implementation — the same logic the interactive tool runs, in a shareable, citable form.

<?php
/**
 * seamless-texture — tileable-texture math, PHP port: wrap-blend weights,
 * integer lattice hash, wrapping value noise, torus Worley (ext-json free,
 * math ext only).
 * PHP port of the pure src/lib/seamless.ts (the TS lib has no canvas or DOM
 * dependency at all); the browser island owns all drawing. The 32-bit
 * avalanche is emulated with masks — PHP ints are 64-bit.
 */

declare(strict_types=1);

/** 32-bit wrapping multiplication — the Math.imul analogue. */
function imul(int $a, int $b): int
{
    $r = (($a & 0xFFFFFFFF) * ($b & 0xFFFFFFFF)) & 0xFFFFFFFF;
    return $r >= 0x80000000 ? $r - 0x100000000 : $r;
}

/** The JS `h >>> n`: a logical right shift of the int32 bit pattern. */
function shr(int $h, int $n): int
{
    return ($h & 0xFFFFFFFF) >> $n;
}

/**
 * Cross-fade weight for wrap-blending a texture's seams: 1 in the interior,
 * falling linearly to 0 across $feather pixels approaching each wrapped edge
 * (axis weights multiplied, clamped 0..1). $feather <= 0 disables the ramp.
 */
function blendWeight(float $x, float $y, float $w, float $h, float $feather): float
{
    if (!($feather > 0)) {
        return 1.0;
    }
    $fx = min($x, $w - $x) / $feather;
    $fy = min($y, $h - $y) / $feather;
    return min(1.0, max(0.0, $fx)) * min(1.0, max(0.0, $fy));
}

/**
 * Deterministic 0..1 hash of an integer lattice point: 32-bit integer
 * avalanche — h = x·0x9E3779B1 ^ y·0x85EBCA77 ^ seed·0xC2B2AE35 (truncated
 * to int32), then three xor-shift/multiply rounds; the unsigned result is
 * scaled by 1/2³². Same (x, y, seed) → same value on every platform and run.
 */
function hash2(int $x, int $y, int $seed): float
{
    $h = imul($x, 0x9E3779B1) ^ imul($y, 0x85EBCA77) ^ imul($seed, 0xC2B2AE35);
    $h ^= shr($h, 15);
    $h = imul($h, 0x2C1B3C6D);
    $h ^= shr($h, 12);
    $h = imul($h, 0x297A2D39);
    $h ^= shr($h, 15);
    return ($h & 0xFFFFFFFF) / 4294967296.0;
}

/** Mirrors the TS NoiseOptions (octaves < 1 snaps to 1). */
final class NoiseOptions
{
    public function __construct(
        public readonly int $seed,
        public readonly float $scale,   // tile period in px; noise(x + scale, y) === noise(x, y)
        public readonly int $octaves = 1, // ≥1; each octave doubles frequency, halves amplitude
    ) {}
}

/**
 * Wrapping value noise over a unit lattice; the field tiles exactly.
 * Octaves ≥ 2 sum frequency-doubled, amplitude-halved layers normalized by
 * total amplitude; scale < 1 snaps to 1.
 */
function valueNoise(float $x, float $y, NoiseOptions $opts): float
{
    $scale = max(1, round($opts->scale)); // PHP round() is half-away-from-zero, like JS Math.round here
    $octaves = max(1, intdiv((int) floor($opts->octaves), 1));
    $sum = 0.0;
    $total = 0.0;
    $amp = 1.0;
    $freq = 1.0;
    for ($o = 0; $o < $octaves; $o++) {
        $sum += $amp * layer($x * $freq, $y * $freq, $scale * $freq, $opts->seed + $o * 101);
        $total += $amp;
        $amp /= 2;
        $freq *= 2;
    }
    return $sum / $total;
}

/** One octave: bilinear (quintic-faded) interpolation of wrapped lattice hashes. */
function layer(float $x, float $y, float $period, int $seed): float
{
    $x0 = floor($x);
    $y0 = floor($y);
    $u = fade($x - $x0);
    $v = fade($y - $y0);
    $xa = wrapIdx($x0, $period);
    $xb = wrapIdx($x0 + 1, $period);
    $ya = wrapIdx($y0, $period);
    $yb = wrapIdx($y0 + 1, $period);
    $top = lerp(hash2((int) $xa, (int) $ya, $seed), hash2((int) $xb, (int) $ya, $seed), $u);
    $bottom = lerp(hash2((int) $xa, (int) $yb, $seed), hash2((int) $xb, (int) $yb, $seed), $u);
    return lerp($top, $bottom, $v);
}

/** Positive modulo — lattice indices wrap onto the torus of $period cells. */
function wrapIdx(float $i, float $period): float
{
    return fmod(fmod($i, $period) + $period, $period);
}

/** Quintic smoothstep fade — C2-continuous interpolation curve. */
function fade(float $t): float
{
    return $t * $t * $t * ($t * ($t * 6 - 15) + 10);
}

function lerp(float $a, float $b, float $t): float
{
    return $a + ($b - $a) * $t;
}

/** Mirrors the TS WorleyOptions: number of hashed feature points (≥1). */
final class WorleyOptions
{
    public function __construct(
        public readonly int $seed,
        public readonly float $points,
    ) {}
}

/** Mapped 0..1 distances to the nearest (d1) and second-nearest (d2) points. */
final class WorleyResult
{
    public function __construct(
        public readonly float $d1,
        public readonly float $d2,
    ) {}
}

/**
 * Wrapping (torus-metric) F1-F2 field; tiles exactly with period 1. Each
 * axis distance wraps via min(d, 1-d); raw torus distances top out at the
 * half-diagonal √½, which maps to 1. Fewer than one point returns the
 * all-max {1, 1}; exactly one point leaves d2 at the maximum.
 */
function worley(float $x, float $y, WorleyOptions $opts): WorleyResult
{
    // Wrap the query onto the unit tile first so shifted queries are exact.
    $wx = $x - floor($x);
    $wy = $y - floor($y);
    $n = (int) floor($opts->points);
    $d1 = 1.0;
    $d2 = 1.0;
    for ($i = 0; $i < $n; $i++) {
        $px = hash2($i, 0, $opts->seed);
        $py = hash2($i, 1, $opts->seed);
        $dx = abs($wx - $px);
        $dx = min($dx, 1 - $dx);
        $dy = abs($wy - $py);
        $dy = min($dy, 1 - $dy);
        $d = sqrt($dx * $dx + $dy * $dy) * M_SQRT2;
        if ($d < $d1) {
            $d2 = $d1;
            $d1 = $d;
        } elseif ($d < $d2) {
            $d2 = $d;
        }
    }
    return new WorleyResult($d1, $d2);
}

Also available in 13 other languages

Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →