<?php
  // convert_wildcard.php
  // RJM Programming 
  // October, 2022
  // Facilitate the equivalent of ImageMagick command line ...
  // convert ./imspec*.*g* -density 56% ./*.jpeg
  
$erroris=" ";
$outeis=".jpeg";
$fspec="";
$ext="";
$width=0;
$height=0;
$iferr="";
$includesare="";
$cr="";
$crd="";
$dcr="";
$zipfile="";
$ziparr = array();
$ziparrc = array();
$ziparrwh = array();
$quality=-1;
$taberows="";
$chkbox="";
if (strpos(strtolower($_SERVER['SERVER_NAME']), 'rjmprogramming.com.au') === false) {
  $chkbox="<br><br>Keep Image Files <input type=checkbox id=delc title='Real web server files left after processing, perhaps lessening need to deploy download click functionalities.'></input>";
}
$inputask="<br><br><form onsubmit=\" if (document.getElementById('delc')) { if (!document.getElementById('delc').checked) {  document.getElementById('mysub').name='delfile'; } } return true;\" method=POST action=./convert_wildcard.php><input type=text style='width:80%;' value='' placeholder='Enter relative image file specification eg. f*.png' name=ispec></input><br><br><input type=text style='width:80%;' placeholder='Enter output relative image file extension [.jpeg]' value='' name=outext></input><br><br>Quality: <input type=number style='display:inline-block;' name=quality value=75 step=1 min=-1 max=100 name=quality title='Jpeg Quality Percentage'></input>%" . $chkbox . "<br><br><br><br><input id=mysub type=submit value='Convert Image Format(s)'></input></form>";

 if (isset($_GET['ispec']) && (isset($_GET['outext']) || $outeis != '')) {
    if (isset($_GET['quality'])) {  $quality=explode('.',str_replace('%','',str_replace('+',' ',urldecode($_GET['quality']))))[0];   }
    if (isset($_GET['outext'])) { if (strlen($_GET['outext']) > 0) { $outeis=str_replace('+',' ',urldecode($_GET['outext'])); } }
    ourglob(str_replace('+',' ',urldecode($_GET['ispec'])), $outeis);
 } else if (isset($_POST['ispec']) && (isset($_POST['outext']) || $outeis != '')) {
    if (isset($_POST['quality'])) {  $quality=explode('.',str_replace('%','',str_replace('+',' ',urldecode($_POST['quality']))))[0];   }
    if (isset($_POST['outext'])) {  if (strlen($_POST['outext']) > 0) { $outeis=str_replace('+',' ',urldecode($_POST['outext'])); } }
    ourglob(str_replace('+',' ',urldecode($_POST['ispec'])), $outeis);
 }

 function ourglob($ofwhat, $inext) {
   global $inputask, $erroris, $zipfile, $ziparrwh, $ziparrc, $ziparr, $fspec, $ext, $width, $height, $iferr, $includesare, $cr, $crd, $dcr, $quality, $tablerows;
   $image=null;
   $lastname="";
    //file_put_contents('xpp.xpp', $includesare);
   $concernyourself=0;
   if ($zipfile == "") {
     if ($erroris == "") {
      return glob($ofwhat);
     } else {
      foreach (glob($ofwhat) as $ourfilename) { 
        if (strpos($inputask, "<scr") === false) {
           $inputask.="<br><sc" . "ript type=text/javascript> function onl() {  document.getElementById('deta').innerHTML='+'; document.getElementById('myh1').innerHTML='<input type=button style=display:inline-block; onclick=\"cura=0; checkca();\" title=All value=Downloadable></input> Image Conversions'; } setTimeout(onl, 3000); </scr" . "ipt>";
        }
        $ext="." . explode(".", $ourfilename)[-1 + sizeof(explode(".", $ourfilename))];
        $new_file=str_replace($ext, strtolower(str_replace('..','.',"." . $inext)), $ourfilename);
        //echo $ofwhat . "<br>" . $ourfilename . "<br>" . $ext . "<br>" . $inext . "<br>" . $new_file;
        if (file_exists($new_file)) {
          $new_file=str_replace($ext, strtoupper(str_replace('..','.',"." . $inext)), $ourfilename);
        }
        if (file_exists($new_file)) {
          $new_file=str_replace($ext, strtolower(str_replace('..','.',"." . $inext)), $ourfilename);
          while (file_exists($new_file)) {
            $new_file=str_replace(strtolower(str_replace('..','.',"." . $inext)), "0" . strtolower(str_replace('..','.',"." . $inext)), $new_file);
          }
        }
        array_push($ziparr, $new_file); //$ourfilename);
        $im_php = imagecreatefromstring(file_get_contents($ourfilename));
        $width = imagesx($im_php);
        $height = imagesy($im_php);
        switch ($inext) {
    case '.jpg':
    case '.jpeg':
       $image = imagejpeg($im_php, $new_file, $quality);
    break;
    case '.gif':
       $image = imagegif($im_php, $new_file);
    break;
    case '.png':
       $image = imagepng($im_php, $new_file);
    break;
        }
        imagedestroy($im_php);
        //array_push($ziparrwh, '' . $width . ',' . $height);
        //array_push($ziparrc, file_get_contents($new_file));
        //if (!file_exists($new_file)) { echo "<br>oops RE " . $new_file . "<br>"; }
        $tablerows.="<tr><th><a class=ca title='Click to download " . basename($new_file) . "' target=_blank download=" . basename($new_file) . " href=" . 'data:image/' . str_replace("jpg","jpeg",strtolower(explode(".", $new_file)[-1 + sizeof(explode(".", $new_file))])) . ';base64,' . base64_encode(file_get_contents($new_file)) . ">" . basename($new_file) . "</a></th></tr><tr><td><a title='Click to download " . basename($new_file) . "' target=_blank download=" . basename($new_file) . " href=" . 'data:image/' . str_replace("jpg","jpeg",strtolower(explode(".", $new_file)[-1 + sizeof(explode(".", $new_file))])) . ';base64,' . base64_encode(file_get_contents($new_file)) . "><details open> \n <summary></summary>\n<img style='width:" . $width . "px;height:" . $height . "px;' title='Click to download " . basename($new_file) . "' src=" . 'data:image/' . str_replace("jpg","jpeg",strtolower(explode(".", $new_file)[-1 + sizeof(explode(".", $new_file))])) . ';base64,' . base64_encode(file_get_contents($new_file)) . "></img></a><br>\n</details></td></tr>";
        if (strpos(strtolower($_SERVER['SERVER_NAME']), 'rjmprogramming.com.au') === false) {
        imagedestroy($image);
        if (isset($_GET['delfile']) || isset($_POST['delfile'])) {
        unlink($new_file);
        }
        } else {
        unlink($new_file);
        }
      }
      $zipfile=" ";
      return $ziparr;
     }
   } else {
     $zip = zip_open($zipfile);
     if ($zip) {
      while ($zip_entry = zip_read($zip)) {
       $ourfilename=zip_entry_name($zip_entry);
       if (strpos($includesare, $ourfilename) !== false || $includesare == '') {
       $regexpok=0;
       if (strpos($ourfilename, "_MACOSX/") === false) {
       $regexp="/^[\S]" . str_replace('[\s\S]?','[\s\S]*',str_replace('*','[\s\S]?',str_replace('?',',',str_replace('%',',',str_replace('.','[.]',$ofwhat))))) . "$/";
       $regexpok=preg_match($regexp, explode("/",$ourfilename)[-1 + sizeof(explode("/",$ourfilename))]);
       if ($regexpok !== 0) {
        if ($ext == "") $ext="." . explode(".", $ourfilename)[-1 + sizeof(explode(".", $ourfilename))];
        if ($iferr != "" && strpos($iferr, "&include=&") !== false) {
          $iferr=str_replace("&include=&", "&include=" . urlencode($ourfilename) . "&", $iferr);
          $lastname=$ourfilename;
          $concernyourself++;
          $cr.=$crd . $ourfilename; // . " selected";
          if ($dcr != " ") { $dcr.=$crd . "<a " . $adatam . "onmouseover=\"butwhatif(this,'" . less($ourfilename) . "');\" onclick=\"butwhatif(this,'" . less($ourfilename) . "');\" target=myif href='" . $ourfilename . "'>" . basename($ourfilename) . "</a>"; }
          $crd="\n";
        } else if ($iferr != "" && $concernyourself == 1) {
          $iferr=str_replace("&include=" . urlencode($lastname) . "&", "&include=" . urlencode($lastname . "," . $ourfilename) . "&", $iferr);
          $concernyourself++;
          $cr.=$crd . $ourfilename; // . " selected";
          if ($dcr != " ") { $dcr.=$crd . "<a " . $adatam . "onmouseover=\"butwhatif(this,'" . less($ourfilename) . "');\" onclick=\"butwhatif(this,'" . less($ourfilename) . "');\" target=myif href='" . $ourfilename . "'>" . basename($ourfilename) . "</a>"; }
          $crd="\n";
        } else {
          $cr.=$crd . $ourfilename;
          if ($dcr != " ") { $dcr.=$crd . "<a " . $adatam . "onmouseover=\"butwhatif(this,'" . less($ourfilename) . "');\" onclick=\"butwhatif(this,'" . less($ourfilename) . "');\" target=myif href='" . $ourfilename . "'>" . basename($ourfilename) . "</a>"; }
          $crd="\n";
        }
        array_push($ziparr, $ourfilename);
        $isc=zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
        $im_php = imagecreatefromstring($isc);
        $width = imagesx($im_php);
        $height = imagesy($im_php);
        imagedestroy($im_php);
        array_push($ziparrwh, '' . $width . ',' . $height);
        array_push($ziparrc, $isc);
       }
       }
       } else { // reject this one
       if (strpos($ourfilename, "_MACOSX/") === false) {
       $regexp="/^[\S]" . str_replace('[\s\S]?','[\s\S]*',str_replace('*','[\s\S]?',str_replace('?',',',str_replace('%',',',str_replace('.','[.]',$ofwhat))))) . "$/";
       $regexpok=preg_match($regexp, explode("/",$ourfilename)[-1 + sizeof(explode("/",$ourfilename))]);
       if ($regexpok !== 0) {
          $cr.=$crd . $ourfilename;
          if ($dcr != " ") { $dcr.=$crd . "<a " . $adatam . "onmouseover=\"butwhatif(this,'" . less($ourfilename) . "');\" onclick=\"butwhatif(this,'" . less($ourfilename) . "');\" target=myif href='" . $ourfilename . "'>" . basename($ourfilename) . "</a>"; }
          $crd="\n";
       }
       }
       }
      }
      zip_close($zip);
      $includesare="";
     }
     //echo $zhuha;
     //exit;
     return $ziparr;
   }
 }

