<?php
function buildNFO($nfotext, $footer = "", $fg = "cc6600") {
  // Write Headers for PNG
  header("Content-Type: image/png");
  header("Content-Disposition: inline; filename=\"nfo2png.sf.net.png\"");
  
  if(!strlen($nfotext)) $nfotext = "Empty String submitted.";

  $nfo = explode("\n", $nfotext);
  
  // Load the Bitmap-Font
  $fnt = imageloadfont("backend/nfo2pngfont");
             
  // Check for empty lines
  $fillers = strlen($nfo[1]) + strlen($nfo[3]) + strlen($nfo[5]) + strlen($nfo[7]) < 9 ? 1 : 0;
  
  $nxo = array();
  $xmax = 0;
  
  // Reformat each line
  foreach($nfo as $key => $line) {
    $line = chop($line);
    if($xmax < strlen($line)) $xmax = strlen($line);
    if($fillers and ($key & 1)) continue;
    array_push($nxo, $line);
  }
  
  // Show footer
  if(strlen($footer)) {
    array_push($nxo, "");
    $fill = str_repeat(" ", ($xmax - strlen($footer) >> 1));
    array_push($nxo, $fill . $footer);
  }
  
  // Maximum characters per line based on image width
  $maxCharsPerLine = floor(($xmax * 8) / 8); // Assuming 8 pixels per character width
  
  // Process each line to handle long lines
  $processedNxo = [];
  foreach ($nxo as $line) {
    if (strlen($line) > $maxCharsPerLine) {
      // Break long lines into multiple lines
      $brokenLines = str_split($line, $maxCharsPerLine);
      $processedNxo = array_merge($processedNxo, $brokenLines);
    } else {
      $processedNxo[] = $line;
    }
  }

  // Update ymax based on processed lines
  $ymax = count($processedNxo);
  
  // Set foreground color
  $color = array(0, 0, 0);
  if(strlen($fg) == 6) {
    $color[0] = intval(substr($fg,0,2), 16);
    $color[1] = intval(substr($fg,2,2), 16);
    $color[2] = intval(substr($fg,4,2), 16);
  }
                      
  // Render NFO
  $im = ImageCreate($xmax * 8, $ymax * 16);
  ImageInterlace($im, 1); 
  $background_color = ImageColorTransparent($im, ImageColorAllocate($im, 254, 254, 126));
  $text_color = ImageColorAllocate($im, $color[0], $color[1], $color[2]);
  
  foreach($processedNxo as $y => $line) {
    ImageString($im, $fnt, 0, $y * 16, $line, $text_color);
  }
  ImagePNG($im);
}
?>