<?php
// speech_bubble_overlays.php
// RJM Programming
// October, 2025
// ImageMagick extraction of macOS Preview helping Paintbrush PNG image Speech Bubble content as overlaying background images onto a base image
// As asked for a new overlay image can be created via PHP exec ImageMagick calls
// Thanks to https://github.com/ImageMagick/ImageMagick/discussions/4606 and https://stackoverflow.com/questions/71094480/how-to-execute-multi-line-command-with-shell-exec
header('Access-Control-Allow-Origin: http://www.rjmprogramming.com.au');
//header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');

$locbit='<input name=hidelocal value=y type=hidden></input>';
$port=":8888";
$dataize="";
$ib="none";
$cmd="";
$newcmd="";
$baseimage='./luna_upstairs.jpg';
$origbase=dirname(__FILE__) . DIRECTORY_SEPARATOR . basename($baseimage);
$overlayimage='./luna_backagain.png';
$origoverlay=dirname(__FILE__) . DIRECTORY_SEPARATOR . basename($overlayimage);
$exceptthat="";
$onl="";
$thissw="";
if (isset($_GET['myinnards'])) {
  $thissw=str_replace('+',' ',urldecode($_GET['myinnards']));
  $thissw=$_GET['myinnards'];
  if (strpos($thissw, '%') === false && strpos($thissw, '+') !== false) {
    $thissw=str_replace('+','%20',urlencode($thissw));
  } else if (strpos($thissw, '+') !== false) {
    $thissw=str_replace('+','%20',urlencode($thissw));
  } else if (strpos($thissw, ' ') !== false) {
    $thissw=str_replace('+','%20',urlencode($thissw));
  }
} else if (isset($_POST['myinnards'])) {
  $thissw=str_replace('+',' ',urldecode($_POST['myinnards']));
  $thissw=$_POST['myinnards'];
  if (strpos($thissw, '%') === false && strpos($thissw, '+') !== false) {
    $thissw=str_replace('+','%20',urlencode($thissw));
  } else if (strpos($thissw, ' ') !== false) {
    $thissw=str_replace('+','%20',urlencode($thissw));
  } else if (strpos($thissw, '+') !== false) {
    $thissw=str_replace('+','%20',urlencode($thissw));
  }
}

if (isset($_GET['hidelocal'])) {
  $ib="none";
  $dataize="data-";
} else if (isset($_GET['showlocal'])) {
  $ib="inline-block";
  $dataize="data-";
  if (substr((str_replace('+',' ',urldecode($_GET['showlocal'])) . ':'),0,1) == ':') {
    $port=str_replace('+',' ',urldecode($_GET['showlocal']));
    $locbit="<input id=showlocal name=showlocal value='" . $port . "' type=hidden></input>";
  } else {
    $locbit="<input id=showlocal name=showlocal value='" . $port . "' type=hidden></input>";
  }
}
if (strpos($_SERVER['SERVER_NAME'], 'localhost') !== false) {
  $dataize='data-';
  $ib="none";
}


file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . "speech_bubble_overlays.json", "{\n\"productName\": \"Super Widget\",\n\"price\": 19.99,\n\"inStock\": true,\n\"colors\": [\"red\", \"blue\", \"green\"]}");
file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . "speech_bubble_overlays.js", " function okaytogo() {  } ");

if (isset($_GET['callback']) && 3 == 6) {
  echo file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . "speech_bubble_overlays.json");
  exit;
}

$innards="";
$fgc="";
$ifstuff="<iframe id=lfi style=display:none; onerror='errmampshowImg(event);' onload='checkif(this);' src='HTTP://localhost" . $port . "/speech_bubble_overlays.php?rand=" . rand(0,45678) . "'></iframe>";
if (strpos($_SERVER['SERVER_NAME'], 'localhost') !== false) {
  $ifstuff='';
  if (file_exists($origoverlay)) {
   if (strpos($_SERVER['SERVER_NAME'], 'localhost') === false) {
    $onl=' parent.document.getElementById("locbutone").style.display="inline-block"; ';
   }
  }
} else {
    $innards="$.ajax({
    url: \"HTTP://localhost:" . $port . "/speech_bubble_overlays.php\", // The URL of the cross-domain API
    type: \"GET\",
    dataType: \"jsonp\", // Specifies that it's a JSONP request
    jsonp: \"callback\", // The name of the query parameter for the callback function (default is 'callback')
    jsonpCallback: \"myJsonpCallback\", // The name of the global callback function to be executed
    data: {},
    success: function(data) {
        // This function is executed when the JSONP response is successfully received and parsed
        alert(35643);
        " . str_replace('parent.','',$onl) . "
        console.log(\"Data received:\", data);
        // Process the received data here
    },
    error: function(jqXHR, textStatus, errorThrown) {
        // This function is executed if there's an error during the request
        alert(5643);
        //console.error(\"Error:\", textStatus, errorThrown);
        " . str_replace('parent.','',$onl) . "
        alert(95643);
    }
});
";
}

