PLWL/tools/svg2png.sh
2026-09-25 19:46:58 +02:00

33 lines
1 KiB
Bash

#!/usr/bin/env bash
# Convert an SVG file to a square PNG using whichever converter is available.
# Tries, in order: rsvg-convert, inkscape, magick, convert.
# Usage: svg2png.sh <input.svg> <output.png> [size]
set -e
in="$1"
out="$2"
size="${3:-256}"
[ -f "$in" ] || { echo "svg2png: input not found: $in" >&2; exit 1; }
if command -v rsvg-convert >/dev/null 2>&1; then
rsvg-convert -w "$size" -h "$size" -o "$out" "$in" && exit 0
fi
if command -v inkscape >/dev/null 2>&1; then
if inkscape --version 2>/dev/null | grep -Eq 'Inkscape 0\.'; then
inkscape -z "$in" -e "$out" -w "$size" -h "$size" && exit 0
fi
inkscape "$in" --export-type=png --export-filename="$out" -w "$size" -h "$size" && exit 0
fi
if command -v magick >/dev/null 2>&1; then
magick -background none "$in" -resize "${size}x${size}" "$out" && exit 0
fi
if command -v convert >/dev/null 2>&1; then
convert -background none "$in" -resize "${size}x${size}" "$out" && exit 0
fi
echo "svg2png: no SVG converter found (install librsvg, inkscape or imagemagick)" >&2
exit 1