<?php
  // paintbrush_png_jpg.php
  // August, 2025
  // crontab php command line called way to backup Paintbrush created *.png to reduced size *.jpg
  $pdfprefix="";
  $pdfcommand="";
  $gifcommand="";
  $it = new GlobIterator(__DIR__ . "/*.png");
  $last2mis = 9020;
  foreach ( $it as $file ) {
    if ((time() - $file->getMTime()) <= $last2mis) {
      if (strpos($file, '_to_pdf') !== false || strpos($file, '_via_pdf') !== false) {
        if (strpos($file, '_to_pdf') !== false) {
          $pdfprefix=explode('_to_pdf', $file)[0];
          if (file_exists($pdfprefix . ".Pdf")) {
            unlink($pdfprefix . ".Pdf");
          }
          $pdfcommand=' ;  /usr/local/bin/convert -quality 100 "' . $pdfprefix . '*.{jpeg}" ' . $pdfprefix . '.Pdf';
        } else if (strpos($file, '_via_pdf') !== false) {
          $pdfprefix=explode('_via_pdf', $file)[0];
          if (file_exists($pdfprefix . ".Pdf")) {
            unlink($pdfprefix . ".Pdf");
          }
          $pdfcommand=' ;  /usr/local/bin/convert -quality 100 "' . $pdfprefix . '*.{jpeg}" ' . $pdfprefix . '.Pdf';
        }
      } 
      if (strpos($file, '_to_gif') !== false || strpos($file, '_via_gif') !== false) {
        if (strpos($file, '_to_gif') !== false) {
          $gifprefix=explode('_to_gif', $file)[0];
          if (file_exists($gifprefix . ".Gif")) {
            unlink($gifprefix . ".Gif");
          }
          $gifcommand=' ;  /usr/local/bin/convert -delay 400 "' . $gifprefix . '*.{jpeg}" -loop 0 ' . $gifprefix . '.Gif';
        } else if (strpos($file, '_via_gif') !== false) {
          $gifprefix=explode('_via_gif', $file)[0];
          if (file_exists($gifprefix . ".Gif")) {
            unlink($gifprefix . ".Gif");
          }
          $gifcommand=' ;  /usr/local/bin/convert -delay 400 "' . $gifprefix . '*.{jpeg}" -loop 0 ' . $gifprefix . '.Gif';
        }
      }
      if (!file_exists(str_replace('.png','.jpg',$file)) && !file_exists(str_replace('.png','.jpeg',$file))) {
        exec("cd " . __DIR__ . '; /usr/local/bin/convert -quality 30 ' . $file . ' ' . str_replace('.png','.jpeg',$file) . $pdfcommand . $gifcommand);
      } else if (!file_exists(str_replace('.png','.jpg',$file)) && file_exists(str_replace('.png','.jpeg',$file))) {
        exec("cd " . __DIR__ . '; /usr/local/bin/convert -quality 30 ' . $file . ' ' . str_replace('.png','.jpg',$file) . $pdfcommand . $gifcommand);
      }
    }
  }
?>