function somenot($theta) {
  $wds=explode(' ', $theta);
  if (sizeof($wds) > 3) {
    $newta=str_replace($wds[0] . ' ' . $wds[1] . ' ', ' ', $theta);
    return substr($newta, 0, (strlen($newta) - 1 - strlen($wds[-1 + sizeof($wds)])));
  }
  return $theta;
}


if (isset($_GET['baseimage']) && isset($_GET['overlayimage'])) {
  unlink(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'luna_backagain.png');
  $baseimage=str_replace('+',' ',urldecode($_GET['baseimage']));
  $overlayimage=str_replace('//','/',str_replace('./','/',dirname(__FILE__) . DIRECTORY_SEPARATOR . str_replace('+',' ',urldecode($_GET['overlayimage']))));
  $ouroverlayimage=dirname(__FILE__) . DIRECTORY_SEPARATOR . 'luna_backagain.png';
  
  // Thanks to https://github.com/ImageMagick/ImageMagick/discussions/4606 and https://stackoverflow.com/questions/71094480/how-to-execute-multi-line-command-with-shell-exec
  $cmd = <<<CMD
/bin/convert luna_backagain-1.jpeg -alpha off \
\( -clone 0 -color-threshold "srgb(0,0,0)-srgb(0,0,0)" \) \
\( -clone 0 -color-threshold "srgb(220,220,220)-srgb(235,235,235)" \) \
\( -clone 1,2 -compose plus -composite \) \
-delete 1,2 \
-alpha off -quality 2400 -colorspace RGB -compose copy_opacity -composite \
luna_backagain.png
CMD;

  if (isset($_GET['myinnards'])) {
  $partc = <<<CMD
 -alpha off \
\( -clone 0 -color-threshold "srgb(0,0,0)-srgb(0,0,0)" \) \
\( -clone 0 -color-threshold "srgb(220,220,220)-srgb(235,235,235)" \) \
\( -clone 1,2 -compose plus -composite \) \
-delete 1,2 \
-alpha off -quality 2400 -colorspace RGB -compose copy_opacity -composite \
CMD;
  $cmd=str_replace("\n\n"," ",str_replace($partc, str_replace('+',' ',urldecode($_GET['myinnards'])), $cmd));
  $newcmd=$cmd;
  } else if (isset($_POST['myinnards'])) {
  $partc = <<<CMD
 -alpha off \
\( -clone 0 -color-threshold "srgb(0,0,0)-srgb(0,0,0)" \) \
\( -clone 0 -color-threshold "srgb(220,220,220)-srgb(235,235,235)" \) \
\( -clone 1,2 -compose plus -composite \) \
-delete 1,2 \
-alpha off -quality 2400 -colorspace RGB -compose copy_opacity -composite \
CMD;
  $cmd=str_replace("\n\n"," ",str_replace($partc, str_replace('+',' ',urldecode($_GET['myinnards'])), $cmd));
  $newcmd=$cmd;
  }

  $sx="/bin/convert";
  if (file_exists('/usr/local/bin/convert')) {
    $sx="/usr/local/bin/convert";
  }
  $cmd=str_replace("/bin/convert", "" . $sx, str_replace(" luna_backagain-1.jpeg", " " . $overlayimage . "", str_replace("luna_backagain.png", "" . $ouroverlayimage . " 2> " . dirname(__FILE__) . DIRECTORY_SEPARATOR . "luna.bad", $cmd)));

  $sx=shell_exec($cmd); //" /bin/convert " . $overlayimage . " " . $sw . " -alpha off -quality 2400 -colorspace RGB -compose copy_opacity -composite " . $ouroverlayimage . " 2> luna.bad > luna.ok");
  if (!file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'luna_backagain.png')) {
  $exceptthat="<font color=red> ... but errors above cause the fallback you see above</font>";
  //file_put_contents('xx.xx', $sx);
  $derivedvia="&nbsp;a&nbsp;Preview&nbsp;inspired&nbsp;Speech&nbsp;Bubble&nbsp;extracted&nbsp;image&nbsp;regarding&nbsp;could&nbsp;not&nbsp;be&nbsp;created&nbsp;via&nbsp;ImageMagick&nbsp;<font color=red>(" . $sx . file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'luna.bad') . ")</font>&nbsp;from&nbsp;<a download='" . basename(str_replace(dirname(__FILE__) . DIRECTORY_SEPARATOR, './', $overlayimage)) . "' href='" . str_replace(dirname(__FILE__) . DIRECTORY_SEPARATOR, './', $overlayimage) . "?rand=" . rand(0,567876) . "'><img id=imgoverlayimagevia style='border:2px dotted yellow;display:inline-block;width:160px;' src='" . str_replace(dirname(__FILE__) . DIRECTORY_SEPARATOR, './', $overlayimage) . "?rand=" . rand(0,567876) . "'></img></a>";
  $ouroverlayimage=$overlayimage;
  copy($origbase, $origoverlay);
  $ib.=";background-color:yellow";
  $thissw="";
  } else {
  $ouroverlayimage=dirname(__FILE__) . DIRECTORY_SEPARATOR . 'luna_backagain.png';
  $derivedvia="&nbsp;created&nbsp;via&nbsp;ImageMagick&nbsp;from&nbsp;<a download='" . basename(str_replace(dirname(__FILE__) . DIRECTORY_SEPARATOR, './', $overlayimage)) . "' href='" . str_replace(dirname(__FILE__) . DIRECTORY_SEPARATOR, './', $overlayimage) . "?rand=" . rand(0,567876) . "'><img id=imgoverlayimagevia style='border:2px dotted yellow;display:inline-block;width:160px;' src='" . str_replace(dirname(__FILE__) . DIRECTORY_SEPARATOR, './', $overlayimage) . "?rand=" . rand(0,567876) . "'></img></a>";
  }
} else if (isset($_POST['baseimage']) && isset($_POST['overlayimage'])) {
  unlink(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'luna_backagain.png');
  $baseimage=str_replace('+',' ',urldecode($_POST['baseimage']));
  $overlayimage=str_replace('//','/',str_replace('./','/',dirname(__FILE__) . DIRECTORY_SEPARATOR . str_replace('+',' ',urldecode($_POST['overlayimage']))));
  $ouroverlayimage=dirname(__FILE__) . DIRECTORY_SEPARATOR . 'luna_backagain.png';
  
  // Thanks to https://github.com/ImageMagick/ImageMagick/discussions/4606 and https://stackoverflow.com/questions/71094480/how-to-execute-multi-line-command-with-shell-exec
  $cmd = <<<CMD
/bin/convert luna_backagain-1.jpeg -alpha off \
\( -clone 0 -color-threshold "srgb(0,0,0)-srgb(0,0,0)" \) \
\( -clone 0 -color-threshold "srgb(220,220,220)-srgb(235,235,235)" \) \
\( -clone 1,2 -compose plus -composite \) \
-delete 1,2 \
-alpha off -quality 2400 -colorspace RGB -compose copy_opacity -composite \
luna_backagain.png
CMD;

  if (isset($_GET['myinnards'])) {
  $partc = <<<CMD
 -alpha off \
\( -clone 0 -color-threshold "srgb(0,0,0)-srgb(0,0,0)" \) \
\( -clone 0 -color-threshold "srgb(220,220,220)-srgb(235,235,235)" \) \
\( -clone 1,2 -compose plus -composite \) \
-delete 1,2 \
-alpha off -quality 2400 -colorspace RGB -compose copy_opacity -composite \
CMD;
  $cmd=str_replace("\n\n"," ",str_replace($partc, str_replace('+',' ',urldecode($_GET['myinnards'])), $cmd));
  $newcmd=$cmd;
  } else if (isset($_POST['myinnards'])) {
  $partc = <<<CMD
 -alpha off \
\( -clone 0 -color-threshold "srgb(0,0,0)-srgb(0,0,0)" \) \
\( -clone 0 -color-threshold "srgb(220,220,220)-srgb(235,235,235)" \) \
\( -clone 1,2 -compose plus -composite \) \
-delete 1,2 \
-alpha off -quality 2400 -colorspace RGB -compose copy_opacity -composite \
CMD;
  $cmd=str_replace("\n\n"," ",str_replace($partc, str_replace('+',' ',urldecode($_GET['myinnards'])), $cmd));
  $newcmd=$cmd;
  }

  $sx="/bin/convert";
  if (file_exists('/usr/local/bin/convert')) {
    $sx="/usr/local/bin/convert";
  }
  $cmd=str_replace("/bin/convert", "" . $sx, str_replace(" luna_backagain-1.jpeg", " " . $overlayimage . "", str_replace("luna_backagain.png", "" . $ouroverlayimage . " 2> " . dirname(__FILE__) . DIRECTORY_SEPARATOR . "luna.bad" . "", $cmd)));

  $sx=shell_exec($cmd); //" /bin/convert " . $overlayimage . " " . $sw . " -alpha off -quality 2400 -colorspace RGB -compose copy_opacity -composite " . $ouroverlayimage . " 2> luna.bad > luna.ok");
  if (!file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'luna_backagain.png')) {
  $exceptthat="<font color=red> ... but errors above cause the fallback you see above</font>";
  $derivedvia="&nbsp;a&nbsp;Preview&nbsp;inspired&nbsp;Speech&nbsp;Bubble&nbsp;extracted&nbsp;image&nbsp;regarding&nbsp;could&nbsp;not&nbsp;be&nbsp;created&nbsp;via&nbsp;ImageMagick&nbsp;<font color=red>(" . $sx . file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'luna.bad') . ")</font>&nbsp;from&nbsp;<a download='" . basename(str_replace(dirname(__FILE__) . DIRECTORY_SEPARATOR, './', $overlayimage)) . "' href='" . str_replace(dirname(__FILE__) . DIRECTORY_SEPARATOR, './', $overlayimage) . "?rand=" . rand(0,567876) . "'><img id=imgoverlayimagevia style='border:2px dotted yellow;display:inline-block;width:160px;' src='" . str_replace(dirname(__FILE__) . DIRECTORY_SEPARATOR, './', $overlayimage) . "?rand=" . rand(0,567876) . "'></img></a>";
  $ouroverlayimage=$overlayimage;
  copy($origbase, $origoverlay);
  $ib.=";background-color:yellow";
  $thissw="";
  } else {
  $ouroverlayimage=dirname(__FILE__) . DIRECTORY_SEPARATOR . 'luna_backagain.png';
  $derivedvia="&nbsp;created&nbsp;via&nbsp;ImageMagick&nbsp;from&nbsp;<a download='" . basename(str_replace(dirname(__FILE__) . DIRECTORY_SEPARATOR, './', $overlayimage)) . "' href='" . str_replace(dirname(__FILE__) . DIRECTORY_SEPARATOR, './', $overlayimage) . "?rand=" . rand(0,567876) . "'><img id=imgoverlayimagevia style='border:2px dotted yellow;display:inline-block;width:160px;' src='" . str_replace(dirname(__FILE__) . DIRECTORY_SEPARATOR, './', $overlayimage) . "?rand=" . rand(0,567876) . "'></img></a>";
  }
} else {
  unlink(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'luna_backagain.png');
  $overlayimage=dirname(__FILE__) . DIRECTORY_SEPARATOR . 'luna_backagain-1.jpeg';
  $ouroverlayimage=dirname(__FILE__) . DIRECTORY_SEPARATOR . 'luna_backagain.png';
  
  // Thanks to https://github.com/ImageMagick/ImageMagick/discussions/4606 and https://stackoverflow.com/questions/71094480/how-to-execute-multi-line-command-with-shell-exec
  $cmd = <<<CMD
/bin/convert luna_backagain-1.jpeg -alpha off \
\( -clone 0 -color-threshold "srgb(0,0,0)-srgb(0,0,0)" \) \
\( -clone 0 -color-threshold "srgb(220,220,220)-srgb(235,235,235)" \) \
\( -clone 1,2 -compose plus -composite \) \
-delete 1,2 \
-alpha off -quality 2400 -colorspace RGB -compose copy_opacity -composite \
luna_backagain.png
CMD;

  if (isset($_GET['myinnards'])) {
  $partc = <<<CMD
 -alpha off \
\( -clone 0 -color-threshold "srgb(0,0,0)-srgb(0,0,0)" \) \
\( -clone 0 -color-threshold "srgb(220,220,220)-srgb(235,235,235)" \) \
\( -clone 1,2 -compose plus -composite \) \
-delete 1,2 \
-alpha off -quality 2400 -colorspace RGB -compose copy_opacity -composite \
CMD;
  $cmd=str_replace("\n\n"," ",str_replace($partc, str_replace('+',' ',urldecode($_GET['myinnards'])), $cmd));
  $newcmd=$cmd;
  } else if (isset($_POST['myinnards'])) {
  $partc = <<<CMD
 -alpha off \
\( -clone 0 -color-threshold "srgb(0,0,0)-srgb(0,0,0)" \) \
\( -clone 0 -color-threshold "srgb(220,220,220)-srgb(235,235,235)" \) \
\( -clone 1,2 -compose plus -composite \) \
-delete 1,2 \
-alpha off -quality 2400 -colorspace RGB -compose copy_opacity -composite \
CMD;
  $cmd=str_replace("\n\n"," ",str_replace($partc, str_replace('+',' ',urldecode($_GET['myinnards'])), $cmd));
  $newcmd=$cmd;
  }

  $sx="/bin/convert";
  if (file_exists('/usr/local/bin/convert')) {
    $sx="/usr/local/bin/convert";
  }
  $cmd=str_replace("/bin/convert", "" . $sx, str_replace(" luna_backagain-1.jpeg", " " . $overlayimage . "", str_replace("luna_backagain.png", "" . $ouroverlayimage . " 2> " . dirname(__FILE__) . DIRECTORY_SEPARATOR . "luna.bad" . "", $cmd)));

  $sx=shell_exec($cmd); //" /bin/convert " . $overlayimage . " " . $sw . " -alpha off -quality 2400 -colorspace RGB -compose copy_opacity -composite " . $ouroverlayimage . " 2> luna.bad > luna.ok");
  //echo $sx;
  //exit;
  if (!file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'luna_backagain.png')) {
  $exceptthat="<font color=red> ... but errors above cause the fallback you see above</font>";
  $derivedvia="&nbsp;a&nbsp;Preview&nbsp;inspired&nbsp;Speech&nbsp;Bubble&nbsp;extracted&nbsp;image&nbsp;regarding&nbsp;could&nbsp;not&nbsp;be&nbsp;created&nbsp;via&nbsp;ImageMagick&nbsp;<font color=red>(" . $sx . file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'luna.bad') . ")</font>&nbsp;from&nbsp;<a download='" . basename(str_replace(dirname(__FILE__) . DIRECTORY_SEPARATOR, './', $overlayimage)) . "' href='" . str_replace(dirname(__FILE__) . DIRECTORY_SEPARATOR, './', $overlayimage) . "?rand=" . rand(0,567876) . "'><img id=imgoverlayimagevia style='border:2px dotted yellow;display:inline-block;width:160px;' src='" . str_replace(dirname(__FILE__) . DIRECTORY_SEPARATOR, './', $overlayimage) . "?rand=" . rand(0,567876) . "'></img></a>";
  $ouroverlayimage=$overlayimage;
  copy($origbase, $origoverlay);
  $ib.=";background-color:yellow";
  $thissw="";
  } else {
  $derivedvia="&nbsp;created&nbsp;via&nbsp;ImageMagick&nbsp;from&nbsp;<a download='" . basename(str_replace(dirname(__FILE__) . DIRECTORY_SEPARATOR, './', $overlayimage)) . "' href='" . str_replace(dirname(__FILE__) . DIRECTORY_SEPARATOR, './', $overlayimage) . "?rand=" . rand(0,567876) . "'><img id=imgoverlayimagevia style='border:2px dotted yellow;display:inline-block;width:160px;' src='" . str_replace(dirname(__FILE__) . DIRECTORY_SEPARATOR, './', $overlayimage) . "?rand=" . rand(0,567876) . "'></img></a>";
  }
}

