<?php
// php_calls_pdfimages.php
// RJM Programming
// April, 2023
set_time_limit(1200);

function ourunlink($what) {
   $huhpis=explode('.', $what);
foreach (glob($huhpis[0] . '-*') as $filename) {
    try {
    unlink($filename);
    } catch (Exception $werty) { }
}
   unlink($what);
}

$fextra='';
$infile='"input_pdf.pdf"';
$pathp='.' . DIRECTORY_SEPARATOR;
if (isset($_GET['inpath'])) {
  if (trim(str_replace('+',' ',urldecode($_GET['inpath']))) != '') {
    $pathp=str_replace('+',' ',urldecode($_GET['inpath']));
    if (substr(strrev($pathp),0,1) != DIRECTORY_SEPARATOR) {
      $pathp.=DIRECTORY_SEPARATOR;
    }
  }
} else if (isset($_POST['inpath'])) {
  if (trim(str_replace('+',' ',urldecode($_POST['inpath']))) != '') {
    $pathp=str_replace('+',' ',urldecode($_POST['inpath']));
    if (substr(strrev($pathp),0,1) != DIRECTORY_SEPARATOR) {
      $pathp.=DIRECTORY_SEPARATOR;
    }
  }
}

if (isset($_GET['convertthis'])) {
  $infile='"' . $pathp . str_replace('+',' ',urldecode($_GET['convertthis'])) . '"';
  $fextra='<font size="1">&nbsp;<a target="_blank" title="Original PDF" href="file://' . urlencode(str_replace('"','',$infile)) . '">' . basename(str_replace('"','',$infile)) . '</a></font>';
} else if (isset($_POST['convertthis'])) {
  $infile='"' . $pathp . str_replace('+',' ',urldecode($_POST['convertthis'])) . '"';
  $fextra='<font size="1">&nbsp;<a target="_blank" title="Original PDF" href="file://' . urlencode(str_replace('"','',$infile)) . '">' . basename(str_replace('"','',$infile)) . '</a></font>';
}

if (isset($_POST['delv'])) {
  if ($_POST['delv'] != '') {
    if (file_exists(basename(str_replace('+',' ', urldecode($_POST['delv']))))) {
      if (strpos(strtolower(str_replace('+',' ', urldecode($_POST['delv'])) . "?"), ".mp4?") !== false) {
       sleep(20);
       if (file_exists(basename(str_replace('+',' ', urldecode($_POST['delv']))))) {
         ourunlink(basename(str_replace('+',' ', urldecode($_POST['delv']))));
       }
      }
    }
    if (!isset($_POST['delp']) && !isset($_POST['dela'])) {
      exit;
    }
  }
}

if (isset($_POST['dela'])) {
  if ($_POST['dela'] != '') {
    if (file_exists(basename(str_replace('+',' ', urldecode($_POST['dela']))))) {
      if (strpos(strtolower(str_replace('+',' ', urldecode($_POST['dela'])) . "?"), ".gif?") !== false) {
       sleep(20);
       if (file_exists(basename(str_replace('+',' ', urldecode($_POST['dela']))))) {
         ourunlink(basename(str_replace('+',' ', urldecode($_POST['dela']))));
       }
      }
    }
    if (!isset($_POST['delp'])) {
      exit;
    }
  }
}


if (isset($_POST['delp'])) {
  if ($_POST['delp'] != '') {
    if (file_exists(basename(str_replace('+',' ', urldecode($_POST['delp']))))) {
      if (strpos(strtolower(str_replace('+',' ', urldecode($_POST['delp'])) . "?"), ".pdf?") !== false) {
       sleep(20);
       if (file_exists(basename(str_replace('+',' ', urldecode($_POST['delp']))))) {
         ourunlink(basename(str_replace('+',' ', urldecode($_POST['delp']))));
       }
      }
    }
    exit;
  }
}

$vsw=0;
$vw=720;
$vh=540;
$dovideo=false;
$doag=false;
$dopdf=false;
$sparepdf="";

if (isset($_GET['dovideo']) || isset($_POST['dovideo'])) {
  $dovideo=true;
} 
if (isset($_GET['doag']) || isset($_POST['doag'])) {
  $doag=true;
}
if (isset($_GET['dopdf']) || isset($_POST['dopdf'])) {
  $dopdf=true;
}

$vsw=0;
$vw=720;
$vh=540;
if (isset($_GET['sw'])) {
  $vsw=intval($_GET['sw']);
  $vh=(540 * (720 / ($vsw * 0.48)));
  $vw=($vsw * 0.48);
  $vh=explode(".", "$vh")[0];
  $vh /= 2; 
  $vh=explode(".", "$vh")[0];
  $vh *= 2; 
  $vh=explode(".", "$vh")[0];
  $vw=explode(".", "$vw")[0]; 
  $vw /= 2; 
  $vw=explode(".", "$vw")[0];
  $vw *= 2; 
  $vh=intval("$vh");
  $vw=intval("$vw");
  //echo $vh . ',' . $vw . ' via ' . $vsw;
  //exit;
} else if (isset($_POST['sw'])) {
  $vsw=intval($_POST['sw']);
  $vh=(540 * (720 / ($vsw * 0.48)));
  $vw=($vsw * 0.48);
  $vh=explode(".", "$vh")[0];
  $vh /= 2; 
  $vh=explode(".", "$vh")[0];
  $vh *= 2; 
  $vh=explode(".", "$vh")[0];
  $vw=explode(".", "$vw")[0]; 
  $vw /= 2; 
  $vw=explode(".", "$vw")[0];
  $vw *= 2; 
  $vh=intval("$vh");
  $vw=intval("$vw");
  //echo $vh . ',' . $vw . ' via ' . $vsw;
  //exit;
}

