<?php
// emoji_image.php
// Get helped out by Google PageSpeed to create an emoji image
// November, 2020

$gei="";
if (isset($_GET['emoji_textarea_image'])) {
    $gei=str_replace("+"," ",urldecode($_GET['emoji_textarea_image']));
}

if (isset($_GET['emoji_rm_image'])) {
  if (file_exists('emoji_image.txt')) {
  $etext=file_get_contents('emoji_image.txt');
  if ($etext != trim($etext) && trim($etext) == str_replace("+"," ",urldecode($_GET['emoji_rm_image']))) {
  unlink('emoji_image.txt');
  }
  }
  //exec("sleep 50; rm -f " . dirname(__FILE__) . "/emoji_image.txt");
  exit;
} else if (isset($_GET['emoji_mr_image'])) {
  exit;
} else if (file_exists('emoji_image.txt')) {
  $etext=file_get_contents('emoji_image.txt');
  if ($etext != trim($etext)) {
  unlink('emoji_image.txt');
  } else {
  file_put_contents('emoji_image.txt', $etext . ' ');
  }
  $etext=trim($etext);
  if (substr(($etext . "z"),0,1) <= "9") {
  echo "<html><head><meta charset='utf-8'></head><body><p style='font-size:256px;'>&#" . str_replace("\n","<br>",str_replace(".", ";&#", str_replace("+"," ",$etext))) . ";</p></body></html>";
  } else {
  echo "<html><head><meta charset='utf-8'></head><body><p style='font-size:256px;'>" . str_replace("\n","<br>",$etext) . "</p></body></html>";
  }
} else if (trim($gei) != "" || isset($_GET['emoji_image'])) {
  if (trim($gei) == "") {
    $gei=str_replace("+"," ",urldecode($_GET['emoji_image']));
  } else if (isset($_GET['emoji_image'])) {
    $pregei=str_replace("+"," ",urldecode($_GET['emoji_image']));
    if (trim($pregei) != "") {
      $pregei.="\n\n" . $gei;
      $gei=$pregei;
    }
  }
  file_put_contents('emoji_image.txt',  str_replace(";","",str_replace("&#","",str_replace(";&#",".",$gei)))  );
  sleep(5);
  // Thanks to https://stackoverflow.com/questions/28644340/how-do-i-get-base64-encoded-image-from-clipboard-in-internet-explorer/28799619 below ...
  echo "<html><head>
<title>Emoji Image Creator - RJM Programming - November, 2020 ... thanks to Google PageSpeed</title>
<meta charset='utf-8'>
<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js\"></script>
<script type='text/javascript'>
$(document).ready(function() {
  $('#editor').on('paste', function(e) {
    var orgEvent = e.originalEvent;
    for (var i = 0; i < orgEvent.clipboardData.items.length; i++) {
      if (orgEvent.clipboardData.items[i].kind == \"file\" && orgEvent.clipboardData.items[i].type == \"image/png\") {
        var imageFile = orgEvent.clipboardData.items[i].getAsFile();
        var fileReader = new FileReader();
        fileReader.onloadend = function() {
          $('#result').html(fileReader.result);
        }
        fileReader.readAsDataURL(imageFile);
        break;
      }
    }
  });
});

function checkfordel() {
  document.getElementById('mydeler').src=document.getElementById('mydeler').src.replace('emoji_mr_image=', 'emoji_rm_image=') + 'x';
  setTimeout(checkfordel,10000);
}

function doemail() {
  var emis=prompt('Please enter email address to send Email To?  (all lowercase uses client emailer where you should Paste into body of email and all numbers we shall assume you are sending an SMS, else will send an Inline HTML Email (that will not work for all email clients))', '');
  if (emis == null) { emis=''; }
  if (emis.indexOf('@') != -1 || (emis.trim() != '' && emis.replace(/0/g,'').replace(/1/g,'').replace(/2/g,'').replace(/3/g,'').replace(/4/g,'').replace(/5/g,'').replace(/6/g,'').replace(/7/g,'').replace(/8/g,'').replace(/9/g,'') == '')) {
  if (emis.toLowerCase() == emis) {
  if (emis.indexOf('@') != -1) {
  document.getElementById('aemail').href='mailto:' + emis + '?subject=' + encodeURIComponent('My Emoji Image(s)') + '&body=' + encodeURIComponent(' ... via http://www.rjmprogramming.com.au/PHP/emoji_image.php ...' + String.fromCharCode(10));
  document.getElementById('aemail').click();
  } else {
  document.getElementById('aemail').href='sms:' + emis + '&body=' + encodeURIComponent('My Emoji Image(s)' + ' ... via http://www.rjmprogramming.com.au/PHP/emoji_image.php ... ' + String.fromCharCode(10));
  document.getElementById('aemail').click();
  }
  } else {
  var xzhr = new XMLHttpRequest();
  var xform=new FormData();
  xform.append('inline', '');
  xform.append('to', emis);
  xform.append('subject', 'My Emoji Image(s)');
  xform.append('body', '<body><a target=_blank title=\"Emoji Image Creator\" href=\"http://www.rjmprogramming.com.au/PHP/emoji_image.php\"><img src=\"' + document.getElementById('result').innerHTML + '\" title=\"Emoji Image(s)\"></img></a></body>');
  xzhr.open('post', '//www.rjmprogramming.com.au/HTMLCSS/emailhtml.php', true);
  xzhr.send(xform);
  }
  }
}
</script>
<style>
td { 
  vertical-align: top; 
}

#editor {
  width: 500px;
  min-height: 40px;
  border: solid 1px gray;
  padding: 4px;
}

