<?php
  // gimp_guillotine_followup.php
  // RJM Programming
  // August, 2018
  // Follow up on a Gimp ...
  // 1) Create Gimp Guides
  // 2) "Image -> Transform -> Guillotine"
  
  
  function less($ins) {
    return str_replace(".","",str_replace(substr("\\",0,1),"",str_replace("/","",$ins)));
  }
  
  if (!isset($_GET['donothing']) || isset($_POST['filespec']) || isset($_GET['filespec'])) {
  $img_filelist="";
  $gimp_place="";
  $secondbit="<br><textarea id=tresults style='display:none;' cols=180 rows=20></textarea>";
  $commis="";
  $commresult="";
  $results="";
  $fspec="";
  $jscr="";
  $imagec="";
  $imagex="";
  $cwidth="500";
  $cheight="500";
  $imgd="";
  $ifm="<iframe style='display:none;' src='./gimp_guillotine_followup.php?temp_file_list=" . urlencode($temp_file_list) . "&command=" . urlencode($commis) . "'></iframe>";
  $thirdbit="";
  $fourthbit="";
  $none="none";
  $below="Image";
  $bimg="";
  $subject='Perhaps a Gimp Guillotine Image';
  $to='';
  $headers='';
  $via='mycanvas';
  $ext="";
  if (isset($_GET['filespec'])) { $fspec=str_replace("+"," ",urldecode($_GET['filespec'])); if (strpos($fspec, "#") !== false) {  $parts=explode("#",$fspec); if (strpos($parts[1],"@") !== false) { $to=$parts[1];  $fspec=$parts[0];   }   }  }
  if (isset($_GET['divmarginuse'])) { $none="INLINE-BLOCK;opacity:0.5;z-index:-6"; $via='topdiv'; }
  if (isset($_GET['canvas'])) { $bimg=" bimg(true); "; }
  if (isset($_GET['canvasnorepeat'])) { $bimg=" bimg(false); "; }

  
  if (isset($_GET['command']) && isset($_GET['temp_file_list'])) {
   while ($commresult == "") {
    exec(str_replace("+"," ",urldecode($_GET['command'])));
    $commresult=file_get_contents(str_replace("+"," ",urldecode($_GET['temp_file_list'])));
    if ($commresult == "") {
      sleep(5);
    } else {
      if (strpos($commresult, "-0-0.") !== false) {
        $prefix=explode("-0-0.", $commresult)[0];
        $cr="";
        $crd="";
        $row="0";
        $cnt=0;
        $bdel="<br>";
        $cumwidth=0;
        $premargin="";
        $margin="";
        foreach (glob($prefix . "-*-*.*g*") as $filename) {
          $ifm="";
          if ($below == "Image") $below="<a onclick='dodur();' href='#" . less($filename) . "' title='Div Margin Use'>Image</a> (or in <a style=text-decoration:underline;cursor:pointer; onclick='intonew(); dodur();' title='Show in Div Margin Use in New Window'>New Window</a> or <a style=text-decoration:underline;cursor:pointer; onclick='bimg(true);' title='Background Image via Canvas'>Background Image via Canvas</a> (<a style=text-decoration:underline;cursor:pointer; onclick='bimg(false);' title='Background Image via Canvas'>no-repeat</a>))";
          if ($ext == "") $ext="." . explode(".", $filename)[-1 + sizeof(explode(".", $filename))];
          list($width, $height, $type, $attr) = getimagesize($filename);
          $imagec.=$filename . ':' . $width . ':' . $height . ',';
          if ($imagex == "") {
            $imagex="'; \n function ios() { \n";
            if ("row" == "0") $cwidth=$width;
            if ("row" == "0") $cheight=$height;
            $row=explode(".",explode("-", $filename)[-1 + sizeof(explode("-", $filename))])[0];
          } else if (explode(".",explode("-", $filename)[-1 + sizeof(explode("-", $filename))])[0] != "$row") {
            $premargin="margin-top:-" . (explode(".",explode("-", $filename)[-1 + sizeof(explode("-", $filename))])[0] * 6) . "px;";
            $cumwidth=0;
            if (strpos($filename, "-0-" . $row . ".") === false) {
              if (strpos($thirdbit, "-" . explode(".",explode("-", $filename)[-2 + sizeof(explode("-", $filename))])[0] . "-0" . $ext . "' style='left:") !== false) {
                $cumwidth=explode("px", explode("-" . explode(".",explode("-", $filename)[-2 + sizeof(explode("-", $filename))])[0] . "-0" . $ext . "' style='left:", $thirdbit)[1])[0];
                $margin="margin-";
              }
            }
            $bdel="<br>";
            $row=explode(".",explode("-", $filename)[-1 + sizeof(explode("-", $filename))])[0];
            $cheight+=$height;
          } else if ("$row" == "0") {
            $cwidth+=$width;
          }
          if (1 == 1) {
          $thirdbit.=$bdel . "<img id='" . less($filename) . "' width=" . $width . " height=" . $height . " title='" . $filename . "' style='" . $premargin . $margin . "left:" . $cumwidth . "px;display:INLINE-BLOCK;' alt=img" . $cnt . " src='data:image/" . substr($ext,1) . ";base64," . base64_encode(file_get_contents($filename)) . "'></img>"; 
          $bdel="";
          $margin="";
          $cnt++;
          } else {
          $imagex.="\n imageo.push(new Image()); \n imageo[eval(-1 + imageo.length)].src='data:image/" . substr($ext,1) . ";base64," . base64_encode(file_get_contents($filename)) . "'; \n";
          }
          $cumwidth+=$width;
          if ($jscr == "") $jscr="\n if (imagec == ',') { dur=dur;  ";
          $jscr.="\n
           imagec+='" . $filename . ",';
           imageo.push(new Image());
           imageo[eval(-1 + imageo.length)].onload = function () {
           imagec = imagec.replace('," . $filename . ",', '," . $filename . ":' + eval(('' + this.width).replace('px','')) + ':' + eval(('' + this.height).replace('px','')) + ',');
           };
           imageo[eval(-1 + imageo.length)].src='" . $filename . "';
          \n";
          $cr.=$crd . $filename;
          $crd="\n";
        }
        if ($imagex != "") $imagex.="\n } \n imagec+='";
        if ($jscr != "") $jscr.=" \n } \n if (imagec != ',') {  lookati();    } \n";
        echo "<html><body onload=\" if (parent.document.getElementById('results')) { parent.document.getElementById('results').innerHTML='" . str_replace("\n","' + String.fromCharCode(10) + '",$cr) . "'; }    \"></body></html>";
        exit;
      }
    }
   }    
  } else if (isset($_POST['filespec']) || $fspec != "") {
   $dcr="";
   $cr="";
   $crd="";
   $row="0";
   $cnt=0;
   if ($fspec == "") { $fspec=str_replace("+"," ",urldecode($_POST['filespec'])); }
   $bdel="<br>";
   $cumwidth=0;
   $premargin="";
   $margin="";
   foreach (glob($fspec) as $filename) {
          $ifm="";
          if ($below == "Image") $below="<a onclick='dodur();' href='#" . less($filename) . "' title='Div Margin Use'>Image</a> (or in <a style=text-decoration:underline;cursor:pointer; onclick='intonew(); dodur();' title='Show in Div Margin Use in New Window'>New Window</a> or <a style=text-decoration:underline;cursor:pointer; onclick='bimg(true);' title='Background Image via Canvas'>Background Image via Canvas</a> (<a style=text-decoration:underline;cursor:pointer; onclick='bimg(false);' title='Background Image via Canvas'>no-repeat</a>))";
          if ($ext == "") $ext="." . explode(".", $filename)[-1 + sizeof(explode(".", $filename))];
          if ($jscr == "") $jscr="\n if (imagec == ',') { dur=dur;   ";
          list($width, $height, $type, $attr) = getimagesize($filename);
          $imagec.=$filename . ':' . $width . ':' . $height . ',';
          if ($imagex == "") {
            $imagex="'; \n function ios() { \n";
            if ("row" == "0") $cwidth=$width;
            if ("row" == "0") $cheight=$height;
            $row=explode(".",explode("-", $filename)[-1 + sizeof(explode("-", $filename))])[0];
          } else if (explode(".",explode("-", $filename)[-1 + sizeof(explode("-", $filename))])[0] != "$row") {
            $premargin="margin-top:-" . (explode(".",explode("-", $filename)[-1 + sizeof(explode("-", $filename))])[0] * 6) . "px;";
            $cumwidth=0;
            if (strpos($filename, "-0-" . $row . ".") === false) {
              if (strpos($thirdbit, "-" . explode(".",explode("-", $filename)[-2 + sizeof(explode("-", $filename))])[0] . "-0" . $ext . "' style='left:") !== false) {
                $cumwidth=explode("px", explode("-" . explode(".",explode("-", $filename)[-2 + sizeof(explode("-", $filename))])[0] . "-0" . $ext . "' style='left:", $thirdbit)[1])[0];
                $margin="margin-";
              }
            }
            $bdel="<br>";
            $row=explode(".",explode("-", $filename)[-1 + sizeof(explode("-", $filename))])[0];
            $cheight+=$height;
          } else if ("$row" == "0") {
            $cwidth+=$width;
          }
          if (1 == 1) {
          $thirdbit.=$bdel . "<img id='" . less($filename) . "' width=" . $width . " height=" . $height . " title='" . $filename . "' style='" . $premargin . $margin . "left:" . $cumwidth . "px;display:INLINE-BLOCK;' alt=img" . $cnt . " src='data:image/" . substr($ext,1) . ";base64," . base64_encode(file_get_contents($filename)) . "'></img>"; 
          $bdel="";
          $margin="";
          $cnt++;
          } else {
          $imagex.="\n imageo.push(new Image()); \n imageo[eval(-1 + imageo.length)].src='data:image/" . substr($ext,1) . ";base64," . base64_encode(file_get_contents($filename)) . "'; \n";
          }
          $cumwidth+=$width;
          if (1 == 2) $jscr.="\n
           imagec+='" . $filename . ",';
           imageo.push(new Image());
           imageo[eval(-1 + imageo.length)].onload = function () {
           imagec = imagec.replace('," . $filename . ",', '," . $filename . ":' + eval(('' + this.width).replace('px','')) + ':' + eval(('' + this.height).replace('px','')) + ',');
           };
           imageo[eval(-1 + imageo.length)].src='" . $filename . "';
          \n";
          $cr.=$crd . $filename;
          $dcr.=$crd . "<a target=myif href='" . $filename . "'>" . $filename . "</a>";
          $crd="\n";
   }
   if ($imagex != "") $imagex.="\n } \n imagec+='";
   if ($jscr != "") $jscr.=" \n } \n if (imagec != ',') { lookati();    }  \n";
   //$jscr.="\n alert(imagec); \n";
   //$jscr.="\n alert(imagec); \n";
   if ($cr == "") {
      //echo "<html><body onload=\" if (parent.document.getElementById('results')) { parent.document.getElementById('results').innerHTML=' '; }    \"></body></html>";
      //exit;
      $results=" ";
      $secondbit="<br><textarea id=tresults style='display:block;' cols=180 rows=20>" . $results . "</textarea>";
   } else {
      //echo "<html><body onload=\" if (parent.document.getElementById('results')) { parent.document.getElementById('results').innerHTML='" . str_replace("\n","' + String.fromCharCode(10) + '",$cr) . "'; }    \"></body></html>";
      //exit;
      $results=$cr;
      $secondbit="<br><table border=5><tr><td><textarea id=tresults style='display:block;' cols=80 rows=20>" . $results . "</textarea></td><td style='vertical-align:top;'><div id=dresults>" . str_replace("\n","<br>",$dcr) . "</div></td></td><td style='vertical-align:top;'><iframe name=myif id=myif src='./gimp_guillotine_followup.php?donothing=y'></iframe></td></tr></table><br>"; //<canvas id=mycanvas width=" . $cwidth . " height=" . $cheight . " style=display:INLINE-BLOCK;></canvas>"; // . $thirdbit . "";
   }
   if ($to != "") {
            $eol = PHP_EOL;
            $headers = 'From: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;
            $headers .= 'Reply-To: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;

            $filname="gimp_guillotine.html";
                $content = chunk_split(base64_encode( "<!doctype html><html><body>" . str_replace("margin-top:-","margin-top:0.00",str_replace("margin-top: ","margin-top:",$thirdbit)) . "</body></html>" ));
                //$uid = md5(uniqid(time()));

                // a random hash will be necessary to send mixed content
                $separator = md5(time());

                $headers .= "MIME-Version: 1.0" . $eol;
                $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol . $eol;
                $headers .= "Content-Transfer-Encoding: 7bit" . $eol;
                $headers .= "This is a MIME encoded message." . $eol . $eol;

                // message
                $headers .= "--" . $separator . $eol;
                $headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
                $headers .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;

                $headers .= "Please see attachment below:" . $eol . $eol;

            //ourpremail($tem, $mysubject, "", $headers . "--" . $separator . "--");

                // attachment
                $headers .= "--" . $separator . $eol;
                $headers .= "Content-Type: application/octet-stream; name=\"" . $filname . "\"" . $eol;
                $headers .= "Content-Transfer-Encoding: base64" . $eol;
                $headers .= "Content-Disposition: attachment;filename=\"" . $filename . "\"" . $eol;
                $headers .= $content . $eol . $eol;

                $headers .= "--" . $separator . "--";

            //mail($to, $subject, "", $headers);
   }
  }

  if (file_exists("../../GIMP.app/Contents/MacOS/Gimp")) {
      $gimp_place="../../GIMP.app/Contents/MacOS/Gimp";
  } else if (file_exists("/Applications/GIMP.app/Contents/MacOS/Gimp")) {
      $gimp_place="/Applications/GIMP.app/Contents/MacOS/Gimp";
  }
  
  if ($ifm != "") {
      $ifm="<iframe style='display:none;' src='./gimp_guillotine_followup.php?temp_file_list=" . urlencode($temp_file_list) . "&command=" . urlencode($commis) . "'></iframe>";
  }
  
  if ($gimp_place == "") {
      echo "<!doctype html>
<html>
<head>
<title>GIMP Guillotine Follow Up - RJM Programming - August, 2018</title>
<style>
  margin { 0 0 0 0 }
  padding { 0 0 0 0 }
</style>
<scr" . "ipt type='text/javascript'>
            var dur='';
            var lookatidone=false;
            var elem=null;
            var context=null;
            var ext='" . $ext . "';
            var results='';
            var imageo=new Array();
            var imagec='," . $imagec . $imagex . "';
            /" . "/ var imagec=',./mondrian-1504681_640-0-0.png;133;146,./mondrian-1504681_640-1-0.png;164;146,./mondrian-1504681_640-2-0.png;343;146,./mondrian-1504681_640-2-1.png;343;145,./mondrian-1504681_640-2-2.png;343;189,';
  function bimg(dobr) {
            if (dobr) document.body.style.backgroundRepeat='repeat';
            if (!dobr) document.body.style.backgroundRepeat='no-repeat';
            document.body.style.backgroundImage=\"url('\" + dur + \"')\";
  }
  function ourdocumentgetElementById(huh) {
    var isa=document.getElementsByTagName('img');
    for (var jsa=0; jsa<isa.length; jsa++) {
      if (isa[jsa].alt == huh) return isa[jsa];
    }
    return document.getElementById(huh);
  }
            function intonew() {
            var wo=window.open('','_blank','top=0,left=0,height=" . $cheight . ",width=" . $cwidth . "');
            wo.document.write('<style> margin { 0 0 0 0 } padding { 0 0 0 0 } </style>' + document.getElementById('topdiv').innerHTML.replace(/\-top\:\ /g,'-top:').replace(/\-top\:\-/g,'-top:0.00'));
            }
            function dodur() {
            var iob=new Image();
            if (dur == '') {
            dur=document.getElementById('dhuh').innerHTML;
            }
            if (dur == '') {
            if (parent.document.getElementById('dhuh')) {
            dur=parent.document.getElementById('dhuh').innerHTML;
            }
            }
            //alert('DuR=' + dur);
            iob.onload = function () {
            //alert(11);
            elem=document.getElementById('mycanvas');
            //alert(111);
            context=elem.getContext('2d');
            //alert(1111);
            context.drawImage(this,0,0);
            //alert(21111);
            };
            iob.src=dur;
            }
            function lookati() {
            if (!lookatidone) {
            lookatidone=true;
//document.title='5';
            // ios();
//document.title='51';
            //alert('-0-0' + ext + ': In ' + imagec);
            elem.style.display='block';
            var myimagec=imagec;
            imagec=',';
            var pfix=myimagec.split('-0-0' + ext + ':')[0].split(',')[eval(-1 + myimagec.split('-0-0' + ext + ':')[0].split(',').length)];
            // var iis=myimagec.split(',');
            var icol=0, irow=0, top=0, left=0, cumtop=0, cumleft=0, fwidth=0, fheight=0, thisw=0, thish=0, thisws=[];
            while (myimagec.indexOf('-' + irow + ext + ':') != -1) {
              if (myimagec.indexOf('-' + irow + ext + ':') != -1) {
               if (myimagec.indexOf('-' + icol + '-' + irow + ext + ':') != -1) {
                 thisw=eval(myimagec.split('-' + icol + '-' + irow + ext + ':')[1].split(':')[0]);
                 thish=eval(myimagec.split('-' + icol + '-' + irow + ext + ':')[1].split(':')[1].split(',')[0]);
                 //alert(eval(-1 + myimagec.split(',' + pfix + '-' + icol + '-' + irow + ext)[0].split(',').length));
                 if (ourdocumentgetElementById('img' + eval(-1 + myimagec.split(',' + pfix + '-' + icol + '-' + irow + ext)[0].split(',').length))) {
                 //ourdocumentgetElementById('img' + eval(-1 + myimagec.split(',' + pfix + '-' + icol + '-' + irow + ext)[0].split(',').length)).style.position='absolute';
                 //ourdocumentgetElementById('img' + eval(-1 + myimagec.split(',' + pfix + '-' + icol + '-' + irow + ext)[0].split(',').length)).style.top='' + top + 'px';
                 //ourdocumentgetElementById('img' + eval(-1 + myimagec.split(',' + pfix + '-' + icol + '-' + irow + ext)[0].split(',').length)).style.left='' + left + 'px';
                 try {
                 context.drawImage(ourdocumentgetElementById('img' + eval(-1 + myimagec.split(',' + pfix + '-' + icol + '-' + irow + ext)[0].split(',').length)), left, top);
                 } catch(ee) {
                 alert(ee.message);
                 }
                 //alert('-' + eval(-1 + myimagec.split(',' + pfix + '-' + icol + '-' + irow + ext)[0].split(',').length));
                 } else {
                 context.drawImage(imageo[eval(-1 + myimagec.split(',' + pfix + '-' + icol + '-' + irow + ext)[0].split(',').length)], left, top);
                 }
    //document.title+='context.drawImage(z[' + (-1 + myimagec.split(',' + pfix + '-' + icol + '-' + irow + ext)[0].split(',').length) + '],' + top + ',' + left + ')';
 myimagec=myimagec.replace('-' + icol + '-' + irow + ext + ':','_' + icol + '_' + irow + ext + ':');
                 left+=thisw;
                 thisws.push(thisw);
                                  if (myimagec.indexOf('-' + irow + ext + ':') == -1) {
               icol=-1;
               irow++;
               top+=thish;
               left=0;
                 }

               } else {
                 left+=thisws[icol];
               }
               icol++;
              } else {
               icol=0;
               irow++;
               top+=thish;
               left=0;
              } 
            }
            if (elem) {
            dur=elem.toDataURL();
            document.getElementById('dhuh').innerHTML=dur;
            //dodur();
            }
            //alert('dur=' + dur);
            //document.body.style.backgroundImage=\"url('\" + dur + \"')\";
            if (document.URL.indexOf('divmarginuse=') != -1) {
            document.getElementById('topdiv').style.display='INLINE-BLOCK';
            dodur();
            }
            }
            }
            function prelook() {
            if (!elem) {
            if (document.getElementById('mycanvas')) {
            elem=document.getElementById('mycanvas');
            context=elem.getContext('2d');
            }
            }
            //if (imagec != ',') { lookati(); }             
            }
            function looknow() {
            if (dur == '') {
            results=document.getElementById('results').innerHTML;
            if (results == '' || (results.indexOf('.') == -1 && results.indexOf(' ') == -1)) {
              setTimeout(looknow,5000);
            } else if ((results + ',').substring(0,1) == ' ') {
              alert('No files found in ' + document.getElementById('filespec').value + ' ...  Please try again');
              document.getElementById('filespec').value='';
            } else {
              document.getElementById('tresults').innerHTML=results;
              document.getElementById('tresults').style.display='block';
            }
            } " . $jscr . "
            }
</scr" . "ipt>
</head>
<body onload='prelook();  looknow(); " . $bimg . "'>
<div id=topdiv style='position:absolute;top:0px;left:0px;display:" . $none . ";'>" . str_replace(" id=", " data-id=",$thirdbit) . "</div>
<div id=results style=display:none;>" . $results . "</div>
<h1>GIMP Guillotine Follow Up</h1>
<h3>RJM Programming - August, 2018</h3><br><br>
<h4>Input Images</h4>
<form target='_top' method=GET action=./gimp_guillotine_followup.php>
GIMP Guillotine Image Filespec (of images): <input title='Optionally append by # then email address to email pieced together image' placeholder='Optionally append by # then email address to email pieced together image' style='width:40%;' type='text' value='" . $fspec . "' name='filespec' id='filespec'></input><br><br>
<input style='background-color:lightgreen;' type=submit value=Process... id=isubmit></input>&nbsp;<input style='background-color:lightgreen;' type=submit value='Process with Top Div Margin Use' name=divmarginuse></input>&nbsp;<input style='background-color:lightgreen;' type=submit value='Process with Canvas Background Image Repeat' name=canvas></input>&nbsp;<input style='background-color:lightgreen;' type=submit value='Process with Canvas Background Image No Repeat' name=canvasnorepeat></input>
</form>
<hr><br><br>
<h4>HTML Use of Input " . $below . " List Below</h4>" . $secondbit . $ifm . "<iframe id='postit' name='postit' style='display:none;' src='./gimp_guillotine_followup.php?donothing=y'></iframe><canvas onclick=dodur(); id=mycanvas width=" . $cwidth . " height=" . $cheight . " style=display:INLINE-BLOCK;></canvas>" . $thirdbit . "<div id=dhuh style=display:none;'></div></body></html>";
    if ($to != '' && $headers != '') {
     mail($to, $subject, "", $headers);
    }
        exit;
  } else if (isset($_GET['gimp'])) {
      exec($gimp_place);
      exit;
  } else {
      $temp_file_list = tempnam(sys_get_temp_dir(), 'myfile.list');
      exec("echo '' > " . $temp_file_list);
      $temp_file = tempnam(sys_get_temp_dir(), 'myfile.name');
      exec("echo '' > " . $temp_file);
      //echo $temp_file;
      if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
      $commis="forfiles /P C:\\ /M *-0-0.*g* /S /D -0 2> nul > " . $temp_file_list;
      } else {
      $commis="find / -name '*-0-0.*g*' -type f -newer " . $temp_file . " -print 2> /dev/null > " . $temp_file_list;
      }

      echo "<!doctype html>
<html>
<head>
<title>GIMP Guillotine Follow Up - RJM Programming - August, 2018</title>
<style>
  margin { 0 0 0 0 }
  padding { 0 0 0 0 }
</style>
<scr" . "ipt type='text/javascript'>
            var dur='';
            var lookatidone=false;
            var elem=null;
            var context=null;
            var ext='" . $ext . "';
            var results='';
            var imageo=new Array();
            var imagec='," . $imagec . $imagex . "';
  function bimg(dobr) {
            if (dobr) document.body.style.backgroundRepeat='repeat';
            if (!dobr) document.body.style.backgroundRepeat='no-repeat';
            document.body.style.backgroundImage=\"url('\" + dur + \"')\";
  }
  function ourdocumentgetElementById(huh) {
    var isa=document.getElementsByTagName('img');
    for (var jsa=0; jsa<isa.length; jsa++) {
      if (isa[jsa].alt == huh) return isa[jsa];
    }
    return document.getElementById(huh);
  }
            function dodur() {
            var iob=new Image();
            if (dur == '') {
            dur=document.getElementById('dhuh').innerHTML;
            }
            if (dur == '') {
            if (parent.document.getElementById('dhuh')) {
            dur=parent.document.getElementById('dhuh').innerHTML;
            }
            }
            //alert('dUr=' + dur);
            iob.onload = function () {
            //alert(11);
            elem=document.getElementById('mycanvas');
            //alert(111);
            context=elem.getContext('2d');
            //alert(1111);
            context.drawImage(this,0,0);
            //alert(21111);
            };
            iob.src=dur;
            }
            function intonew() {
            var wo=window.open('','_blank','top=0,left=0,height=" . $cheight . ",width=" . $cwidth . "');
            wo.document.write('<style> margin { 0 0 0 0 } padding { 0 0 0 0 } </style>' + document.getElementById('topdiv').innerHTML.replace(/\-top\:\ /g,'-top:').replace(/\-top\:\-/g,'-top:0.00'));
            }
            function lookati() {
            if (!lookatidone) {
            lookatidone=true;
//document.title='54';
            // ios();
//document.title='154';
            //alert('-0-0' + ext + ': in ' + imagec);
            elem.style.display='block';
            var myimagec=imagec;
            imagec=',';
            var pfix=myimagec.split('-0-0' + ext + ':')[0].split(',')[eval(-1 + myimagec.split('-0-0' + ext + ':')[0].split(',').length)];
            // alert(pfix);
            // var iis=myimagec.split(',');
            var icol=0, irow=0, top=0, left=0, cumtop=0, cumleft=0, fwidth=0, fheight=0, thisw=0, thish=0, thisws=[];
            while (myimagec.indexOf('-' + irow + ext + ':') != -1) {
              if (myimagec.indexOf('-' + irow + ext + ':') != -1) {
               if (myimagec.indexOf('-' + icol + '-' + irow + ext + ':') != -1) {
                 thisw=eval(myimagec.split('-' + icol + '-' + irow + ext + ':')[1].split(':')[0]);
                 thish=eval(myimagec.split('-' + icol + '-' + irow + ext + ':')[1].split(':')[1].split(',')[0]);
                 //document.title+='-' + 'img' + eval(-1 + myimagec.split(',' + pfix + '-' + icol + '-' + irow + ext)[0].split(',').length);
                 //alert(eval(-1 + myimagec.split(',' + pfix + '-' + icol + '-' + irow + ext)[0].split(',').length));
                 if (ourdocumentgetElementById('img' + eval(-1 + myimagec.split(',' + pfix + '-' + icol + '-' + irow + ext)[0].split(',').length))) {
                 //ourdocumentgetElementById('img' + eval(-1 + myimagec.split(',' + pfix + '-' + icol + '-' + irow + ext)[0].split(',').length)).style.position='absolute';
                 //ourdocumentgetElementById('img' + eval(-1 + myimagec.split(',' + pfix + '-' + icol + '-' + irow + ext)[0].split(',').length)).style.top='' + top + 'px';
                 //ourdocumentgetElementById('img' + eval(-1 + myimagec.split(',' + pfix + '-' + icol + '-' + irow + ext)[0].split(',').length)).style.left='' + left + 'px';
                 try {
                 context.drawImage(ourdocumentgetElementById('img' + eval(-1 + myimagec.split(',' + pfix + '-' + icol + '-' + irow + ext)[0].split(',').length)), left, top);
                 } catch(ee) {
                 alert(ee.message);
                 }
                 //alert('--' + eval(-1 + myimagec.split(',' + pfix + '-' + icol + '-' + irow + ext)[0].split(',').length));
				 } else {
                 context.drawImage(imageo[eval(-1 + myimagec.split(',' + pfix + '-' + icol + '-' + irow + ext)[0].split(',').length)], left, top);
                 }
    //document.title+='context.drawImage(x[' + (-1 + myimagec.split(',' + pfix + '-' + icol + '-' + irow + ext)[0].split(',').length) + '],' + top + ',' + left + ')';
 myimagec=myimagec.replace('-' + icol + '-' + irow + ext + ':','_' + icol + '_' + irow + ext + ':');
     //alert(document.title);
                 left+=thisw;
                 thisws.push(thisw);
                 if (myimagec.indexOf('-' + irow + ext + ':') == -1) {
               icol=-1;
               irow++;
               top+=thish;
               left=0;
                 }
               } else {
                 left+=thisws[icol];
               }
               icol++;
              } else {
               icol=0;
               irow++;
               top+=thish;
               left=0;
              } 
            }
            if (elem) {
            dur=elem.toDataURL();
            document.getElementById('dhuh').innerHTML=dur;
            //dodur();
            }
            //alert('Dur=' + dur);
            //document.body.style.backgroundImage=\"url('\" + dur + \"')\";
            if (document.URL.indexOf('divmarginuse=') != -1) {
            document.getElementById('topdiv').style.display='INLINE-BLOCK';
            dodur();
            }
            }
            }
            function prelook() {
            if (!elem) {
            if (document.getElementById('mycanvas')) {
            elem=document.getElementById('mycanvas');
            context=elem.getContext('2d');
            //alert('/' + imagec);
            }
            }
            //if (imagec != ',') { lookati(); } 
            }
            function looknow() {
            if (dur == '') {
            results=document.getElementById('results').innerHTML;
            if (results == '' || (results.indexOf('.') == -1 && results.indexOf(' ') == -1)) {
              setTimeout(looknow,5000);
            } else if ((results + ',').substring(0,1) == ' ') {
              alert('No files found in ' + document.getElementById('filespec').value + ' ...  Please try again');
              document.getElementById('filespec').value='';
            } else {
              document.getElementById('tresults').innerHTML=results;
              document.getElementById('tresults').style.display='block';
            }
            } " . $jscr . "
            }
</scr" . "ipt>
</head>
<body onload='prelook(); looknow(); " . $bimg . "'>
<div id=topdiv style='position:absolute;top:0px;left:0px;display:" . $none . ";'>" . str_replace(" id=", " data-id=",$thirdbit) . "</div>
<div id=results style=display:none;>" . $results . "</div>
<h1>GIMP Guillotine Follow Up</h1>
<h3>RJM Programming - August, 2018</h3><br><br>
<h4>Input Images</h4>
<form target='_top' method=GET action=./gimp_guillotine_followup.php>
GIMP Guillotine Image Filespec (of images): <input title='Optionally append by # then email address to email pieced together image' placeholder='Optionally append by # then email address to email pieced together image' style='width:40%;' type='text' value='" . $fspec . "' name='filespec' id='filespec'></input> ... Versus ... <input style='background-color:yellow;' type=button value='GIMP ... Image - Transform - Guillotine' onclick=\" document.getElementById('donothing').src='./gimp_guillotine_followup.php?gimp=y';  \"></input><br><br>
<input style='background-color:lightgreen;' type=submit value=Process... id=isubmit></input>&nbsp;<input style='background-color:lightgreen;' type=submit value='Process with Top Div Margin Use' name=divmarginuse></input>&nbsp;<input style='background-color:lightgreen;' type=submit value='Process with Canvas Background Image Repeat' name=canvas></input>&nbsp;<input style='background-color:lightgreen;' type=submit value='Process with Canvas Background Image No Repeat' name=canvasnorepeat></input>
</form>
<hr><br><br>
<h4>HTML Use of Input " . $below . " List Below</h4>" . $secondbit . $ifm . "<iframe id='donothing' style='display:none;' src='./gimp_guillotine_followup.php?donothing=y'></iframe><iframe id='postit' name='postit' style='display:none;' src='./gimp_guillotine_followup.php?donothing=y'></iframe><canvas onclick=dodur(); id=mycanvas width=" . $cwidth . " height=" . $cheight . " style=display:INLINE-BLOCK;></canvas>" . $thirdbit . "<div id=dhuh style=display:none;'></div></body></html>";
  }
    if ($to != '' && $headers != '') {
     mail($to, $subject, "", $headers);
    }
        exit;
  } else {
    exit;
  }
?>