ImageMagick draw script on stdout. ## ## Used for any slate script that emits text rather than an image. A blank ## first line is treated as no header. ## ## Usage: render-text.php ## date_default_timezone_set(getenv('SLATE_TZ') ?: 'UTC'); $W = (int)($argv[1] ?? 1072); $H = (int)($argv[2] ?? 1448); $MARGIN = 44; $INK = 'black'; $FADED = '#9a9a9a'; $lines = []; while (($raw = fgets(STDIN)) !== false) $lines[] = rtrim($raw, "\n"); while (count($lines) && $lines[count($lines) - 1] === '') array_pop($lines); function esc($s) { return str_replace(["\\", "'"], ["\\\\", "\\'"], $s); } $head = count($lines) ? array_shift($lines) : ''; $head_h = $head === '' ? $MARGIN : 132; $avail = $H - $head_h - $MARGIN; $n = max(count($lines), 1); $row_h = min(64, intdiv($avail, $n)); ## size to fit the widest line as well as the row height, so wide output ## shrinks instead of getting clipped $widest = 0; foreach ($lines as $l) $widest = max($widest, strlen($l)); $fs_h = (int)($row_h * 0.62); $fs_w = $widest ? (int)((($W - 2 * $MARGIN)) / ($widest * 0.60)) : $fs_h; $fs = max(12, min($fs_h, $fs_w)); $out = []; $out[] = "push graphic-context"; if ($head !== '') { $out[] = "font-size 60"; $out[] = "fill $INK"; $out[] = sprintf("text %d,%d '%s'", $MARGIN, 74, esc($head)); $out[] = "font-size 30"; $out[] = "fill $FADED"; $out[] = sprintf("text %d,%d '%s'", $MARGIN, 110, esc(date('D j M H:i'))); $out[] = "fill $INK"; $out[] = sprintf("rectangle %d,%d %d,%d", $MARGIN, 124, $W - $MARGIN, 127); } ## clip to what fits rather than running off the edge $room = (int)((($W - $MARGIN) - $MARGIN) / ($fs * 0.60)); $y = $head_h; $out[] = "font-size $fs"; $out[] = "fill $INK"; foreach ($lines as $line) { $y += $row_h; if ($y > $H - $MARGIN) break; if (strlen($line) > $room) $line = substr($line, 0, $room - 3) . '...'; if ($line === '') continue; $out[] = sprintf("text %d,%d '%s'", $MARGIN, $y, esc($line)); } $out[] = "pop graphic-context"; echo implode("\n", $out), "\n";