echo "<html>
<head>
<script src=\"HTTP://localhost" . $port . "/speech_bubble_overlays.js\" crossorigin=\"anonymous\"></script>
<script src=\"http://api.jquery.com/jquery-wp-content/themes/jquery/js/modernizr.custom.2.6.2.min.js\"></script>
 
        <script src=\"//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js\"></script>
        <script>window.jQuery || document.write(unescape('%3Cscript src=\"//api.jquery.com/jquery-wp-content/themes/jquery/js/jquery-1.9.1.min.js\"%3E%3C/script%3E'))</script>
 
        <script src=\"//api.jquery.com/jquery-wp-content/themes/jquery/js/plugins.js\"></script>
        <script src=\"//api.jquery.com/jquery-wp-content/themes/jquery/js/main.js\"></script>
 
        <script src=\"//use.typekit.net/wde1aof.js\"></script>
        <script>try{Typekit.load();}catch(e){}</script>
        <script type='text/javascript' src='//api.jquery.com/wp-includes/js/comment-reply.min.js?ver=3.8'></script>
<!--link rel='preload' href='HTTP://localhost" . $port . "/" . basename($origoverlay) . "' as='image' type='image/png' crossorigin /-->
<title>ImageMagick Command Line Interfacer - RJM Programming - October, 2025</title>
<script type=text/javascript>
 var evtcnt=-1, sbo=null, zzqhr=null, oops=false;
 var sofarport='" . $port . "';
 var vsval='';
 var altval=jsomenot(decodeURIComponent('" . $thissw . "').replace(/\+/g,' '));
 var storedval=decodeURIComponent(('' + window.localStorage.getItem('speech_bubble_overlays_success_list')).replace(/^undefined$/g,'').replace(/^null$/g,''));
 
  //alert('altval=' + altval + ' and storedval=' + storedval);