echo "<!doctyle html>
<html>
<head>
<style>
  th { border-top: 1px dotted red; }
  .ca:visited {  color: pink;  }
</style>
<s" . "cript type=text/javascript>
  var cura=0;
  var aaas=[];

  function checkca() {
    if (aaas.length == 0) { aaas=document.getElementsByTagName('a'); }
    for (var ias=0; ias<aaas.length; ias++) {
      if (('' + aaas[ias].className) == 'ca' && ias == cura) {
        if (cura > 10) { alert(aaas[ias].title); }
        aaas[ias].click();
        cura++;
        setTimeout(checkca, 5000);
      } else if (ias == cura) {
        cura++;
        setTimeout(checkca, 200);
      }
    }
  }

  function closeall() {
    var aas=document.getElementsByTagName('details');
    for (var ias=0; ias<aas.length; ias++) {
        aas[ias].removeAttribute('open');
        aas[ias].style.display='none';
    }
    document.getElementsByTagName('deta').innerHTML='-';
  }
  
</sc" . "ript>
</head>
<body>
<h1 id=myh1>Image Conversions</h1>
<h3>RJM Programming <a onclick=closeall(); id=deta style='cursor:pointer;text-decoration:none;'>-</a> October, 2022</h3>
<table>" . $tablerows . "
</table>" . $inputask . "
</body>
</html>";

?>