if (isset($_GET['install'])) {
   if ($_GET['install'] == 'pdfimages') {
    if (PHP_OS == "Darwin") {
    putenv('PATH=/usr/local/bin');
    $x="<p>Regarding ...<br><br>brew install popper<br><br>If errors below, you should try command above, yourself, on command line ...<br><br><br>";
    $x.=shell_exec('brew install poppler 2> huh.jnk');
     if (file_exists('huhjunk.jnk')) {
       $x.=file_get_contents('huhjnk.jnk');
       unlink('huh.jnk');
     }
     echo "<html><body><pre>" . $x . "</pre></body></html>";
     } else if (PHP_OS =='WINNT' || PHP_OS =='WIN32' || PHP_OS =='Windows') {
     $x="<p>Regarding ...<br><br>sudo apt-get update <br>sudo apt-get install poppler-utils<br><br>If errors below, you should try commands above, yourself, on command line ...<br><br><br>";
     $x.=shell_exec('sudo apt-get update');
     $x.=shell_exec('sudo apt-get install poppler-utils');
     echo "<html><body><pre>" . $x . "</pre></body></html>";
     }
   } else if ($_GET['install'] == 'ffmpeg') {
    if (PHP_OS == "Darwin") {
    putenv('PATH=/usr/local/bin');
    $x="<p>Regarding ...<br><br>brew install ffmpeg<br><br>If errors below, you should try command above, yourself, on command line ...<br><br><br>";
    $x.=shell_exec('brew install ffmpeg 2> huh.jnk');
     if (file_exists('huhjunk.jnk')) {
       $x.=file_get_contents('huhjnk.jnk');
       unlink('huh.jnk');
     }
     echo "<html><body><pre>" . $x . "</pre></body></html>";
     } else if (PHP_OS =='WINNT' || PHP_OS =='WIN32' || PHP_OS =='Windows') {
     $x="<p>Regarding ...<br><br>sudo apt update -y <br>sudo apt install ffmpeg -y<br><br>If errors below, you should try commands above, yourself, on command line ...<br><br><br>";
     $x.=shell_exec('sudo apt update -y');
     $x.=shell_exec('sudo apt install ffmpeg -y');
     echo "<html><body><pre>" . $x . "</pre></body></html>";
     }
   } else if ($_GET['install'] == 'imagemagick') {
    if (PHP_OS == "Darwin") {
    putenv('PATH=/usr/local/bin');
    $x="<p>Regarding ...<br><br>brew install imagemagick<br><br>If errors below, you should try command above, yourself, on command line ...<br><br><br>";
    $x.=shell_exec('brew install imagemagick 2> huh.jnk');
     if (file_exists('huhjunk.jnk')) {
       $x.=file_get_contents('huhjnk.jnk');
       unlink('huh.jnk');
     }
     echo "<html><body><pre>" . $x . "</pre></body></html>";
     } else if (PHP_OS =='WINNT' || PHP_OS =='WIN32' || PHP_OS =='Windows') {
     $x="<p>Regarding ...<br><br>sudo apt update<br>sudo apt install imagemagick<br><br>If errors below, you should try commands above, yourself, on command line ...<br><br><br>";
     $x.=shell_exec('sudo apt update');
     $x.=shell_exec('sudo apt install imagemagick');
     echo "<html><body><pre>" . $x . "</pre></body></html>";
     }
   }
   exit;
}
if (isset($_POST['install'])) {
   if ($_POST['install'] == 'pdfimages') {
    if (PHP_OS == "Darwin") {
    putenv('PATH=/usr/local/bin');
    $x="<p>Regarding ...<br><br>brew install popper<br><br>If errors below, you should try command above, yourself, on command line ...<br><br><br>";
    $x.=shell_exec('brew install poppler 2> huh.jnk');
     if (file_exists('huhjunk.jnk')) {
       $x.=file_get_contents('huhjnk.jnk');
       unlink('huh.jnk');
     }
     echo "<html><body><pre>" . $x . "</pre></body></html>";
     } else if (PHP_OS =='WINNT' || PHP_OS =='WIN32' || PHP_OS =='Windows') {
     $x="<p>Regarding ...<br><br>sudo apt-get update <br>sudo apt-get install poppler-utils<br><br>If errors below, you should try commands above, yourself, on command line ...<br><br><br>";
     $x.=shell_exec('sudo apt-get update');
     $x.=shell_exec('sudo apt-get install poppler-utils');
     echo "<html><body><pre>" . $x . "</pre></body></html>";
     }
   } else if ($_POST['install'] == 'ffmpeg') {
    if (PHP_OS == "Darwin") {
    putenv('PATH=/usr/local/bin');
    $x="<p>Regarding ...<br><br>brew install ffmpeg<br><br>If errors below, you should try command above, yourself, on command line ...<br><br><br>";
    $x.=shell_exec('brew install ffmpeg 2> huh.jnk');
     if (file_exists('huhjunk.jnk')) {
       $x.=file_get_contents('huhjnk.jnk');
       unlink('huh.jnk');
     }
     echo "<html><body><pre>" . $x . "</pre></body></html>";
     } else if (PHP_OS =='WINNT' || PHP_OS =='WIN32' || PHP_OS =='Windows') {
     $x="<p>Regarding ...<br><br>sudo apt update -y <br>sudo apt install ffmpeg -y<br><br>If errors below, you should try commands above, yourself, on command line ...<br><br><br>";
     $x.=shell_exec('sudo apt update -y');
     $x.=shell_exec('sudo apt install ffmpeg -y');
     echo "<html><body><pre>" . $x . "</pre></body></html>";
     }
   } else if ($_POST['install'] == 'imagemagick') {
    if (PHP_OS == "Darwin") {
    putenv('PATH=/usr/local/bin');
    $x="<p>Regarding ...<br><br>brew install imagemagick<br><br>If errors below, you should try command above, yourself, on command line ...<br><br><br>";
    $x.=shell_exec('brew install imagemagick 2> huh.jnk');
     if (file_exists('huhjunk.jnk')) {
       $x.=file_get_contents('huhjnk.jnk');
       unlink('huh.jnk');
     }
     echo "<html><body><pre>" . $x . "</pre></body></html>";
     } else if (PHP_OS =='WINNT' || PHP_OS =='WIN32' || PHP_OS =='Windows') {
     $x="<p>Regarding ...<br><br>sudo apt update<br>sudo apt install imagemagick<br><br>If errors below, you should try commands above, yourself, on command line ...<br><br><br>";
     $x.=shell_exec('sudo apt update');
     $x.=shell_exec('sudo apt install imagemagick');
     echo "<html><body><pre>" . $x . "</pre></body></html>";
     }
   }
   exit;
}


$filepath='';
$rest="<iframe src=./php_calls_pdfimages.php frameborder=0 style='width:100%;height:1200px;'></iframe>";
$restfrom="<!--table id='thetable'><tbody><tr><td><iframe onload='iifopen(this);' id='cbi' frameborder='0' style='width:173px;height:228px;margin-top:-184px;' src='/HTMLCSS/client_browsing.htm?d=320562686'></iframe></td><td id='lstd'></td></tr></tbody></table>
<p id='myp'> ... or ...</p>
<hr id='hr1'></hr>
<br><br>
<form method=POST action=./php_calls_pdfimages.php>
<input title='Path' name='inpath' style='display:inline-block;width:50%;' type='text' id='path' value='' placeholder='Optional input path'><br><br>
<input type='text' data-onblur=\"if (document.URL.toLowerCase().indexOf('rjmprogramming.com.au') != -1) { this.value=''; } else { assessit(this.value); }\" style='width:90%;' title='Use + (perhaps just by itself) rather than comma to define a list that results in one output file for concatenated or merged media.  Use - to flatten out an Animated GIF or video or PDF.' placeholder='Input PDF file' id='thewords' name='convertthis' value=''><br><br>
<input type=hidden name=sw id=sw value=0></input><input type=hidden name=\"delp\" id=\"delp\" value=\"\"></input><input title='Output prefix' name='outprefix' style='display:inline-block;width:50%;' type='text' id='outpath' value='' placeholder='Optional output *.jpg file prefix [ideas] ... fully capitalize to retain'>&nbsp;<input style=display:inline-block; type=checkbox checked name=dovideo id=dovideo>Video</input>&nbsp;<input style=display:inline-block; type=checkbox checked name=doag id=doag>Animated GIF</input>&nbsp;<input type=checkbox style=display:inline-block; name=dopdf id=dopdf>Image PDF</input><br><br>
<input onclick=\"this.style.cursor='progress';\" type=submit value='Extract Images (and maybe Create Video and/or Animated GIF and/or Image PDF) From ...' style='background-color:yellow;'></input>
</form>
<div id='dpf' style='display:none;'></div>
<div id='dimap'></div>
<div title=' ' id=justputpdfpath style=display:none;></div>
<div title='' id=justputpdffilename style=display:none;></div>
<div title='' id=justputpdffilesize style=display:none;></div>
<iframe style='display:none;' id='gifif' name='gifif' src='//www.rjmprogramming.com.au/About_Us.html'></iframe></div-->";
$restto="<br><br><br><br><br><br><br><br><br><br><table id='thetable'><tbody><tr><td><iframe onload='iifopen(this);' id='cbi' frameborder='0' style='width:173px;height:228px;margin-top:-184px;' src='/HTMLCSS/client_browsing.htm?d=320562686'></iframe></td><td id='lstd'></td></tr></tbody></table>
<p id='myp'> ... or ...</p>
<hr id='hr1'></hr>
<br><br>
<form method=POST action=./php_calls_pdfimages.php>
<input title='Path' name='inpath' style='display:inline-block;width:50%;' type='text' id='path' value='' placeholder='Optional input path'><br><br>
<input type='text' data-onblur=\"if (document.URL.toLowerCase().indexOf('rjmprogramming.com.au') != -1) { this.value=''; } else { assessit(this.value); }\" style='width:90%;' title='Use + (perhaps just by itself) rather than comma to define a list that results in one output file for concatenated or merged media.  Use - to flatten out an Animated GIF or video or PDF.' placeholder='Input PDF file' id='thewords' name='convertthis' value=''><br><br>
<input type=hidden name=sw id=sw value=0></input><input type=hidden name=\"delp\" id=\"delp\" value=\"\"></input><input title='Output prefix' name='outprefix' style='display:inline-block;width:50%;' type='text' id='outpath' value='' placeholder='Optional output *.jpg file prefix [ideas] ... fully capitalize to retain'>&nbsp;<input style=display:inline-block; type=checkbox checked name=dovideo id=dovideo>Video</input>&nbsp;<input style=display:inline-block; type=checkbox checked name=doag id=doag>Animated GIF</input>&nbsp;<input type=checkbox style=display:inline-block; name=dopdf id=dopdf>Image PDF</input><br><br>
<input onclick=\"this.style.cursor='progress';\" type=submit value='Extract Images (and maybe Create Video and/or Animated GIF and/or Image PDF) From ...' style='background-color:yellow;'></input>
</form>
<div id='dpf' style='display:none;'></div>
<div id='dimap'></div>
<div title=' ' id=justputpdfpath style=display:none;></div>
<div title='' id=justputpdffilename style=display:none;></div>
<div title='' id=justputpdffilesize style=display:none;></div>
<iframe style='display:none;' id='gifif' name='gifif' src='//www.rjmprogramming.com.au/About_Us.html'></iframe></div>";