#resultcnt {
  width: 100%;
  margin-top: 16px;
}

#result {
  display: block;
  max-width: 90%;
  margin: 16px 0 32px 0;
  font-size: 12px;
  color: blue;
  overflow: visible;
  word-break: break-all;
}

textarea {
  font-family: Courier New;
}

input {
  font-family: Courier New;
}
</style>
</head>
<body onload='window.open(\"https://developers.google.com/speed/pagespeed/insights/?url=http%3A%2F%2Fwww.rjmprogramming.com.au%2FPHP%2Femoji_image.php&tab=mobile\",\"_blank\",\"top=50,left=50,height=900,width=1300\"); setTimeout(checkfordel,10000);  '><p style='font-size:256px;'>&#" . str_replace(".", ";&#", str_replace("+"," ",$etext)) . ";</p>
<h1 style='color:red;position:absolute;top:0px;left:400px;'>Copy (Image) from Google PageSpeed <br>(thanks, and note to toggle between mobile and desktop tabs perhaps) <br>and Paste via macOS Alt-PrtScr Below (for Data URI as well) <br>or to an Image Editor that can Crop out Extraneous White Parts <br>or Resize Image and Save <br>(ready for Border Image or Background Image or just Image purposes)</h1>
<table style='position:absolute;top:280px;left:0px;' border=1><tr><th>Emoji Image <font size=1><a target=_blank href='./emoji_image.php' title='Back to defining emojis'>Creator</a></font><br><font size=1>(Paste Below)</font></th><th>Data URI</th></tr><tr><td><div id='editor' contenteditable=true></div></td><td>
<div id='resultcnt'>Copied image src (that you can <a style='cursor:pointer;text-decoration:underline;' onclick='doemail();' title='Email'>Email &#128231;</a>):<br>
  <div id='result'></div>
</div></td></table><iframe id=mydeler style='display:none;' src='./emoji_image.php?emoji_mr_image=" . urlencode(str_replace(";","",str_replace("&#","",str_replace(";&#",".",$gei)))) . "&suff=x'></iframe>  
<a id=aemail href='mailto:?subject=" . str_replace("+","%20",urlencode('My Emoji Image(s)')) . "&body=' style=display:none;>Email</a></body></html>";
  //$eresult=file_get_contents("https://developers.google.com/speed/pagespeed/insights/?url=http%3A%2F%2Fwww.rjmprogramming.com.au%2FPHP%2Femoji_image.php");
  //file_put_contents('emojiimage.txt', $eresult);
  //exec("sleep 15; rm -f " . dirname(__FILE__) . "/emoji_image.txt");
} else {
  echo "<html><head><title>Emoji Image Creator - RJM Programming - November, 2020 ... thanks to Google PageSpeed</title><meta charset='utf-8'></head><body style='background-color:#e0e0e0;' onload=' if (window.top == window.self) { document.getElementById(\"emoji_image\").focus(); } '><h1 title='Copy (Image) from Google PageSpeed (thanks, and note to toggle between mobile and desktop tabs perhaps) and Paste via macOS Alt-PrtScr Below (for Data URI as well) or to an Image Editor that can Crop out Extraneous White Parts or Resize Image and Save (ready for Border Image or Background Image or just Image purposes)'>Emoji Image Creator</h1><h3>Thanks to <a target=_blank title='Google PageSpeed' href='https://developers.google.com/speed/pagespeed/insights/'>Google PageSpeed</a> and <a target=_blank title='Useful link, thanks' href='https://stackoverflow.com/questions/28644340/how-do-i-get-base64-encoded-image-from-clipboard-in-internet-explorer/28799619'>Stack Overflow</a> and <a target=_blank title='jQuery' href='https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'>jQuery</a></h3><form method=GET action=./emoji_image.php><input type=text name=emoji_image id=emoji_image value='' placeholder='Emoji CodePoint(s) here (or macOS Control-Command-Space) eg. 127462.127482 is compound Emoji' style='width:99%;' required></input><br><br><textarea onchange=\"if (this.value.trim().length > 0 && document.getElementById('emoji_image').value.trim().length == 0) { document.getElementById('emoji_image').value=' '; } \" name=emoji_textarea_image id=emoji_textarea_image value='' placeholder=' ... and/or rows of Emoji CodePoint(s) here (or macOS Control-Command-Space) eg. 127462.127482 is compound Emoji' cols=120 rows=10></textarea><br><br><input style=background-color:yellow; type=submit value='Create Image'></input></form></body></html>"; 
}
?>
