<?php
// watermarking.php
// Thanks to http://php.net/manual/en/image.examples.merged-watermark.php
// Load the stamp and the photo to apply the watermark to

$opacity="50";
if (isset($_GET['opacity'])) {
  $opacity=urldecode($_GET['opacity']);
} else if (isset($_POST['opacity'])) {
  $opacity=urldecode($_POST['opacity']);
}

$wis="100";
$toplinewording='libGD';
$bottomlinewording='(c) 2007-9';
if (isset($_GET['toplinewording'])) {
  $toplinewording=urldecode($_GET['toplinewording']);
  if (strlen($toplinewording) > strlen($bottomlinewording)) $wis='' . strlen($toplinewording) * 12;
} else if (isset($_POST['toplinewording'])) {
  $toplinewording=urldecode($_POST['toplinewording']);
  if (strlen($toplinewording) > strlen($bottomlinewording)) $wis='' . strlen($toplinewording) * 12;
}

if (isset($_GET['bottomlinewording'])) {
  $bottomlinewording=urldecode($_GET['bottomlinewording']);
  if (strlen($bottomlinewording) > strlen($toplinewording)) $wis='' . strlen($bottomlinewording) * 12;
} else if (isset($_POST['bottomlinewording'])) {
  $bottomlinewording=urldecode($_POST['bottomlinewording']);
  if (strlen($bottomlinewording) > strlen($toplinewording)) $wis='' . strlen($bottomlinewording) * 12;
}

$imis='photo.jpeg';
if (isset($_GET['image'])) {
  $imis=urldecode($_GET['image']);
  $iimis=strtolower(explode("/",$imis)[-1 + sizeof(explode("/",$imis))]);
  if (strpos($iimis, ".jp") !== false) {
    $im = imagecreatefromjpeg($imis);
  } else if (strpos($iimis, ".png") !== false) {
    $im = imagecreatefrompng($imis);
  } else if (strpos($iimis, ".gif") !== false) {
    $im = imagecreatefromgif($imis);
  } else {
    $im = imagecreatefromstring(file_get_contents($imis));
  }
} else if (isset($_POST['image'])) {
  $imis=urldecode($_POST['image']);
  $iimis=strtolower(explode("/",$imis)[-1 + sizeof(explode("/",$imis))]);
  if (strpos($iimis, ".jp") !== false) {
    $im = imagecreatefromjpeg($imis);
  } else if (strpos($iimis, ".png") !== false) {
    $im = imagecreatefrompng($imis);
  } else if (strpos($iimis, ".gif") !== false) {
    $im = imagecreatefromgif($imis);
  } else {
    $im = imagecreatefromstring(file_get_contents($imis));
  }
} else if (12 == 12) {
  echo "<!doctype html>
  <html>
  <head>
  <title>Watermarking Existant Image - RJM Programming - April, 2018 ... thanks to https://php.net/manual/en/image.examples.merged-watermark.php</title>
  </head>
  <body style='background-color:#f0f0f0;'>
  <h1>Watermarking Existant Image</h1>
  <h3>RJM Programming</h3>
  <h3>April, 2018</h3>
  <h4>Thanks to <a target=_blank title='Useful link' href='//php.net/manual/en/image.examples.merged-watermark.php'>https://php.net/manual/en/image.examples.merged-watermark.php</a></h4>
  <form action='./watermarking.php' method='GET'>
  Image filename or URL: <input onblur=\" document.getElementById('myimg').src=this.value; document.getElementById('mydiv').style.display='block'; \" name='image' type='text' value=''></input><br>
  Watermark opacity 0 to 100: <input name='opacity' type='number' value='50' min='0' max=100' step='1'></input><br>
  Watermark Wording Top line: <input name='toplinewording' type='text' value='Top line'></input><br>
  Watermark Wording Bottom line: <input name='bottomlinewording' type='text' value='Bottom line'></input><br><br>
  <input style='background-color:yellow;' type='submit' value='Watermark (if successful, can Save Image As and use Back button to return here)'></input>
  </form>
  <br><div id='mydiv' style='position:absolute;top:0px;left:0px;display:none;width:100%;height:100vh;z-index:-5;opacity:0.4;'><img id='myimg' src=''></img></div>
  </html>
  ";
  exit;
} else {
  $im = imagecreatefromjpeg($imis);
}

$imis=explode("/",$imis)[-1 + sizeof(explode("/",$imis))];
$imwillbe='watermarked_' . str_replace("." . explode(".",$imis)[-1 + sizeof(explode(".", $imis))], ".png", $imis);


// First we create our stamp image manually from GD
$stamp = imagecreatetruecolor($wis, 70);
imagefilledrectangle($stamp, 0, 0, (-1 + $wis), 69, 0x0000FF);
imagefilledrectangle($stamp, 9, 9, (-10 + $wis), 60, 0xFFFFFF);
imagestring($stamp, 5, 20, 20, $toplinewording, 0x0000FF);
imagestring($stamp, 3, 20, 40, $bottomlinewording, 0x0000FF);

// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Merge the stamp onto our photo with an opacity of 50%
imagecopymerge($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp), $opacity);

// Save the image to file and free memory
while (file_exists($imwillbe)) {
 $imwillbe='watermarked_' . str_replace("." . explode(".",$imwillbe)[-1 + sizeof(explode(".",$imwillbe))], ".png", $imwillbe);
}
imagepng($im, $imwillbe); //'photo_stamp.png');
imagedestroy($im);

header('Content-Type: image/png');
echo file_get_contents($imwillbe);
unlink($imwillbe);
exit;
?>
