encodePng static method

String encodePng(
  1. List<int> pngBytes, {
  2. String? name,
  3. int? columns,
  4. int? rows,
  5. bool preserveAspectRatio = true,
})

Encodes pre-encoded PNG bytes into iTerm2 Image Protocol escape sequences.

Use this when you already have PNG data and want to avoid re-encoding.

pngBytes is the PNG-encoded image data. name is an optional filename for the image. columns is the display width in terminal columns (or use string values like "auto", "Npx", "N%" via the protocol directly). rows is the display height in terminal rows. preserveAspectRatio whether to preserve aspect ratio (default true).

Returns a string containing the escape sequences to display the image.

Implementation

static String encodePng(
  List<int> pngBytes, {
  String? name,
  int? columns,
  int? rows,
  bool preserveAspectRatio = true,
}) {
  final base64Data = base64Encode(pngBytes);

  final args = StringBuffer('inline=1');
  if (name != null) {
    args.write(';name=${base64Encode(utf8.encode(name))}');
  }
  // Include file size so the terminal can show transfer progress.
  args.write(';size=${pngBytes.length}');
  if (columns != null) {
    args.write(';width=$columns');
  }
  if (rows != null) {
    args.write(';height=$rows');
  }
  if (!preserveAspectRatio) {
    args.write(';preserveAspectRatio=0');
  }

  return '${Ansi.osc}1337;File=$args:$base64Data${Ansi.st}';
}