<?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

$cmd="";
$baseimage='./luna_upstairs.jpg';
$overlayimage='./luna_backagain.png';
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;

  $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')) {
  $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;(" . file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'luna.bad') . ")&nbsp;from&nbsp;<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>";
  $ouroverlayimage=$overlayimage;
  } else {
  $ouroverlayimage=dirname(__FILE__) . DIRECTORY_SEPARATOR . 'luna_backagain.png';
  $derivedvia="&nbsp;created&nbsp;via&nbsp;ImageMagick&nbsp;from&nbsp;<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>";
  }
} 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;

  $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')) {
  $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;(" . file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'luna.bad') . ")&nbsp;from&nbsp;<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>";
  $ouroverlayimage=$overlayimage;
  } else {
  $ouroverlayimage=dirname(__FILE__) . DIRECTORY_SEPARATOR . 'luna_backagain.png';
  $derivedvia="&nbsp;created&nbsp;via&nbsp;ImageMagick&nbsp;from&nbsp;<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>";
  }
} 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;

  $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')) {
  $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;(" . file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'luna.bad') . ")&nbsp;from&nbsp;<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>";
  $ouroverlayimage=$overlayimage;
  } else {
  $derivedvia="&nbsp;created&nbsp;via&nbsp;ImageMagick&nbsp;from&nbsp;<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>";
  }
}

echo "<html>
<head>
<script type=text/javascript>

 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() {
   document.body.title=document.getElementById('myta').value;
 }

</script>
<style>
 * { margin: 0 0 0 0;
     padding: 0 0 0 0;
    }
</style>
</head>
<body onload=dotitle(); 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 ImageMagick Speech Bubble Extraction and Background Image Overlay</h1><br>
<h3>RJM Programming - October, 2025</h3><br>
<h4>Thanks to https://github.com/ImageMagick/ImageMagick/discussions/4606 and https://stackoverflow.com/questions/71094480/how-to-execute-multi-line-command-with-shell-exec</h4><br><br>
<form style=font-size:24px; onsubmit='return veturls();' action='./speech_bubble_overlays.php' method=GET>
Base image URL: <input id=baseimage name=baseimage type=text value='" . $baseimage . "'></input>&nbsp;<img id=imgbaseimage style='border:2px dotted yellow;display:inline-block;width:160px;' src='" . $baseimage . "?rand=" . rand(0,567876) . "'></img></br>
Overlay image URL: <input id=overlayimage name=overlayimage type=text value='" . str_replace(dirname(__FILE__) . DIRECTORY_SEPARATOR, './', $overlayimage) . "'></input>&nbsp;<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>" . $derivedvia . "</br><br>
<input style='font-size:24px;padding:5 5 5 5;background-color:lightgreen;' type=submit value=Display></input>
</form>
</td></table>
<textarea style=display:none; id=myta>" . str_replace(dirname(__FILE__) . DIRECTORY_SEPARATOR, "", $cmd) . "</textarea>
</body>
</html>";
?>