function jsomenot(theta) {
  return theta;
}

 
// Define the global callback function if jsonpCallback is specified
function myJsonpCallback(data) {
    console.log(\"Data received via custom callback:\", data);
    // This function can also process the data, or you can rely solely on the 'success' callback in $.ajax()
}

 function veturls() {
   if (document.getElementById('baseimage').value.trim().indexOf('.') == -1 || document.getElementById('overlayimage').value.trim().indexOf('.') == -1) {
     alert('Not image URLs.');
     return false;
   } else if (document.getElementById('overlayimage').value.trim().indexOf('//') != -1) {
     alert('Overlay images need to be relative URLs.');
     return false;
   }
   return true;
 }
 
 function dotitle() {
   var nc='" . ($newcmd == '' ? '' : 'x') . "', newta='';
   if (nc != '') { newta=document.getElementById('myselect').outerHTML + document.getElementById('myinnards').outerHTML.split('>')[0] + '>';  document.getElementById('mydiv').innerHTML=newta + '" . somenot(str_replace("\n","' + String.fromCharCode(32) + '",str_replace("\r","' + String.fromCharCode(32) + '",str_replace("\r\n","' + String.fromCharCode(32) + '",str_replace("'","' + String.fromCharCode(39) + '",$newcmd))))) . "</textarea>';  }
   document.body.title=document.getElementById('myta').value;
   
 }
   
 function prestateChanged() {
  if (zzqhr.readyState == 4) {
   if (zzqhr.status == 200) {
     alert('yay');
     evtcnt=2;
   }
  }
 }
  
  function errmampshowStuff(evt) {
  evtcnt++;
  }
  
  function errmampshowImg(evt) {
  evtcnt++;
  //alert('here');
  console.warn(evt);
  }
  
  function preajaxit(inimgo) {
     var spares='', origst=storedval;
     if (inimgo == null) {
       vsval=document.getElementById('myinnards').value;
 //alert('Altval=' + altval + ' and Storedval=' + storedval);
       if (altval.trim() != '' || storedval.trim() != '') {
 //alert('Altval=' + altval + ' and storedval=' + storedval);
         if (altval.trim() != vsval.trim() && altval.trim() != '') {
           if (storedval.trim().indexOf(encodeURIComponent(altval.trim())) == -1) {
              if (storedval.trim() == '') {
                storedval=encodeURIComponent(altval);
              } else {
                storedval+=encodeURIComponent('@!@') + encodeURIComponent(altval);
              }
           }
         }
         if (storedval != '') {
           if (storedval != origst) {
             if (origst != '') {
               window.localStorage.removeItem('speech_bubble_overlays_success_list');
             }
 //alert('altVal=' + altval + ' and storedval=' + storedval);
             window.localStorage.setItem('speech_bubble_overlays_success_list', storedval);
           }
 //alert('aLtVal=' + altval + ' and storedval=' + storedval);
           var svals=encodeURIComponent(storedval).split(encodeURIComponent('@!@'));
           for (var ivs=0; ivs<svals.length; ivs++) {
             if (ivs == 0) {
               document.getElementById('myselect').style.display='block';
             } 
             //alert(svals[ivs]);
             spares=decodeURIComponent(svals[ivs]);
             while (spares.indexOf(String.fromCharCode(13) + String.fromCharCode(10)) != -1) {
               spares=spares.replace(String.fromCharCode(13) + String.fromCharCode(10), ' ');
             }
             while (spares.indexOf(String.fromCharCode(10)) != -1) {
               spares=spares.replace(String.fromCharCode(10), ' ');
             }
             while (spares.indexOf(String.fromCharCode(13)) != -1) {
               spares=spares.replace(String.fromCharCode(13), ' ');
             }
             if (spares.trim() != '') {
             document.getElementById('myselect').innerHTML+='<option value=\"' + svals[ivs] + '\">' + spares + '</option><option value=\"Forget ' + svals[ivs] + '\"> ... or forget above .</option>';
             }
           }
         }
       }
     }
     if (document.URL.toLowerCase().indexOf('localhost') == -1) {
     setTimeout(maybehthree, 6000);
     if (inimgo) {
     evtcnt=2;
     } else if (7 == 7) {
     //alert('' + (typeof okaytogo));
     //if (typeof okaytogo === 'function') {  alert('yay'); }
     " . $innards . "
     if (evtcnt == -1) { evtcnt=0; }
     zzqhr = new XMLHttpRequest();
     zzqhr.onreadystatechange=prestateChanged;
     zzqhr.addEventListener('error', errmampshowStuff);
     zzqhr.addEventListener('timeout', errmampshowStuff);
     //alert(456);
     zzqhr.open('get', 'HTTP://localhost" . $port . "/speech_bubble_overlays.php', true);
     //zzqhr.open('get', document.getElementById('lfi').src, true);
     zzqhr.send();
     //alert(4565);
     }
     }
  }
  
  function maybehthreeforce() {
         //document.getElementById('locbutone').style.display='inline-block';
  }
  
  function maybehthree() {
     if (evtcnt >= 0 && evtcnt < 3) {
      document.getElementById('locbutone').style.display='none';
      if (5 == 7) {
         if (1 == 1) {
         document.getElementById('locbutone').style.display='inline-block';
         } else {
         document.getElementsByTagName('h3')[0].innerHTML+=' <button style=display:inline-block; id=locbutone title=\"Can click for local web server Speech Bubble ImageMagick interfacing?\" onclick=tosb();> ... Local Web Server &#x1F5A5;&#xFE0F;</button>';
         }
      }
     } else if (evtcnt > 8) {
      //if (document.getElementById('locbutone')) {
      //alert(1);
      //}
         if (!oops) { document.getElementById('locbutone').style.display='inline-block'; }
      //if (document.getElementById('locbutone')) {
      //alert(11);
      //}
     }
     //evtcnt=-1;
     if (oops) { 
       document.getElementById('dl').innerHTML=document.getElementById('dl').innerHTML.replace('inline-block','none'); 
       if (document.getElementById('locbutone')) { 
         document.getElementById('locbutone').style.display='none'; 
       } 
       if (window.parent) {
       parent.document.getElementById('dl').innerHTML=parent.document.getElementById('dl').innerHTML.replace('inline-block','none'); 
       if (parent.document.getElementById('locbutone')) { 
         parent.document.getElementById('locbutone').style.display='none'; 
       } 
       }
     } else {
         if ('" . explode(';',$ib)[0] . "' == 'none') {
           location.href=(document.URL + '&showlocal=' + encodeURIComponent(sofarport)).replace('.php&','.php?');
         }
     }
  }
  
  function tosb() {
    if (!oops) {
    sbo=window.open('HTTP://localhost" . $port . "/speech_bubble_overlays.php?' + (document.URL + '?').split('?')[1], '_blank', 'top=0,left=0,width=' + screen.width + ',height=' + screen.height);
    } else {
    alert('Sorry, not installed.');
    }
  }
     
  function checkif(iois) {
    evtcnt++;
    //alert(0);
    var aconto = (iois.contentWindow || iois.contentDocument);
    //alert(1);
    evtcnt++;
    if (aconto != null) {
    evtcnt++;
    //alert(4);
       try {
       if (aconto.document) { evtcnt++; aconto = aconto.document; evtcnt++; }
       } catch(errts) {
       }
    //alert(44);
       evtcnt++;
       if (aconto.body != null) {
    //alert('444 ' + aconto.body.innerHTML);
         if (aconto.body.innerHTML.indexOf('ion ok' + 'aytogo') == -1) {
         if (sofarport == ':8888') {
           sofarport=':7888';
           document.getElementById('showlocal').value=sofarport;
           iois.src=iois.src.replace(':8888', sofarport);
           return '';
         } else if (sofarport == ':7888') {
           sofarport=':8890';
           document.getElementById('showlocal').value=sofarport;
           iois.src=iois.src.replace(':7888', sofarport);
           return '';
         } else if (sofarport == ':8890') {
           sofarport=':80';
           document.getElementById('showlocal').value=sofarport;
           iois.src=iois.src.replace(':8890', sofarport);
           return '';
         } else if (sofarport == ':80') {
           sofarport='';
           document.getElementById('showlocal').value=sofarport;
           iois.src=iois.src.replace(':80', sofarport);
           return '';
         } 
         oops=true;
         if ('" . explode(';',$ib)[0] . "' == 'inline-block') {
           location.href=(document.URL + '&hidelocal=y').replace('.php&','.php?');
         }
         //parent.document.getElementById('locbutone').style.display='none';
    //alert('4444 ' + aconto.body.innerHTML);
         } else {
         evtcnt++;
         evtcnt+=8;
      //   alert('Here');
    //setTimeout(maybehthreeforce, 8000);
         //setTimeout(function(){
         if (1 == 1) {
         if (!oops) { document.getElementById('locbutone').style.display='inline-block'; }
         } else {
         document.getElementsByTagName('h3')[0].innerHTML+=' <button style=display:inline-block; id=locbutone title=\"Can click for local web server Speech Bubble ImageMagick interfacing?\" onclick=tosb();> ... macOS MAMP</button>';
         }
         //}, 5000);
        // alert('HerE');
         }
       } else {
         document.getElementById('locbutone').style.display='none';
       }
    }
    setTimeout(maybehthree, 6000);
  }
  
  function yehbut(inov) {
    if (inov.indexOf('Forget ') == 0) {
       var valstored=(('' + window.localStorage.getItem('speech_bubble_overlays_success_list')).replace(/^undefined$/g,'').replace(/^null$/g,''));
       if (valstored.indexOf(inov.substring(7)) != -1) {
         if (valstored.indexOf(encodeURIComponent('@!@') + inov.substring(7)) != -1) {
           valstored=valstored.replace(encodeURIComponent('@!@') + inov.substring(7), '');
         } else if (valstored.indexOf(inov.substring(7) + encodeURIComponent('@!@')) != -1) {
           valstored=valstored.replace(inov.substring(7) + encodeURIComponent('@!@'), '');
         } else {
           valstored=valstored.replace(inov.substring(7), '');
         }
         window.localStorage.removeItem('speech_bubble_overlays_success_list');
         window.localStorage.setItem('speech_bubble_overlays_success_list', valstored);
       }
       return encodeURIComponent(document.getElementById('myinnards').value);
    } 
    return inov;
  }

</script>
<style>
 * { margin: 0 0 0 0;
     padding: 0 0 0 0;
    }
</style>
</head>
<body onload=' " . $onl . " preajaxit(null); setTimeout(dotitle,4000);' title='' style='background:URL(" . $baseimage . "?rand=" . rand(0,567876) . ");background-size:auto;background-repeat:no-repeat;background-blend-mode:difference;'>
<table><tr><td id=tdleft><div>
<img src='" . str_replace(dirname(__FILE__) . DIRECTORY_SEPARATOR, './', $ouroverlayimage) . "?rand=" . rand(0,567876) . "'></img>
</div></td><td id=tdright style=vertical-align:top;>
<h1>Preview Default Coloured <a target=_blank style='text-decoration:none;cursor:pointer;' title='ImageMagick software suite information' href='https://www.imagemagick.org/'>ImageMagick</a> Speech Bubble Extraction and Background Image Overlay</h1><br>
<h3>RJM Programming - October, 2025 <div id=dl style=display:inline-block;><button style=display:" . (strpos($_SERVER['SERVER_NAME'], 'localhost') !== false ? 'none' : $ib) . "; id=locbutone title=\"Can click for local web server Speech Bubble ImageMagick interfacing?\" onclick=tosb();> ... Local Web Server &#x1F5A5;&#xFE0F;</button></div></h3><br>
<h4>Thanks to <a target=_blank href='//github.com/ImageMagick/ImageMagick/discussions/4606'>https://github.com/ImageMagick/ImageMagick/discussions/4606</a> and <a target=_blank href='//stackoverflow.com/questions/71094480/how-to-execute-multi-line-command-with-shell-exec'>https://stackoverflow.com/questions/71094480/how-to-execute-multi-line-command-with-shell-exec</a></h4><br><br>
<form style=font-size:24px; onsubmit='return veturls();' action='./speech_bubble_overlays.php' method=GET>" . $locbit . "
Base image URL: <input id=baseimage name=baseimage type=text value='" . $baseimage . "'></input>&nbsp;<a download='" . basename($baseimage) . "' href='" . $baseimage . "?rand=" . rand(0,567876) . "'><img id=imgbaseimage style='border:2px dotted yellow;display:inline-block;width:160px;' src='" . $baseimage . "?rand=" . rand(0,567876) . "'></img></a></br>
Overlay image URL: <input id=overlayimage name=overlayimage type=text value='" . str_replace(dirname(__FILE__) . DIRECTORY_SEPARATOR, './', $overlayimage) . "'></input>&nbsp;<a download='" . basename(str_replace(dirname(__FILE__) . DIRECTORY_SEPARATOR, './', $ouroverlayimage)) . "' href='" . str_replace(dirname(__FILE__) . DIRECTORY_SEPARATOR, './', $ouroverlayimage) . "?rand=" . rand(0,567876) . "'><img id=imgoverlayimage style='border:2px dotted yellow;display:inline-block;width:160px;' src='" . str_replace(dirname(__FILE__) . DIRECTORY_SEPARATOR, './', $ouroverlayimage) . "?rand=" . rand(0,567876) . "'></img></a>" . $derivedvia . "</br><br>
ImageMagick Switches <font size=1>(acting on last image above to create middle image above" . $exceptthat . ")</font> ... <br>
<div id=mydiv><select onchange=\"if (this.value.length != 0) { document.getElementById('myinnards').name=document.getElementById('myinnards').id; document.getElementById('myinnards').value=decodeURIComponent(yehbut(this.value));  }\" id=myselect style=display:none;><option value=''>Optionally select a successful switch set from the past ...</option></select><textarea style=width:70%;max-height:150px;background-color:rgb(230,230,230); rows=6 onblur='this.name=this.id;' onchange='this.name=this.id;' id=myinnards> -alpha off \\
\\( -clone 0 -color-threshold \"srgb(0,0,0)-srgb(0,0,0)\" \\) \\
\\( -clone 0 -color-threshold \"srgb(220,220,220)-srgb(235,235,235)\" \\) \\
\\( -clone 1,2 -compose plus -composite \\) \\
-delete 1,2 \\
-alpha off -quality 2400 -colorspace RGB -compose copy_opacity -composite \\
</textarea></div>
<input style='font-size:24px;padding:5 5 5 5;background-color:lightgreen;margin-top:4px;' type=submit value=Display></input>
</form>
</td></table>
<textarea style=display:none; id=myta>" . str_replace(dirname(__FILE__) . DIRECTORY_SEPARATOR, "", $cmd) . "</textarea>
<!--iframe id=lfi style=display:none; onerror='errmampshowImg(event);' onload='checkif(this);' src='HTTP://localhost" . $port . "/" . basename($origoverlay) . "'></iframe-->
<iframe id=lfi style=display:none; onerror='errmampshowImg(event);' " . $dataize . "onload='checkif(this);' src='HTTP://localhost" . $port . "/speech_bubble_overlays.js'></iframe>
" . $ifstuff . "
        <!--script>
 " . $innards . "
        </script-->
        <!--script type='text/javascript' src='HTTP://localhost" . $port . "/speech_bubble_overlays.php?callback=myCallback'></script-->
</body>
</html>";
?>
