<?php
// tutorial_to_animated_gif.php
// RJM Programming
// September, 2015
// Thanks to http://www.jeroenvanwissen.nl/weblog/php/howto-generate-animated-gif-with-php

$icnt=1;
$moreh1="";
$imagegif="";
$imagegifandthen="";
$delay=40;
$agifphp="<?php

header ('Content-type:image/gif');
include('GIFEncoder.class.php');
";

$eachone="

\$text='';

// Open the first source image and add the text.
\$image = imagecreatefromjpeg('source01.jpg');
\$text_color = imagecolorallocate(\$image, 200, 200, 200);
imagestring(\$image, 5, 5, 5,  \$text, \$text_color);

// Generate GIF from the $image
// We want to put the binary GIF data into an array to be used later,
//  so we use the output buffer.
ob_start();
imagegif(\$image);
\$frames[]=ob_get_contents();
\$framed[]=40; // Delay in the animation.
ob_end_clean();

// And again..
";

$lastbit="
// Generate the animated gif and output to screen.
\$gif = new GIFEncoder(\$frames,\$framed,0,2,0,0,0,'bin');
echo \$gif->GetAnimation();

?>
"; 

$lastbitfrom="echo \$gif->GetAnimation();";
$lastbitto="\$fp = fopen('animegif.gif', 'w'); 
\$data = \$gif->GetAnimation(); 
\$dataUri = 'data:image/gif;base64,' . base64_encode(\$data);
fwrite(\$fp, \$data); 
fclose(\$fp);
\$fp = fopen('animegif.html', 'w'); 
fwrite(\$fp, '<!doctype html><html><body><h1>Data URI version below<h1><br><h4> ... via web browser (Windows right click, Mac OS X two finger gesture ...</h4><br><img src=' . \"\\n\" . \$dataUri . \"\\n\" . ' title=DataURI></img></body></html>'); 
fclose(\$fp);";
$agtext="";
$url="";

function server_remote_addr() {
    $rma = $_SERVER['REMOTE_ADDR'];
    $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    // you can add different browsers with the same way ..
    if(preg_match('/(chromium)[ \/]([\w.]+)/', $ua))
            $rma = '000000'.$rma;
    elseif(preg_match('/(chrome)[ \/]([\w.]+)/', $ua))
            $rma = '00000'.$rma;
    elseif(preg_match('/(safari)[ \/]([\w.]+)/', $ua))
            $rma = '0000'.$rma;
    elseif(preg_match('/(opera)[ \/]([\w.]+)/', $ua))
            $rma = '000'.$rma;
    elseif(preg_match('/(msie)[ \/]([\w.]+)/', $ua))
            $rma = '00'.$rma;
    elseif(preg_match('/(mozilla)[ \/]([\w.]+)/', $ua))
            $rma = '0'.$rma;
    return str_replace(":", "_", $rma);
}

function analyze($url) {
 //$aprefix="../";
 $aprefix="";
 if (strpos($url, "//") === false && strpos($_SERVER['SERVER_NAME'], "rjmprogramming.com.au") !== false) return $aprefix;
 if (strpos($url, "//") === false && strpos($_SERVER['SERVER_NAME'], "localhost") !== false) return $aprefix;
 $aub=$_SERVER['REQUEST_URI'];
 $xubf=explode("?", $aub);
 $xubf[0]=str_replace("tutorial_to_animated_gif.php", "", str_replace("@!@","", str_replace("@!@/","", "@!@" . $xubf[0])));
 $xubff=explode("/", $xubf[0]);
 for ($ih=1; $ih<sizeof($xubff); $ih++) $aprefix .= "../";
 //echo $aprefix;
 if (strpos($xubff[sizeof($xubff) - 1], ".") !== false && (sizeof($xubff) - 1) > 2) {
       $xub=str_replace("/" . $xubff[sizeof($xubff) - 1], "/", $xubf[0]);
 } else {
       $xub=str_replace("@$@", "", str_replace("/@$@", "/", ($xubf[0] . "@$@")));
 }
 $ubf=explode("?", $url);
 
 $ubff=explode("/", $ubf[0]);
 if (strpos($ubff[sizeof($ubff) - 1], ".") !== false && (sizeof($ubff) - 1) > 2) {
       $ub=str_replace("/" . $ubff[sizeof($ubff) - 1], "/", $ubf[0]);
 } else {
       $ub=str_replace("@$@", "", str_replace("/@$@", "/", ($ubf[0] . "@$@")));
 }
 $preubff=explode("rjmprogramming.com.au:80/", $ub);
 if (sizeof($preubff) > 1) {
  $aprefix.=$preubff[1];
 } else {
 $preubff=explode("rjmprogramming.com.au/", $ub);
 if (sizeof($preubff) > 1) {
  $aprefix.=$preubff[1];
 } else {
 $preubff=explode("localhost:8888/", $ub);
 if (sizeof($preubff) > 1) {
  $aprefix.=$preubff[1];
 }
 }
 }
 return $aprefix;
}

if (isset($_GET['slideshow']) || isset($_POST['slideshow'])) {
 if (isset($_GET['slideshow'])) $url=($_GET['slideshow']);
 if (isset($_POST['slideshow'])) $url=($_POST['slideshow']);
 if (strpos($url, ' src="') !== false) {
 $shtml=$url;
 file_put_contents(server_remote_addr() . "_huh.html", $url);
 $moreh1=" ... you have 10 seconds to change delay value as necessary";
 $imagegifandthen.="document.getElementById('slideshow').value='http://www.rjmprogramming.com.au/PHP/animegif/" . server_remote_addr() . "_huh.html'; setTimeout(and_then,10000); ";
 $shtml="";
 } else {
 if (isset($_GET['slideshow'])) $shtml=@file_get_contents($_GET['slideshow']);
 if (isset($_POST['slideshow'])) $shtml=@file_get_contents($_POST['slideshow']);
 }
 $prefix=analyze($url);
 if (isset($_GET['delay'])) $delay=($_GET['delay']);
 if (isset($_POST['delay'])) $delay=($_POST['delay']);
 $eachone=str_replace("[]=40;", "[]=" . $delay . ";", $eachone);
 $gifphp=$agifphp;
 if (isset($_GET['stitle'])) $agtext=($_GET['stitle']);
 if (isset($_POST['stitle'])) $agtext=($_POST['stitle']);
 if (strpos($shtml, ' src="') === false) {
  $shtml=' src="' . $url . '" ';
  $icnt++;
  while (isset($_GET['slideshow' . $icnt]) || isset($_POST['slideshow' . $icnt])) {
   if (isset($_GET['slideshow' . $icnt])) $url=($_GET['slideshow' . $icnt]);
   if (isset($_POST['slideshow' . $icnt])) $url=($_POST['slideshow' . $icnt]);
   $shtml.=' src="' . $url . '" ';
   $icnt++;
  }
 } 
 $photos=explode(' src="', $shtml);
 if (sizeof($photos) > 1) {
   $atext=$agtext;
   for ($io=1; $io<sizeof($photos); $io++) {
    $aphoto=explode('"', $photos[$io]);
    $prefix=analyze($aphoto[0]);
    if (strpos($aphoto[0], "rjmprogramming.com.au/") !== false) {
      $bts=explode("rjmprogramming.com.au/", $aphoto[0]);
      $aphoto[0]=$bts[1];
    } else if (strpos($aphoto[0], "rjmprogramming.com.au:80/") !== false) {
      $bts=explode("rjmprogramming.com.au:80/", $aphoto[0]);
      $aphoto[0]=$bts[1];
    } else if (strpos($aphoto[0], "localhost:8888/") !== false) {
      $bts=explode("localhost:8888/", $aphoto[0]);
      $aphoto[0]=$bts[1];
    }
    $dbits=explode("/", $aphoto[0]);
    for ($itr=0; $itr<(sizeof($dbits) - 1); $itr++) {
      $dtib=$dbits[$itr] . "/";
      $oy=1;
      if (strpos($prefix, $dtib) !== false) $prefix=str_replace($dtib, "", $prefix, $oy);
    }
    if (strpos($aphoto[0], ";base64,") !== false) {
      $gifphp.=str_replace("imagecreatefromjpeg('source01.jpg');", "imagecreatefromstring(base64_decode('" . explode(";base64,",$aphoto[0])[1] . "'));", str_replace("\$text='';", "\$text='" . $atext . "';", $eachone));
    } else if (strpos(strtolower($aphoto[0]), "//") !== false) {
      $gifphp.=str_replace("imagecreatefromjpeg('source01.jpg');", "imagecreatefromstring(@file_get_contents('" . $prefix . $aphoto[0] . "'));", str_replace("\$text='';", "\$text='" . $atext . "';", $eachone));
    } else if (strpos(strtolower($aphoto[0]), ".jp") !== false) {
      $gifphp.=str_replace("imagecreatefromjpeg('source01.jpg');", "imagecreatefromjpeg('" . $prefix . $aphoto[0] . "');", str_replace("\$text='';", "\$text='" . $atext . "';", $eachone));
    } else if (strpos(strtolower($aphoto[0]), ".png") !== false) {
      $gifphp.=str_replace("imagecreatefromjpeg('source01.jpg');", "imagecreatefrompng('" . $prefix . $aphoto[0] . "');", str_replace("\$text='';", "\$text='" . $atext . "';", $eachone));
    } else if (strpos(strtolower($aphoto[0]), ".gif") !== false) {
      $gifphp.=str_replace("imagecreatefromjpeg('source01.jpg');", "imagecreatefromgif('" . $prefix . $aphoto[0] . "');", str_replace("\$text='';", "\$text='" . $atext . "';", $eachone));
    } else if (strpos(strtolower($aphoto[0]), ".bmp") !== false) {
      $gifphp.=str_replace("imagecreatefromjpeg('source01.jpg');", "imagecreatefrombmp('" . $prefix . $aphoto[0] . "');", str_replace("\$text='';", "\$text='" . $atext . "';", $eachone));
    } 
    $atext="";
   }
   if ($agtext == "") $agtext="slideshow";
   $lastbit=str_replace("framed,0,2,0,0,0,", "framed,0," . (sizeof($photos) - 1) . ",0,0,0,", $lastbit);
   //file_put_contents(str_replace(" ", "_", $agtext) . "_preview.php", $gifphp . $lastbit);
   //file_put_contents(str_replace(" ", "_", $agtext) . ".php", $gifphp . str_replace($lastbitfrom, str_replace("'animegif.gif'", "'" . server_remote_addr() . ".gif" . "'", $lastbitto), $lastbit));
   //@file_get_contents(str_replace(" ", "_", $agtext) . ".php");
   file_put_contents(server_remote_addr() . "_preview.php", $gifphp . $lastbit);
   file_put_contents(server_remote_addr() . ".php", $gifphp . str_replace($lastbitfrom, str_replace("'animegif.gif'", "'" . server_remote_addr() . ".gif" . "'", $lastbitto), $lastbit));
   //@file_get_contents(server_remote_addr() . ".php?x=" . rand (2,450456));
   //header("Location: " . str_replace(" ", "_", $agtext) . "_preview.php");
   $imagegif="<br><h1 align='center'>Preview Below ... Animated GIF (Later) (Way) Below</h1><br><iframe frameborder=0 style='width:100%;height:100%;' src='" . server_remote_addr() . "_preview.php?x=" . rand (2,450456) . "' title='" . $agtext . "'></iframe>";
   $imagegif.="<br><br><iframe frameborder=0 style='display:none;' src='" . server_remote_addr() . ".php?x=" . rand (2,450456) . "' title='" . $agtext . "'></iframe>";
   $imagegifandthen.="document.getElementById('idiv').innerHTML+=\"<br><h1 align='center'>Preview Above ... Animated GIF Below</h1><br><img src='" . server_remote_addr() . ".gif" . "' title='" . $agtext . "'></img><br><iframe src='animegif.html?x=" . rand (2,450456) . "' title=DataURI style=width:100%;height:800px;></iframe>" . "\";";
 }
} //else {
  echo "<!doctype html>
<html>
<head>
<link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'>
<title>Tutorial Slideshow to Animated GIF</title>
<script type='text/javascript'>
var icnt=1;
function isimage(tvl) {
  if (tvl.toLowerCase().indexOf('.jp') != -1) return true;
  if (tvl.toLowerCase().indexOf('.gif') != -1) return true;
  if (tvl.toLowerCase().indexOf('.png') != -1) return true;
  if (tvl.toLowerCase().indexOf('.bmp') != -1) return true;
  if (tvl.toLowerCase().indexOf('.tif') != -1) return true;
  return false;
}
function and_then() {
  document.getElementById('mysubmit').click();
}
function maybemore(tval, tid) {
  var newi=null, fo=null;
  if (isimage(tval)) {
   fo=document.getElementById('fdiv' + icnt);
   icnt++;
   newi=document.getElementById('slideshow' + icnt);
   if (fo != null && newi == null) {
    fo.innerHTML+=\"Tutorial Slideshow Image \" + icnt + \" <input onblur='maybemore(this.value, this.id);' style='width:70%;' type='url' id='slideshow\" + icnt + \"' name='slideshow\" + icnt + \"' value=''></input><br><br><div id='fdiv\" + icnt + \"'></div>\";
   } 
  }
}
function andthen() {
" . $imagegifandthen . "
}
</script>
</head>
<body style='background-color:yellow;' onload=' setTimeout(andthen, 8000); '>
<h1 id='myh1' align='center'>Tutorial Slideshow to Animated GIF" . $moreh1 . "</h1>
<div align='center'>
<form id='myform' action='./tutorial_to_animated_gif.php' method='POST'>
Tutorial Slideshow Title <input style='width:70%;' type='text' id='stitle' name='stitle' value='" . $agtext . "'></input><br><br>
Tutorial Slideshow Delay <input style='width:70%;' type='number' id='delay' name='delay' value='" . $delay . "'></input><br><br>
Tutorial Slideshow HTML or Image <input onblur=\"maybemore(this.value, 'slideshow1');\" style='width:70%;' type='url' id='slideshow' name='slideshow' value='" . $url . "'></input><br><br>
<div id='fdiv1'></div><input id=\"mysubmit\" type=\"submit\" title='Tutorial Slideshow to Animated GIF' value='Tutorial Slideshow to Animated GIF'></input>
</form><div id='idiv' align='center' style='background-color:pink;'>" . $imagegif . "</div>
</div>
</body>
</html>";

//}


?>