CandyCodeshot
Code → SVG/PNG screenshot generator
Turn code or terminal output into an SVG or PNG screenshot. The one base requirement is ext-mbstring; the SVG path needs no raster extension (no ext-gd, no Imagick) — plain text output suitable for git diffs and CI artifacts; PNG renders through ext-gd. ANSI SGR (16 / 256 / 24-bit truecolor + bold / italic / underline) becomes per-segment <text> color and font attributes plus <rect> background bands in the SVG; the PNG canvas paints the same foreground colors and background bands.
Install
composer require sugarcraft/candy-codeshot
Quickstart
use SugarCraft\CodeShot\SvgRenderer;
$code = <<<'PHP'
<?php
echo "hello, world\n";
PHP;
$svg = SvgRenderer::dracula()
->withLineNumbers(true)
->withWindow(true)
->withPadding(24)
->render($code);
file_put_contents('out.svg', $svg);
// ANSI input is honoured — SGR colours become per-segment <text> fills
// and <rect> background bands.
$svg = SvgRenderer::dark()
->render("\x1b[31merror:\x1b[0m something broke");
What's in the box
Pure-PHP SVGThe SVG path has no raster pipeline — its output is a text file, diffable, grep-able. PNG is a separate renderer behind ext-gd.
Window chromemacOS-style traffic-light controls, optional drop shadow, configurable border radius.
Line numbersOptional left-gutter line counter.
Five themes + file importdark, light, dracula, tokyo-night, nord — or
--theme <path.json> / the theme loaders for VS Code and flat-palette files.ANSI honoredPipe colored terminal output through; SGR sequences become per-segment text colors, bold / italic / underline and background bands.
Per-segment backgroundANSI background colors render as <rect> fills behind text.
Ligatures (SVG only)Enable font-variant-ligatures for code ligature rendering (→, >=, !==, etc.).
Language detectionHeuristic detection from filename extension, then shebang, then content signatures; sixteen grammars get color, other detected names render plain.
CLI + library
candy-codeshot input.php -o out.svg, or SvgRenderer::dracula()->render($code).Source & demos
- Source on GitHub
- Full README
- Runnable examples
- Originally inspired by freeze (Go); SugarCraft is developed as a native PHP project.
API
| Class | Method | Description |
|---|---|---|
| SvgRenderer | dracula(), dark(), light(), tokyoNight(), nord() | Create renderer with theme |
| SvgRenderer | withLineNumbers(bool) | Show line numbers |
| SvgRenderer | withWindow(bool) | Show window chrome |
| SvgRenderer | withPadding(pixels) | Set padding |
| SvgRenderer | withLigatures(bool) | Enable font-variant-ligatures |
| SvgRenderer | withBorderRadius(int) | Set corner radius (0..256; the CLI refuses above 64) |
| SvgRenderer | withShadow(bool) / withBorder(bool) | Toggle the drop shadow / the frame outline |
| SvgRenderer | withWindowStyle(string|WindowStyle) | Chrome style: macos, windows-terminal, iterm, hyper, none |
| SvgRenderer | withTheme(Theme) | Swap the whole palette |
| SvgRenderer | withFont(?string) | Embed a TTF/OTF/WOFF/WOFF2 file (library checks existence only; the CLI additionally validates the signature) |
| SvgRenderer | withHighlight(int, int, ?string) | Band-highlight lines start..end (SVG only) |
| SvgRenderer | withTitle(string) | Set the document's accessible name (child <title> only — no aria-labelledby anchor; not a visual caption) |
| SvgRenderer | render(code) | Render code to SVG |
| PngRenderer | dark(), light(), dracula(), tokyoNight(), nord(), render(code) | GD raster sibling — same fluent surface minus the SVG-only setters; requires ext-gd, and withBorderRadius() is accepted but corners stay square |
| Theme | dark(), light(), dracula(), tokyoNight(), nord() | Built-in palettes; new Theme(...) for custom colors |
| VsCodeThemeLoader / FlatPaletteThemeLoader | load(path), fromArray(data) | Build a Theme from a VS Code theme or flat JSON palette |
| CodeHighlighter | forTheme(Theme), highlight(code, language) | Turn plain code into ANSI-styled tokens using the theme palette |
| LanguageDetector | detect(content) | Detect language from content (shebang or signatures; reads at most 1 MB) |
| LanguageDetector | detectFromFilename(filename) | Detect language from filename extension |
| Segment | withBg(?string) | Return segment with different background color |
| FileOutputWriter / StringOutputWriter | write(chunk), flush(), close() / result() | Caller-chunked buffer/forward helpers — library-side only; the renderers return complete bytes and the CLI writes via file_put_contents/echo, never through these |
Demos.
VHS-recorded GIF demos of the screenshot and ANSI-pipe flows. Regenerated in CI on pushes to master that change the library.
Screenshot
Code → SVG with traffic-light chrome + line numbers.