Search by

marekskopal / lithophane

marekskopal

Generate printable lithophane STL models from images. Library + CLI.

Package info

github.com/marekskopal/lithophane

pkg:composer/marekskopal/lithophane

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v0.2.0 2026-11-22 20:20 UTC

This package is auto-updated.

Last update: 2026-11-22 20:20:59 UTC


README

Turn a photo into a printable flat lithophane STL — as a PHP library and a command line tool.

Dark pixels become thick, light pixels thin; held against light, the print shows the photo.

Features

  • All frame options of lithophanemaker.com's Framed Lithophane: frame, twine tunnels, hook tab, keychain hole, no border, and no border with a printing stand
  • The plate sits centred in the frame with sloped ledges, standing upright for printing without supports
  • Imports lithophanemaker.com settings-*.txt files to reproduce a model
  • Always watertight (manifold) meshes, streamed to binary STL with flat memory use
  • PNG preview of the backlit result
  • Crop position and scale, negative (inverted) lithophanes, EXIF photo orientation

Requirements

  • PHP 8.4 or newer
  • ext-gd (JPEG, PNG, WebP, … — whatever your GD build supports)
  • ext-exif (optional) to honour the orientation of photos taken by phones and cameras

Installation

composer require marekskopal/lithophane

Command line

vendor/bin/lithophane photo.jpg                                   # photo.stl next to the image
vendor/bin/lithophane photo.jpg -o photo.stl --frame=hook-tab --preview=photo.png
vendor/bin/lithophane photo.jpg --config=preset.json --width=80   # JSON preset + override
vendor/bin/lithophane photo.jpg --settings=settings-window-photo.txt
vendor/bin/lithophane photo.jpg --dry-run                         # sizes and estimates only
vendor/bin/lithophane --help
Option Default Meaning
-o, --output image name with .stl Output STL path
-p, --preview – Also write a PNG preview of the backlit result
-f, --frame frame Frame option, see below
--width, --height 100, 100 Outer size including the frame (mm); the hook tab is added on top
--resolution 0.2 Distance between relief points (mm/pixel)
--min-thickness, --max-thickness 0.6, 3.0 Thickness of white / black areas (mm)
--frame-width 7.5 Border size, "Base Height" on lithophanemaker.com (mm)
--frame-depth 7.5 Frame thickness, "Depth" on lithophanemaker.com (mm); the plate is centred in it
--overhang-angle 60 Slope of the ledges between frame and plate, from vertical
--tab-width, --tab-height 20, 15 Hook tab size (mm)
--hole-diameter 5 Hook tab or keychain hole diameter (mm)
--tunnel-diameter 2 Twine tunnel size (mm)
--stand-thickness, --stand-angle 2, 75 Printing stand thickness along the width (mm) and leg angle from horizontal
--x-shift, --y-shift 0.5 Crop position (0 = left/top, 1 = right/bottom)
--crop-scale 1.0 Fraction of the largest crop rectangle with the picture's aspect ratio
--invert off Negative lithophane
--orientation upright upright (X = thickness, Y = width, Z = height) or flat (lying on its back, relief up)
-c, --config – JSON preset with any of the options above (kebab-case or camelCase keys)
-s, --settings – lithophanemaker.com settings-*.txt (all frame options)
--dry-run off Print dimensions, triangle count and file size estimate without writing the STL

Settings are applied in this order, later ones winning: defaults, --settings, --config, other options.

Frame options

--frame lithophanemaker.com Shape
frame Frame Only Border around the plate, ledges sloped by the overhang angle on all four sides
twine-tunnels Twine Tunnels Frame with square tunnels running diagonally through both top corners, for hanging on twine
hook-tab Hook Tab Frame with a rounded tab and hole above the top centre
keychain-hole Framed Keychain Hole Top Left Frame with a hole through the top left corner
none No Border The bare plate
stand No Border and Printing Stand Bare plate with an A-frame stand at both sides: legs at the stand angle, a base bar and braces

The stand is written as separate closed solids overlapping the plate; slicers merge them into one object. An example preset is in examples/hook-tab.json.

Library usage

use MarekSkopal\Lithophane\Config\FrameType;
use MarekSkopal\Lithophane\Config\LithophaneConfig;
use MarekSkopal\Lithophane\Image\ImageLoader;
use MarekSkopal\Lithophane\LithophaneGenerator;

$config = new LithophaneConfig(width: 100, height: 150, frame: FrameType::HookTab);
$generator = new LithophaneGenerator();

// One shot: image file in, STL file out
$result = $generator->generate('photo.jpg', $config, 'photo.stl');
printf("%d triangles, %.1f × %.1f × %.1f mm\n", $result->triangles, $result->widthMm, $result->heightMm, $result->depthMm);

// Step by step, e.g. to also render a preview or count triangles first
$field = $generator->heightField(ImageLoader::fromFile('photo.jpg'), $config);
echo $generator->estimateTriangles($field), " triangles\n";
$generator->writeStl($field, 'photo.stl', $config->orientation);
$generator->writePreview($field, $config, 'photo.png');

LithophaneConfig::fromArray() builds a config from loose values (for example a decoded JSON preset), and LithophaneMakerSettings::fromFile() imports a lithophanemaker.com settings file:

use MarekSkopal\Lithophane\Config\LithophaneMakerSettings;

$settings = LithophaneMakerSettings::fromFile('settings-window-photo.txt');
$config = LithophaneConfig::fromArray(['resolution' => 0.3], $settings->config);

To stream triangles somewhere other than an STL file, implement MarekSkopal\Lithophane\Geometry\TriangleSink and pass it to $generator->mesh($field, $sink, $config->orientation).

How it works

  1. Crop the image to the picture's aspect ratio and resample it to one sample per grid point.
  2. Brightness → thickness: t = max − L · (max − min), with L the Rec. 601 luma (0 = black, 1 = white).
  3. Height field: picture, frame and hook tab live in one grid with a front and back surface per point; holes and outlines are cells marked empty, twine tunnels are cells hollowed out between two heights. The plate is centred in the frame, ledges slope by the overhang angle, and under the top bar the slope continues down to the relief.
  4. Mesher: emits a closed, manifold solid — relief, back, tunnels and walls on every boundary.
  5. Binary STL is streamed to disk in the requested orientation.

A 90 × 90 mm lithophane at 0.2 mm/pixel with a frame as thick as the plate is about 410 k triangles / 20 MB and takes about 2 s; with a thicker frame (the default) the back is no longer flat and the file roughly doubles.

php tools/stl-heightmap.php reference.stl fixture.png <frameWidth> <minThickness> <maxThickness>

Running tests

composer install
vendor/bin/phpunit                                                  # tests
vendor/bin/phpunit --coverage-text --only-summary-for-coverage-text # as CI runs them (needs pcov or xdebug)
vendor/bin/phpstan analyse                                          # static analysis, max level
vendor/bin/phpcs                                                    # coding standard (vendor/bin/phpcbf to fix)

CI requires at least 98 % line coverage.

License

MIT, see LICENSE.