function rsearch($folder, $pattern, $size) { // thanks, anyway, to https://stackoverflow.com/questions/17160696/php-glob-scan-in-subfolders-for-a-file
    $didea='';
    if (PHP_OS == "Darwin") {
      //file_put_contents("x.ksh", "find " . $folder . " -type f -name \"" . $pattern . "\" 2> /dev/null -exec wc -c {} + | egrep '^ " . $size . " ' | sed '/ " . $size . " /s///g'");
      if ($folder == DIRECTORY_SEPARATOR) {
        $didea=shell_exec("find \$HOME/Downloads -type f -name \"" . $pattern . "\" 2> /dev/null -exec wc -c {} + | egrep '^ " . $size . " ' | sed '/ " . $size . " /s///g'");
      //file_put_contents("xx.ksh", $didea);
        if ($didea != '') { return $didea; }
        $didea=shell_exec("find " . dirname(__FILE__) . DIRECTORY_SEPARATOR  . " -type f -name \"" . $pattern . "\" 2> /dev/null -exec wc -c {} + | egrep '^ " . $size . " ' | sed '/ " . $size . " /s///g'");
      //file_put_contents("xxx.ksh", $didea);
        if ($didea != '') { return $didea; }
      }
      return shell_exec("find " . $folder . " -type f -name \"" . $pattern . "\" 2> /dev/null -exec wc -c {} + | egrep '^ " . $size . " ' | sed '/ " . $size . " /s///g'");
    }
    //$iti = new RecursiveDirectoryIterator($folder);
    //foreach (new RecursiveIteratorIterator($iti) as $file) {
    //foreach (glob($folder . $pattern) as $file) {
    //     if (strpos($file , $pattern) !== false && filesize($file) == $size) {
    //        return $file;
    //     }
    //}
    return '';
}

if (isset($_GET['filename']) && isset($_GET['filesize']) && !isset($_GET['filepath'])) {
  if (file_exists(str_replace('+',' ',urldecode($_GET['filename'])))) {
    if (filesize(str_replace('+',' ',urldecode($_GET['filename']))) == $_GET['filesize']) {
      $filepath=dirname(__FILE__) . DIRECTORY_SEPARATOR . str_replace('+',' ',urldecode($_GET['filename']));
    }
  }
  if ($filepath == '') {
    $filepath = rsearch(DIRECTORY_SEPARATOR, str_replace('+',' ',urldecode($_GET['filename'])), $_GET['filesize']);
  }
  echo "<html><body onload=\"if (parent.document.getElementById('path').value == '') { parent.document.getElementById('path').value='" . explode(str_replace('+',' ',urldecode($_GET['filename'])), $filepath)[0] . "'; }\"></body></html>";
  exit;
} else if (isset($_GET['filename']) && isset($_GET['filesize']) && $_GET['filepath'] == '') {
  if (file_exists(str_replace('+',' ',urldecode($_GET['filename'])))) {
    if (filesize(str_replace('+',' ',urldecode($_GET['filename']))) == $_GET['filesize']) {
      $filepath=dirname(__FILE__) . DIRECTORY_SEPARATOR . str_replace('+',' ',urldecode($_GET['filename']));
    }
  }
  if ($filepath == '') {
    $filepath = rsearch(DIRECTORY_SEPARATOR, str_replace('+',' ',urldecode($_GET['filename'])), $_GET['filesize']);
  }
  echo "<html><body onload=\"if (parent.document.getElementById('path').value == '') { parent.document.getElementById('path').value='" . explode(str_replace('+',' ',urldecode($_GET['filename'])), $filepath)[0] . "'; }\"></body></html>";
  exit;
} else if (isset($_GET['filename']) && isset($_GET['filesize']) && isset($_GET['filepath'])) {
  $anyextrac='';
  if (substr(str_replace('+',' ',urldecode($_GET['filepath'])), -1, 1) != DIRECTORY_SEPARATOR) {
    $anyextrac=DIRECTORY_SEPARATOR;
  }
  echo "<html><body onload=\"if (parent.document.getElementById('path').value == '') { parent.document.getElementById('path').value='" . str_replace('+',' ',urldecode($_GET['filepath'])) . $anyextac . "'; }\"></body></html>";
  exit;
}

$outp='ideas';
if (isset($_GET['outprefix'])) {
  $outp=str_replace('+',' ',urldecode($_GET['outprefix']));
  if (trim($outp) == "") { $outp='ideas'; }
} else if (isset($_POST['outprefix'])) {
  $outp=str_replace('+',' ',urldecode($_POST['outprefix']));
  if (trim($outp) == "") { $outp='ideas'; }
}

$origoutp=$outp;

$hashbit="";
$olhashbit="";

    if ($outp != strtoupper($outp)) { 
    if ($doag && $dovideo) {
      $hashbit=" + '#" . strtoupper($outp) . ".mp4#" . strtoupper($outp) . ".gif'";
    } else if ($dovideo) {
      $hashbit=" + '#" . strtoupper($outp) . ".mp4'";
    } else if ($doag) {
      $hashbit=" + '#" . strtoupper($outp) . ".gif'";
    }
    }
    
 if (!$dopdf && $hashbit != '' && $outp != strtoupper($outp)) {
   $olhashbit='<iframe style=display:none; src=./php_calls_pdfimages.php onload="delit(' . "''" . str_replace($outp, strtoupper($outp), $hashbit) . ');"></iframe>';
 } else if ($dopdf && $hashbit != '' && $outp != strtoupper($outp)) {
   $olhashbit='<iframe style=display:none; src=./php_calls_pdfimages.php onload="delit(' . "'" . strtoupper(${outp}) . ".pdf'" . $hashbit . ');"></iframe>';
 } else if ($dopdf && $hashbit == '' && $outp != strtoupper($outp)) {
   $olhashbit='<iframe style=display:none; src=./php_calls_pdfimages.php onload="delit(' . "'" . strtoupper(${outp}) . ".pdf'" . $hashbit . ');"></iframe>';
 }
 
 $outp=strtoupper($outp);

