<?php
// php_calls_pdfimages.php
// RJM Programming
// April, 2023


$infile='"input_pdf.pdf"';

$proposedtable="<html>
<body>
<h1>Extract the Images Within a PDF File via Pdfimages</h1>
<h3>RJM Programming - April, 2023</h3>
<br><br>
<h4></h4><br>
<table><tbody><tr></tr></tbody></table>
<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 title='Output prefix' name='outprefix' style='display:inline-block;width:50%;' type='text' id='outpath' value='' placeholder='Optional output *.jpg file prefix'><br><br>
<input type=submit value='Extract Images From ...'></input>
</form>
</body>
</html>";

$outp='ideas';
$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['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'; }
}

if (isset($_GET['convertthis'])) {
  $infile='"' . $pathp . str_replace('+',' ',urldecode($_GET['convertthis'])) . '"';
} else if (isset($_POST['convertthis'])) {
  $infile='"' . $pathp . str_replace('+',' ',urldecode($_POST['convertthis'])) . '"';
} else if (strpos(('' . $_SERVER['SERVER_NAME']), "localhost") === false) {
  echo "<html>
<body onload=\"window.open('HTTP://localhost:8888/php_calls_pdfimages.php','_blank','top=50,left=50,width=500,height=500');\"></body>
</html>";
  //exit;
} else {
  echo "<html>
<body>
<h1>Extract the Images Within a PDF File via Pdfimages</h1>
<h3>RJM Programming - April, 2023</h3>
<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(s)' 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'><br><br>
<input type=submit value='Extract Images From ...'></input>
</form>
</body>
</html>";
  exit;
}

if (PHP_OS == "Darwin") {
$proposedtable=str_replace('></h4>', '>/usr/local/bin/pdfimages -j ' . $infile . ' ' . $outp . ' -png</h4>', $proposedtable);
exec('/usr/local/bin/pdfimages -j ' . $infile . ' ' . $outp . ' -png');
} else {
$proposedtable=str_replace('></h4>', '>pdfimages -j ' . $infile . ' ' . $outp . ' -png</h4>', $proposedtable);
exec('pdfimages -j ' . $infile . ' ' . $outp . ' -png');
}
foreach (glob($outp . '-*') as $filename) {
  $datau='data:image/' . str_replace("jpg","jpeg",strtolower(explode(".", $filename)[-1 + sizeof(explode(".", $filename))])) . ';base64,' . base64_encode($filename);
  $proposedtable=str_replace('</tr>', '<td><img title="' . $filename . '" src="' . basename($filename) . '"></img></td></tr>', $proposedtable);
}

echo $proposedtable;
exit;
?>
