Photo Merge — PHP source
Combine photos side by side, stacked, or in a grid — uniform cells, adjustable gap, one PNG out. Runs entirely in your browser.
This is the PHP implementation — the same logic the interactive tool runs, in a shareable, citable form.
<?php
/**
* photo-merge — merge-cell layout for side-by-side, stacked and grid merges.
*
* Language: PHP 7.4+ (standard library only).
* CosmoDev polyglot showcase port of the `photo-merge` tool, ported from
* src/tools/PhotoMerge.tsx.
*
* Why this file exists: the live island is canvas-bound — createImageBitmap
* decodes each photo, drawImage composites it, canvas.toBlob encodes the PNG,
* and the cells are painted over a #0f172a background. Those calls consume
* pure geometry (uniform cell size, per-image fit scale, centered placement);
* this port extracts that geometry so the math is inspectable without a
* browser.
*
* display source — part of CosmoDev's polyglot tool pages.
*/
declare(strict_types=1);
/** A decoded image's dimensions, in pixels. */
final class PhotoMergeImage
{
/** @var int */
public $width;
/** @var int */
public $height;
public function __construct(int $width, int $height)
{
$this->width = $width;
$this->height = $height;
}
}
/** Where one image lands on the merged canvas. */
final class PhotoMergePlacement
{
/** @var int cell column of this image */
public $col;
/** @var int cell row of this image */
public $row;
/** @var float aspect-preserving fit scale */
public $scale;
/** @var int top-left corner on the canvas */
public $x;
/** @var int top-left corner on the canvas */
public $y;
/** @var int fitted size in pixels */
public $w;
/** @var int fitted size in pixels */
public $h;
public function __construct(int $col, int $row, float $scale, int $x, int $y, int $w, int $h)
{
$this->col = $col;
$this->row = $row;
$this->scale = $scale;
$this->x = $x;
$this->y = $y;
$this->w = $w;
$this->h = $h;
}
}
/**
* Compute the merge layout for a list of PhotoMergeImage sizes.
*
* Mirrors the deterministic half of merge() in PhotoMerge.tsx: cellW/cellH
* are the largest input dimensions; 'row' stacks n cells horizontally,
* 'column' vertically, 'grid' folds them into ceil(sqrt(n)) columns; the
* canvas is cells plus perimeter gaps; each image is scaled by
* min(cellW / w, cellH / h) and centered inside its cell. PHP's round()
* rounds halves away from zero, which matches JS Math.round for the positive
* values produced here. Returns ['canvas_width', 'canvas_height', 'cols',
* 'rows', 'placements' => PhotoMergePlacement[]].
*
* @param PhotoMergeImage[] $images
* @return array{canvas_width: int, canvas_height: int, cols: int, rows: int, placements: array<int, PhotoMergePlacement>}
*/
function photo_merge_layout(array $images, int $gap = 12, string $layout = 'row'): array
{
$n = count($images);
$cellW = 0;
$cellH = 0;
foreach ($images as $img) {
$cellW = max($cellW, $img->width);
$cellH = max($cellH, $img->height);
}
$cols = $layout === 'row' ? $n : ($layout === 'column' ? 1 : (int) ceil(sqrt($n)));
$rows = $layout === 'row' ? 1 : ($layout === 'column' ? $n : (int) ceil($n / $cols));
$placements = [];
foreach ($images as $i => $img) {
$col = $i % $cols;
$row = intdiv($i, $cols);
$scale = min($cellW / $img->width, $cellH / $img->height);
$w = (int) round($img->width * $scale);
$h = (int) round($img->height * $scale);
$placements[$i] = new PhotoMergePlacement(
$col,
$row,
$scale,
$gap + $col * ($cellW + $gap) + (int) round(($cellW - $w) / 2),
$gap + $row * ($cellH + $gap) + (int) round(($cellH - $h) / 2),
$w,
$h
);
}
return [
'canvas_width' => $cols * $cellW + ($cols + 1) * $gap,
'canvas_height' => $rows * $cellH + ($rows + 1) * $gap,
'cols' => $cols,
'rows' => $rows,
'placements' => $placements,
];
}
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 →