$proposedtable="<html>
<head>
<style>
 td { vertical-align: top; }
 .irandom { border:2px solid transparent; vertical-align: top; }
 span { background-color: #fafafa; }
 #thetable {  background-color:#f6f6f6; }
</style>
<scr" . "ipt type=text/javascript>
   var dmode='ltor';
   var lastpath='', lastfilename='';
 
   function woit(ioo) {
     var wo=null;
     if (ioo.src.indexOf('data:') == 0) {
      wo=window.open('', '_blank');
      wo.document.write('<img title=\"' + ioo.title + '\" src=\"' + ioo.src + '\"></img>');
      wo.document.title=ioo.title;
     } else {
      window.open(ioo.src, '_blank');
     }
   }

  function checkf() {
    if (document.getElementById('justputpdffilename')) {
    if (document.getElementById('justputpdffilename').innerHTML != '') {
      document.getElementById('justputpdffilename').title=document.getElementById('justputpdffilename').innerHTML;
      document.getElementById('justputpdffilename').innerHTML='';
      if (document.getElementById('thewords').value.trim() == '' || document.getElementById('thewords').value == lastfilename) {
        document.getElementById('thewords').value=document.getElementById('justputpdffilename').title;
        lastfilename=document.getElementById('justputpdffilename').title;
      }
      if (document.getElementById('justputpdfpath').innerHTML == '' && document.getElementById('justputpdfpath').title == '') {
        if (document.getElementById('justputpdffilesize').innerHTML != '') {
          //alert('am going away to look for filename=' + lastfilename + ' of size ' + document.getElementById('justputpdffilesize').innerHTML);
          document.getElementById('justputpdfpath').title=' ';
          //var xx=prompt(document.URL.split('?')[0].split('#')[0] + '?filepath=&filesize=' + document.getElementById('justputpdffilesize').innerHTML + '&filename=' + encodeURIComponent(lastfilename), document.URL.split('?')[0].split('#')[0] + '?filepath=&filesize=' + document.getElementById('justputpdffilesize').innerHTML + '&filename=' + encodeURIComponent(lastfilename));
          document.getElementById('path').style.cursor='progress';
          document.getElementById('gifif').src='./php_calls_pdfimages.php?filepath=&filesize=' + document.getElementById('justputpdffilesize').innerHTML + '&filename=' + encodeURIComponent(lastfilename);
        }
      }
    }
    if (document.getElementById('justputpdfpath').innerHTML != '') {
      document.getElementById('justputpdfpath').title=document.getElementById('justputpdfpath').innerHTML;
      document.getElementById('justputpdfpath').innerHTML='';
      if (document.getElementById('justputpdfpath').title.indexOf('/') != -1) {
        if (document.getElementById('justputpdfpath').title.slice(-1) != '/') {
          document.getElementById('justputpdfpath').title+='/';
        }
      } else {
        if (document.getElementById('justputpdfpath').title.slice(-1) != String.fromCharCode(92)) {
          document.getElementById('justputpdfpath').title+=String.fromCharCode(92);
        }
      }
      if (document.getElementById('path').value.trim() == '' || document.getElementById('path').value == lastpath) {
        document.getElementById('path').value=document.getElementById('justputpdfpath').title;
        lastpath=document.getElementById('justputpdfpath').title;
      }
    }
    if (document.getElementById('path').value != '') {
      document.getElementById('path').style.cursor='pointer';
    }
    }
  }


function iifopen(iois) {
  if (iois != null) {
    var aconto = (iois.contentWindow || iois.contentDocument);
    if (aconto.document) { aconto = aconto.document; }
    if (aconto != null) {
    //alert(1);
      if (aconto.getElementById('distyle')) {
        aconto.getElementById('distyle').innerHTML+='<style> input[type=file]::file-selector-button { background-color: pink; } </style>';
        aconto.getElementById('files').accept='.pdf';
        aconto.getElementById('files').multiple=false;
        aconto.getElementById('files').style.border='5px solid yellow';
        arect=aconto.getElementById('files').getBoundingClientRect();
        //alert(2);
        //aconto.getElementById('files').click();
      }
    }
  }
}
  

   function toggle() {
     document.getElementById('myh4').scrollIntoView(); // location.href='#myh4';
     if (dmode == 'ltor') {
       dmode='ttob';
       document.getElementById('smode').innerHTML='&#11015;';
       document.getElementById('mydflex').style.display='none';
       document.getElementById('mytable').style.display='none';
       document.getElementById('mytabletwo').style.display='table';
     } else if (dmode == 'ttob') {
       dmode='fill';
       document.getElementById('smode').innerHTML='&#10145;';
       document.getElementById('mydflex').style.display='none';
       document.getElementById('mytabletwo').style.display='none';
       document.getElementById('mytable').style.display='table';
     } else {
       dmode='ltor';
       document.getElementById('smode').innerHTML='&#8600;';
       document.getElementById('mytable').style.display='none';
       document.getElementById('mytabletwo').style.display='none';
       document.getElementById('mydflex').style.display='block';
     }     
   }

function delit(ioissrc) {
  var doit=false;
  var thingos=ioissrc.split('#');
  var zhr = new XMLHttpRequest();
  var zform=new FormData();
  if (thingos[0].length > 0) {
    doit=true;
    zform.append('delp', ioissrc.split('#')[0]);
  }
  if (thingos.length > 1) {
    if (thingos[1].indexOf('.mp4') != -1) {
    doit=true;
  zform.append('delv', thingos[1]);
    } else if (thingos[1].indexOf('.gif') != -1) {
    doit=true;
  zform.append('dela', thingos[1]);
    }
  }
  if (thingos.length > 2) {
    if (thingos[2].indexOf('.mp4') != -1) {
    doit=true;
  zform.append('delv', thingos[2]);
    } else if (thingos[2].indexOf('.gif') != -1) {
    doit=true;
  zform.append('dela', thingos[2]);
    }
  }
  if (doit) {
  zhr.open('post', document.URL.split('?')[0].split('#')[0], true);
  zhr.send(zform);
  }
}
  

caches.open('userSettings').then( (cache) => { 
  cache.keys().then( (arrayOfRequest) => { 
      console.log(arrayOfRequest); // [Request,  Request]
  });
});  

  setInterval(checkf, 1000);
</scr" . "ipt>
</head>
<body onload=\" document.getElementById('sw').value='' + screen.width;\">
<h1 style=margin-left:5px; id=myh1>Extract the Images Within a PDF <span style=cursor:pointer; onclick=\"location.href='#bothr';\">File" . $fextra . "</span> via <span title='Install pdfimages if not installed before via double click' style=cursor:pointer; ondblclick=\"window.open('./php_calls_pdfimages.php?install=pdfimages','_blank','top=200,left=200,width=700,height=400');\">Pdfimages</span> then <span title='Install ffmpeg if not installed before via double click' style=cursor:pointer; ondblclick=\"window.open('./php_calls_pdfimages.php?install=ffmpeg','_blank','top=200,left=200,width=700,height=400');\">Ffmpeg</span> and/or <span title='Install ImageMagick if not installed before via double click' style=cursor:pointer; ondblclick=\"window.open('./php_calls_pdfimages.php?install=imagemagick','_blank','top=200,left=200,width=700,height=400');\">ImageMagick</span></h1>
<h3 style=margin-left:5px; id=myh3x>RJM Programming <span id=smode style=cursor:pointer; onclick=toggle(); title='Click to toggle among left to right and top to bottom and width filling modes of image display'>&#10145;</span> April, 2023</h3>
<br><br>
<div id=dinstall></div>
<h4 id=myh4 onclick=\"document.getElementById('myh1').scrollIntoView();\" style=cursor:pointer; title='Back to top'></h4><br>
<table id=mytable><tbody id=mytbody><tr id=mytr></tr></tbody></table>
<table id=mytabletwo style=display:none;><tbody id=mythead><tr id=mytrtwo></TR></thead></table>
<div id=mydflex class='flexContainer' style=display:none;float:top;width:" . $vsw . ";></DIV>
<br><br>
<hr id=bothr title='Click to go back to top' onclick=\"location.href='#myh1x';\"></hr>" . $olhashbit . "
<iframe src=./php_calls_pdfimages.php frameborder=0 style='width:100%;height:1200px;'></iframe>
<!--table id='thetable'><tbody><tr><td><iframe onload='iifopen(this);' id='cbi' frameborder='0' style='width:173px;height:228px;margin-top:-184px;' src='/HTMLCSS/client_browsing.htm?d=320562686'></iframe></td><td id='lstd'></td></tr></tbody></table>
<p id='myp'> ... or ...</p>
<hr id='hr1'></hr>
<br><br><form style=margin-left:5px; method=POST action=./php_calls_pdfimages.php>
<input title='Path' name='inpath' style='display:inline-block;width:50%;' type='text' id='path' value='' placeholder='Optional input path'><br><br>
<input type='text' data-onblur=\"if (document.URL.toLowerCase().indexOf('rjmprogramming.com.au') != -1) { this.value=''; } else { assessit(this.value); }\" style='width:90%;' title='Use + (perhaps just by itself) rather than comma to define a list that results in one output file for concatenated or merged media.  Use - to flatten out an Animated GIF or video or PDF.' placeholder='Input PDF file' id='thewords' name='convertthis' value=''><br><br>
<input type=hidden name=sw id=sw value=0></input><input type=hidden name=\"delp\" id=\"delp\" value=\"\"></input><input title='Output prefix' name='outprefix' style='display:inline-block;width:50%;' type='text' id='outpath' value='' placeholder='Optional output *.jpg file prefix [ideas] ... fully capitalize to retain'>&nbsp;<input style=display:inline-block; type=checkbox checked name=dovideo id=dovideo>Video</input>&nbsp;<input style=display:inline-block; type=checkbox checked name=doag id=doag>Animated GIF</input>&nbsp;<input type=checkbox style=display:inline-block; name=dopdf id=dopdf>Image PDF</input><br><br>
<input onclick=\"this.style.cursor='progress';\" type=submit value='Extract Images (and maybe Create Video and/or Animated GIF and/or Image PDF) From ...' style='background-color:yellow;'></input>
</form>
<div id='dpf' style='display:none;'></div>
<div id='dimap'></div>
<div title=' ' id=justputpdfpath style=display:none;></div>
<div title='' id=justputpdffilename style=display:none;></div>
<div title='' id=justputpdffilesize style=display:none;></div>
<iframe style='display:none;' id='gifif' name='gifif' src='//www.rjmprogramming.com.au/About_Us.html'></iframe></div-->
</body>
</html>";

if (isset($_GET['convertthis'])) {
  $infile='"' . $pathp . str_replace('+',' ',urldecode($_GET['convertthis'])) . '"';
  //$fextra='<font size=1>&nbsp;' . basename(str_replace('"','',$infile)) . '</font>';
  $fextra='<font size="1">&nbsp;<a target="_blank" title="Original PDF" href="file://' . urlencode(str_replace('"','',$infile)) . '">' . basename(str_replace('"','',$infile)) . '</a></font>';
} else if (isset($_POST['convertthis'])) {
  $infile='"' . $pathp . str_replace('+',' ',urldecode($_POST['convertthis'])) . '"';
  //$fextra='<font size=1>&nbsp;' . basename(str_replace('"','',$infile)) . '</font>';
  $fextra='<font size="1">&nbsp;<a target="_blank" title="Original PDF" href="file://' . urlencode(str_replace('"','',$infile)) . '">' . basename(str_replace('"','',$infile)) . '</a></font>';
} else if (strpos(('' . $_SERVER['SERVER_NAME']), "localhost") === false) {
  echo "<html>
<body onload=\" if (('' + window.navigator.platform).toLowerCase().indexOf('mac') == 0) { window.open('HTTP://localhost:8888/php_calls_pdfimages.php','_blank','top=50,left=50,width=600,height=600'); } else {  window.open('HTTP://localhost/php_calls_pdfimages.php','_blank','top=50,left=50,width=600,height=600');  }\">" . str_replace(explode('>', explode('</body>', explode("<body", str_replace($rest, $restto, $proposedtable))[1])[0])[0] . '>', '', explode('</body>', explode("<body", str_replace($rest, $restto, $proposedtable))[1])[0]) . "

</body>
</html>";


echo str_replace($rest, $restto, $proposedtable);

  //exit;
} else {
  echo "<html>
<head>
<style>

 span { background-color: #fafafa; }
 select { width: 180px; }
 h3 { vertical-align: top; }
 
 #thetable {  background-color:#f6f6f6; }
 .irandom { border:2px solid transparent; vertical-align: top; }

 .custom-alert {
  display: inline-block;
  /* visibility: visible; */
  background-color: rgba(102,102,102,0.8);
  color: #fff;
  text-align: center;
  margin: 5% auto;
  padding: 12px 28px;
  opacity: 0.9;
  z-index: 200;
  -webkit-animation: glow 1s linear infinite alternate;
  -moz-animation: glow 1s linear infinite alternate;
  animation: glow 1s linear infinite alternate;
 }

 .custom-prompt {
  display: inline-block;
  /* visibility: visible; */
  background-color: rgba(202,202,202,0.8);
  color: #fff;
  text-align: center;
  margin: 5% auto;
  padding: 12px 28px;
  opacity: 0.9;
  z-index: 200;
  -webkit-animation: wglow 1s linear infinite alternate;
  -moz-animation: wglow 1s linear infinite alternate;
  animation: wglow 1s linear infinite alternate;
 }
 
 body { 
   margin: 0 0 0 0; 
   padding: 0 0 0 0; 
 }
 
 .dblob {
   text-align: center;
   vertical-align: middle;
 }

 /* Thanks to https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_glowing_text */

 @-webkit-keyframes glow {
  from {
    box-shadow: 0 0 3px #fff, 0 0 5px #fff, 0 0 37px #e60073, 0 0 9px #e60073, 0 0 11px #e60073, 0 0 13px #e60073, 0 0 15px #e60073;
  }

  to {
    box-shadow: 0 0 24px #fff, 0 0 6px #ff4da6, 0 0 8px #ff4da6, 0 0 10px #ff4da6, 0 0 12px #ff4da6, 0 0 14px #ff4da6, 0 0 16px #ff4da6;
  }
 }


 @-webkit-keyframes wglow {
  from {
    box-shadow: 0 0 3px #fff, 0 0 5px #fff, 0 0 37px #7300e6, 0 0 9px #7300e6, 0 0 11px #7300e6, 0 0 13px #7300e6, 0 0 15px #7300e6;
  }

  to {
    box-shadow: 0 0 24px #fff, 0 0 6px #ff4da6, 0 0 8px #a64dff, 0 0 10px #a64dff, 0 0 12px #a64dff, 0 0 14px #a64dff, 0 0 16px #a64dff;
  }
 }

</style>
<scr" . "ipt type=text/javascript>
  var lastpath='', lastfilename='';

  function checkf() {
    if (document.getElementById('justputpdffilename').innerHTML != '') {
      document.getElementById('justputpdffilename').title=document.getElementById('justputpdffilename').innerHTML;
      document.getElementById('justputpdffilename').innerHTML='';
      if (document.getElementById('thewords').value.trim() == '' || document.getElementById('thewords').value == lastfilename) {
        document.getElementById('thewords').value=document.getElementById('justputpdffilename').title;
        lastfilename=document.getElementById('justputpdffilename').title;
      }
      if (document.getElementById('justputpdfpath').innerHTML == '' && document.getElementById('justputpdfpath').title == '') {
        if (document.getElementById('justputpdffilesize').innerHTML != '') {
          //alert('am going away to look for filename=' + lastfilename + ' of size ' + document.getElementById('justputpdffilesize').innerHTML);
          document.getElementById('justputpdfpath').title=' ';
          //var xx=prompt(document.URL.split('?')[0].split('#')[0] + '?filepath=&filesize=' + document.getElementById('justputpdffilesize').innerHTML + '&filename=' + encodeURIComponent(lastfilename), document.URL.split('?')[0].split('#')[0] + '?filepath=&filesize=' + document.getElementById('justputpdffilesize').innerHTML + '&filename=' + encodeURIComponent(lastfilename));
          document.getElementById('path').style.cursor='progress';
          document.getElementById('gifif').src='./php_calls_pdfimages.php?filepath=&filesize=' + document.getElementById('justputpdffilesize').innerHTML + '&filename=' + encodeURIComponent(lastfilename);
        }
      }
    }
    if (document.getElementById('justputpdfpath').innerHTML != '') {
      document.getElementById('justputpdfpath').title=document.getElementById('justputpdfpath').innerHTML;
      document.getElementById('justputpdfpath').innerHTML='';
      if (document.getElementById('justputpdfpath').title.indexOf('/') != -1) {
        if (document.getElementById('justputpdfpath').title.slice(-1) != '/') {
          document.getElementById('justputpdfpath').title+='/';
        }
      } else {
        if (document.getElementById('justputpdfpath').title.slice(-1) != String.fromCharCode(92)) {
          document.getElementById('justputpdfpath').title+=String.fromCharCode(92);
        }
      }
      if (document.getElementById('path').value.trim() == '' || document.getElementById('path').value == lastpath) {
        document.getElementById('path').value=document.getElementById('justputpdfpath').title;
        lastpath=document.getElementById('justputpdfpath').title;
      }
    }
    if (document.getElementById('path').value != '') {
      document.getElementById('path').style.cursor='pointer';
    }
  }


function iifopen(iois) {
  if (iois != null) {
    var aconto = (iois.contentWindow || iois.contentDocument);
    if (aconto.document) { aconto = aconto.document; }
    if (aconto != null) {
    //alert(1);
      if (aconto.getElementById('distyle')) {
        aconto.getElementById('distyle').innerHTML+='<style> input[type=file]::file-selector-button { background-color: pink; } </style>';
        aconto.getElementById('files').accept='.pdf';
        aconto.getElementById('files').multiple=false;
        aconto.getElementById('files').style.border='5px solid yellow';
        arect=aconto.getElementById('files').getBoundingClientRect();
        //alert(2);
        //aconto.getElementById('files').click();
      }
    }
  }
}


function delit(ioissrc) {
  var doit=false;
  var thingos=ioissrc.split('#');
  var zhr = new XMLHttpRequest();
  var zform=new FormData();
  if (thingos[0].length > 0) {
    doit=true;
    zform.append('delp', ioissrc.split('#')[0]);
  }
  if (thingos.length > 1) {
    if (thingos[1].indexOf('.mp4') != -1) {
    doit=true;
  zform.append('delv', thingos[1]);
    } else if (thingos[1].indexOf('.gif') != -1) {
    doit=true;
  zform.append('dela', thingos[1]);
    }
  }
  if (thingos.length > 2) {
    if (thingos[2].indexOf('.mp4') != -1) {
    doit=true;
  zform.append('delv', thingos[2]);
    } else if (thingos[2].indexOf('.gif') != -1) {
    doit=true;
  zform.append('dela', thingos[2]);
    }
  }
  if (doit) {
  zhr.open('post', document.URL.split('?')[0].split('#')[0], true);
  zhr.send(zform);
  }
}

caches.open('userSettings').then( (cache) => { 
  cache.keys().then( (arrayOfRequest) => { 
      console.log(arrayOfRequest); // [Request,  Request]
  });
});  

  setInterval(checkf, 1000);
</scr" . "ipt>
</head>
<body onload=\" document.getElementById('sw').value='' + screen.width;\">
<table id='thetable'><tbody><tr><td><iframe onload='iifopen(this);' id='cbi' frameborder='0' style='width:173px;height:228px;margin-top:-184px;' src='/HTMLCSS/client_browsing.htm?d=320562686'></iframe></td><td id='lstd'></td></tr></tbody></table>
<p id='myp'> ... or ...</p>
<hr id='hr1'></hr>
<br><br>
<h1 style=margin-left:5px; id=myh1>Extract the Images Within a PDF File" . $fextra . " via <span title='Install pdfimages if not installed before via double click' style=cursor:pointer; ondblclick=\"window.open('./php_calls_pdfimages.php?install=pdfimages','_blank','top=200,left=200,width=700,height=400');\">Pdfimages</span> then <span title='Install ffmpeg if not installed before via double click' style=cursor:pointer; ondblclick=\"window.open('./php_calls_pdfimages.php?install=ffmpeg','_blank','top=200,left=200,width=700,height=400');\">Ffmpeg</span> and/or <span title='Install ImageMagick if not installed before via double click' style=cursor:pointer; ondblclick=\"window.open('./php_calls_pdfimages.php?install=imagemagick','_blank','top=200,left=200,width=700,height=400');\">ImageMagick</span></h1>
<h3 style=margin-left:5px; id=myh3>RJM Programming - April, 2023</h3>
<form style=margin-left:5px; method=POST action=./php_calls_pdfimages.php>
<input title='Path' name='inpath' style='display:inline-block;width:50%;' type='text' id='path' value='' placeholder='Optional input path'><br><br>
<input type='text' data-onblur=\"if (document.URL.toLowerCase().indexOf('rjmprogramming.com.au') != -1) { this.value=''; } else { assessit(this.value); }\" style='width:90%;' title='Use + (perhaps just by itself) rather than comma to define a list that results in one output file for concatenated or merged media.  Use - to flatten out an Animated GIF or video or PDF.' placeholder='Input PDF file' id='thewords' name='convertthis' value=''><br><br>
<input title='Output prefix' name='outprefix' style='display:inline-block;width:50%;' type='text' id='outpath' value='' placeholder='Optional output *.jpg file prefix [ideas] ... fully capitalize to retain'>&nbsp;<input style=display:inline-block; type=checkbox checked name=dovideo id=dovideo>Video</input>&nbsp;<input style=display:inline-block; type=checkbox checked name=doag id=doag>Animated GIF</input>&nbsp;<input type=checkbox style=display:inline-block; name=dopdf id=dopdf>Image PDF</input><br><br>
<input type=hidden name=sw id=sw value=0></input><input type=hidden name=\"delp\" id=\"delp\" value=\"\"></input><input onclick=\"this.style.cursor='progress';\" type=submit value='Extract Images (and maybe Create Video and/or Animated GIF and/or Image PDF) From ...' style='background-color:yellow;'></input>
</form>
<div id='predpf' style='display:none;'><form target='gifif' style='display:none;' method='POST' id='myfgifif' action='/pdfimageplustext.php'>
<input type='hidden' name='agslide' id='agslides' value=''>
<input style='display:none;' type='submit' id='subag' value='Animated GIF Processing'></form>
</div>
<div id='dpf' style='display:none;'></div>
<div id='dimap'></div>
<div title=' ' id=justputpdfpath style=display:none;></div>
<div title='' id=justputpdffilename style=display:none;></div>
<div title='' id=justputpdffilesize style=display:none;></div>
<iframe style='display:none;' id='gifif' name='gifif' src='//www.rjmprogramming.com.au/About_Us.html'></iframe></div></body>
</html>";
  exit;
}

if (PHP_OS == "Darwin") {
$huhw=shell_exec("where pdfimages");
if (strpos($huhw, DIRECTORY_SEPARATOR) !== false) {
  $proposedtable=str_replace('></h4>', '>' . $huhw . ' -j ' . $infile . ' ' . $outp . ' -png</h4>', $proposedtable);
  foreach (glob($outp . '-*') as $filename) {
    unlink($filename);
  }
  exec($huhw . ' -j ' . $infile . ' ' . $outp . ' -png 2> pdfimages.bad');
} else {
  if (1 == 1) {
  putenv('PATH=/usr/local/bin');
  $proposedtable=str_replace('></h4>', '>pdfimages -j ' . $infile . ' ' . $outp . ' -png</h4>', $proposedtable);
  foreach (glob($outp . '-*') as $filename) {
    unlink($filename);
  }
  exec('pdfimages -j ' . $infile . ' ' . $outp . ' -png 2> pdfimages.bad');
  } else {
  $proposedtable=str_replace('></h4>', '>/usr/local/bin/pdfimages -j ' . $infile . ' ' . $outp . ' -png</h4>', $proposedtable);
  foreach (glob($outp . '-*') as $filename) {
    unlink($filename);
  }
  exec('/usr/local/bin/pdfimages -j ' . $infile . ' ' . $outp . ' -png 2> pdfimages.bad');
  }
}
} else if (PHP_OS =='WINNT' || PHP_OS =='WIN32' || PHP_OS =='Windows') {
$proposedtable=str_replace('></h4>', '>pdfimages -j ' . $infile . ' ' . $outp . ' -png</h4>', $proposedtable);
  foreach (glob($outp . '-*') as $filename) {
    unlink($filename);
  }
exec('pdfimages -j ' . $infile . ' ' . $outp . ' -png 2> pdfimages.bad');
}

$icnt=0;
$sofarw=0;
$padbr='';
$rens=",";

    if ($dopdf) {
    putenv('PATH=/usr/local/bin');
    exec("convert " . $outp . "-*.*g -quality 100 " . $outp . ".pdf 2> pdfimage.bad");
    $src=$outp . '.pdf';
    if ($outp != strtoupper($outp)) { 
     $sparepdf=' onload=\"delit(this.src' . $hashbit . ');\" '; //' title="data:application/pdf;base64,' . base64_encode(file_get_contents($src)) . '" ';
     //unlink($src);
     //$src=$spare;
     //$proposedtable=str_replace('\"delp\" value=\"\"', '\"delp\" value=\"' . $outp . 'pdf\"', $proposedtable);
     //$rest=str_replace('\"delp\" value=\"\"', '\"delp\" value=\"' . $outp . 'pdf\"', $rest);
     //$restto=str_replace('\"delp\" value=\"\"', '\"delp\" value=\"' . $outp . 'pdf\"', $restto);
    }
    if ($fextra != '') {
      $proposedtable=str_replace($fextra, '', $proposedtable);
    }
    $proposedtable=str_replace('Extract the Images Within a PDF <', 'Images Of <details style=display:inline-block;vertical-align:top;width:35%;background-color:#fafafa; id=detpdf><summary id=spdf><font size=1>Image</font> PDF ' . $fextra . '</summary><iframe style=width:100%;height:' . ($vsw / 3) . 'px;' . $sparepdf . ' src="' . $src . '"></iframe></details> <', $proposedtable); 
    }


foreach (glob($outp . '-*') as $filename) {
  if (strpos($filename, ".jp") === false) {
    $rens.=explode('.', basename($filename))[0] . '.jpg,' . explode('.', basename($filename))[1] . ',';
    rename($filename, str_replace('.png', '.jpg', $filename));
  }
}


    if ($doag) {
    if (file_exists($outp . ".gif")) {
      unlink($outp . ".gif");
    }
    putenv('PATH=/usr/local/bin');
    exec("ffmpeg -f image2 -framerate 3 -i " . $outp . "-%03d.jpg -vf \"scale=w=${vw}:h=${vh}:force_original_aspect_ratio=decrease,pad=${vw}:${vh}:(ow-iw)/2:(oh-ih)/2\" -loop 0 " . $outp . ".gif 2> ag.bad");
    if (!file_exists($outp . ".gif")) {
      file_put_contents($outp . ".gif", ""); //$doag=false;
    }
    //exit;
    }


//exit;

foreach (glob($outp . '-*') as $filename) {
  if (strpos($rens, ',' . basename($filename) . ',') !== false) {
  $datau='data:image/' . explode(',',explode(',' . basename($filename) . ',', $rens)[1])[0] . ';base64,' . base64_encode(file_get_contents($filename));
  } else {
  $datau='data:image/' . str_replace("jpg","jpeg",strtolower(explode(".", $filename)[-1 + sizeof(explode(".", $filename))])) . ';base64,' . base64_encode(file_get_contents($filename));
  }
  $proposedtable=str_replace('</tr></tbody>', '<td><img onclick=woit(this); title="' . $filename . '" src="' . $datau . '"></img></td></tr></tbody>', $proposedtable);
  $proposedtable=str_replace('</TR></thead>', '<tr><td><img onclick=woit(this); title="' . $filename . '" src="' . $datau . '"></img></td></tr></TR></thead>', $proposedtable);
  list($width, $height, $type, $attr) = getimagesize($filename);
  $width=intval("$width");
  //file_put_contents("x.x", file_get_contents("x.x") . "sw=" . $vsw . " and original sofarw is " . $sofarw . ' and this width is ' . $width);
  $sofarw+=$width;
  $sofarw+=10;
  //file_put_contents("x.x", file_get_contents("x.x") . " then sofarw becomes " . $sofarw . ' and this width is ' . $width);
  if ("$vsw" != "0" && intval("$sofarw") >= intval("$vsw")) { $padbr="<br>"; $sofarw=$width; $sofarw+=10; } //else { $sofarw+=($width + 10); }
  //file_put_contents("x.x", file_get_contents("x.x") . " padbr length is " . strlen($padbr) . "\n");
  $proposedtable=str_replace('</DIV>', $padbr . '<img class=irandom onclick=woit(this); title="' . $filename . '" src="' . $datau . '"></img>&nbsp;</DIV>', $proposedtable);
  $padbr='';
  if (PHP_OS == "Darwin") {
  if ($icnt == 0 && ($dovideo || $doag || $dopdf)) {
    if (file_exists($outp . '.mp4')) {
      unlink($outp . '.mp4');
    }
    //if (file_exists($outp . '.gif')) {
    //  unlink($outp . '.gif');
    //}
    if ($dovideo) {
    $huhvs=['1', '2'];
    while (sizeof($huhvs) > 1) {
    if (file_exists('video.bad')) {
      unlink('video.bad');
    }
    putenv('PATH=/usr/local/bin');
    exec("ffmpeg -framerate 3 -i " . $outp . "-%03d.jpg -vf \"scale=w=${vw}:h=${vh}:force_original_aspect_ratio=decrease,pad=${vw}:${vh}:(ow-iw)/2:(oh-ih)/2\" " . $outp . ".mp4 2> video.bad"); // thanks to https://stackoverflow.com/questions/20847674/ffmpeg-libx264-height-not-divisible-by-2
    $huhvs=explode('Could not open file :', file_get_contents('video.bad'));
    //file_put_contents('xaaa.xaaa', '' . sizeof($huhvs) . file_get_contents('video.bad'));
    if (sizeof($huhvs) > 1) {
      //file_put_contents('xa.xa', trim(explode(Chr(10), explode('.j', $huhvs[1])[0])) . '.jpg');
      rename(trim(explode(Chr(10), $huhvs[1])[0]) . '', trim(explode('.', $huhvs[1])[0]) . '.jpeg');
      unlink('video.bad');
      if (file_exists($outp . '.mp4')) {
        unlink($outp . '.mp4');
      }
    //} else {
      //file_put_contents('xaa.xaa', '' . sizeof($huhvs) . file_get_contents('video.bad'));
    }
    }
    }
    //exec("ffmpeg -framerate 3 -i " . $outp . "-%03d.jpg  -filter:v \"setpts=5.0*PTS\" -vf \"scale=1280:-2\" " . $outp . ".mp4"); // thanks to https://stackoverflow.com/questions/20847674/ffmpeg-libx264-height-not-divisible-by-2
    //exec("ffmpeg -framerate 3 -i " . $outp . "-%03d.jpg  -filter:v \"setpts=5.0*PTS\" -vf \"scale=1280:-2,pad=ceil(iw/2)*2:ceil(ih/2)*2\" " . $outp . ".mp4"); // thanks to https://stackoverflow.com/questions/20847674/ffmpeg-libx264-height-not-divisible-by-2
    //exec("ffmpeg -framerate 3 -i " . $outp . "-%03d.jpg  -filter:v \"setpts=5.0*PTS\" -vf \"scale=640:-1\" " . $outp . ".mp4"); // thanks to https://stackoverflow.com/questions/20847674/ffmpeg-libx264-height-not-divisible-by-2
    if ($doag) {
    if (!file_exists($outp . ".gif")) {
    putenv('PATH=/usr/local/bin');
    exec("ffmpeg -f image2 -framerate 3 -i " . $outp . "-%03d.jpg -vf \"scale=w=${vw}:h=${vh}:force_original_aspect_ratio=decrease,pad=${vw}:${vh}:(ow-iw)/2:(oh-ih)/2\" -loop 0 " . $outp . ".gif 2> ag.bad");
    if (!file_exists($outp . ".gif")) {
      $doag=false;
    }
    }
    }
    if ($dopdf && 1 == 5) {
    putenv('PATH=/usr/local/bin');
    exec("convert " . $outp . "-*.jpg -quality 100 " . $outp . ".pdf 2> pdfimage.bad");
    $src=$outp . '.pdf';
    if ($outp != strtoupper($outp)) { 
     $sparepdf=' onload=delit(this.src' . $hashbit . '); '; //' title="data:application/pdf;base64,' . base64_encode(file_get_contents($src)) . '" ';
     //unlink($src);
     //$src=$spare;
     //$proposedtable=str_replace('\"delp\" value=\"\"', '\"delp\" value=\"' . $outp . 'pdf\"', $proposedtable);
     //$rest=str_replace('\"delp\" value=\"\"', '\"delp\" value=\"' . $outp . 'pdf\"', $rest);
     //$restto=str_replace('\"delp\" value=\"\"', '\"delp\" value=\"' . $outp . 'pdf\"', $restto);
    }
    $proposedtable=str_replace('Extract the Images Within a PDF <', 'Images Of <details style=display:inline-block;vertical-align:top;width:35%;background-color:#fafafa; id=detpdf><summary id=spdf><font size=1>Image</font> PDF</summary><iframe style=width:100%;height:' . ($vsw / 3) . 'px;' . $sparepdf . ' src="' . $src . '"></iframe></details> <', $proposedtable); 
    }
    if ($dovideo && $doag) {
    $src=$outp . '.mp4';
    $srctwo=$outp . '.gif';
    if ($outp != strtoupper($outp)) { 
     //file_put_contents('xxx5.xxx5', $src);
     $spare='data:video/mp4;base64,' . base64_encode(file_get_contents($src));
     //file_put_contents('xxx6.xxx6', '' . strlen($spare));
     unlink($src);
     $src=$spare;
     //file_put_contents('xxx7.xxx7', '' . strlen($spare));
     $spare='data:image/gif;base64,' . base64_encode(file_get_contents($srctwo));
     //file_put_contents('xxx8.xxx8', '' . strlen($spare));
     unlink($srctwo);
     //file_put_contents('xxx9.xxx9', '' . strlen($spare));
     $srctwo=$spare;
    }
    $proposedtable=str_replace('</h3>', '&nbsp;<br><details style=display:inline-block;vertical-align:top; id=vd open><summary id=svd>Video ...</summary><video type=video/mp4 controls><source src="' . $src . '" type=video/mp4></source></video></details>&nbsp;<details style=display:inline-block;vertical-align:top; id=ag open><summary id=sag>Animated GIF ...</summary><img src="' . $srctwo . '"></img></details></h3>', $proposedtable);
    $proposedtable=str_replace('</h4><br>', '</h4><br><details open><summary>Pdfimages images ...</summary>', $proposedtable);
    $proposedtable=str_replace('</DIV>', '</DIV></details>', $proposedtable);
    //file_put_contents('xxx0.xxx0', '');
    } else if ($doag) {
    $src=$outp . '.gif';
    if ($outp != strtoupper($outp)) { 
     //file_put_contents('xxx00.xxx00', '');
     $spare='data:image/gif;base64,' . base64_encode(file_get_contents($src));
     unlink($src);
     $src=$spare;
    }
    //file_put_contents('xxx0002.xxx0002', '');
    $proposedtable=str_replace('</h3>', '&nbsp;<br><details style=display:inline-block;vertical-align:top; id=ag open><summary id=sag>Animated GIF ...</summary><img src="' . $src . '"></img></details></h3>', $proposedtable);
    $proposedtable=str_replace('</h4><br>', '</h4><br><details open><summary>Pdfimages images ...</summary>', $proposedtable);
    $proposedtable=str_replace('</DIV>', '</DIV></details>', $proposedtable);
    //file_put_contents('xxx0003.xxx0003', '');
   } else if ($dovideo) {
    $src=$outp . '.mp4';
    if ($outp != strtoupper($outp)) { 
     $spare='data:video/mp4;base64,' . base64_encode(file_get_contents($src));
     unlink($src);
     $src=$spare;
    }
    $proposedtable=str_replace('</h3>', '&nbsp;<br><details style=display:inline-block;vertical-align:top; id=vd open><summary id=svd>Video ...</summary><video type=video/mp4 controls><source src="' . $src . '" type=video/mp4></source></video></details></h3>', $proposedtable);
    $proposedtable=str_replace('</h4><br>', '</h4><br><details open><summary>Pdfimages images ...</summary>', $proposedtable);
    $proposedtable=str_replace('</DIV>', '</DIV></details>', $proposedtable);
    }
  }
  }
  //file_put_contents('xxx000.xxx000', '');
  if ($outp != strtoupper($outp) && str_replace(',','',$rens) == '') { 
    //file_put_contents('xxx0000.xxx0000', $filename);
    try {
    if (!file_exists($filename)) {
    if (file_exists(str_replace('.png','.jpeg',$filename))) {
    unlink(str_replace('.png','.jpeg',$filename));
    }
    } else {
    unlink($filename);
    }   
    } catch (Exception $werty) { }
  }
  $icnt++;
}

  if ($outp != strtoupper($outp) && str_replace(',','',$rens) != '') { 
    //sleep(5);
    //file_put_contents('xxx0001.xxx0001', '');
foreach (glob($outp . '-*') as $filename) {
    try {
    unlink($filename);
    } catch (Exception $werty) { }
}
  }

//file_put_contents('xxx00014.xxx00014', '');
echo str_replace($rest, $restto, $proposedtable);
//if ($sparepdf != '') {
//   sleep(20);
//   unlink($outp . ".pdf");
//}

//if (isset($_POST['delp'])) {
//  if ($_POST['delp'] != '') {
//    if (file_exists(str_replace('+',' ', urldecode($_POST['delp'])))) {
//       unlink(str_replace('+',' ', urldecode($_POST['delp'])));
//    }
//  }
//}

exit;
?>
