RewriteEngine On
RewriteRule    ^.*_layered\..*$    index.php    [NC,L]
<?php
session_start
();
if(isset(
$_GET['source'])){
    echo 
'<span style="padding:10px;margin:10px;border:1px solid #000;display:inline-block;" title=".htaccess">';
    
highlight_file(".htaccess");
    echo 
'</span><span style="padding:10px;margin:10px;border:1px solid #000;display:inline-block;" title="index.php">';
    
highlight_file("index.php");
    echo 
'</span>';
    exit;
}elseif(isset(
$_FILES) && is_array($_FILES) && isset($_FILES['file'])){
    if(
getimagesize($_FILES['file']['tmp_name'])!==false){
        
header("Content-Type: text/plain");
        function 
open_image($file) {
            
$size=getimagesize($file);
            switch(
$size["mime"]){
                case 
"image/jpeg":
                    
$im imagecreatefromjpeg($file); //jpeg file
                
break;
                case 
"image/gif":
                    
$im imagecreatefromgif($file); //gif file
              
break;
              case 
"image/png":
                  
$im imagecreatefrompng($file); //png file
              
break;
            default:
                
$im=false;
            break;
            }
            
imagealphablending($imfalse);
            
imagesavealpha($imtrue);
            return 
$im;
        }
        
        if(isset(
$_POST["fudsfa"]) && $_POST["fudsfa"] !== "_none123_") {
            
$uploaded imagecreatefromstring($_SESSION["fudsfa"]);
        }else{
            
$uploaded open_image($_FILES['file']['tmp_name']);
            
$_SESSION["fudsfa"] = $_FILES['file']['tmp_name'];
        }
        
$img_w imagesx($uploaded);
        
$img_h imagesy($uploaded);
        
$image imagecreatetruecolor($img_w$img_h);
        
$white imagecolorallocate($image255255255);
        
imagefill($image00$white);
        
imagecopyresampled($image$uploaded0000$img_w$img_h$img_w$img_h);   
        
        
        if(isset(
$_POST["fudsfa"]) && $_POST["fudsfa"] !== "_none123_"){
            
$toppo open_image($_FILES['file']['tmp_name']);
            
            
$img_w2 imagesx($toppo);
            
$img_h2 imagesy($toppo);
            
            
imagealphablending($toppotrue);
            
imagesavealpha($toppotrue);
            
            
$black imagecolorallocate($toppo000);
            
imagecolortransparent($toppo$black);
            
            
$image2 imagecreatetruecolor($img_w$img_h);
            
imagecopyresampled($image2$toppo0000$img_w$img_h$img_w2$img_h2); 
            
            
imagealphablending($image2true);
            
imagesavealpha($image2true);
            
            
$black imagecolorallocate($image2000);
            
imagecolortransparent($image2$black);
            
            
$o intval($_POST['opacity']);
            
imagecopymerge($image$image20000$img_w$img_h$o);
        }
        
        
        
ob_start(); 
            
imagejpeg($image);
            
$image_data ob_get_contents();
        
ob_end_clean();
        
$_SESSION["fudsfa"] = $image_data;
        
$_SESSION[$_FILES['file']['name']] = base64_encode($image_data);
        
        
imagedestroy($image);
        
imagedestroy($uploaded);
        die(
$_FILES['file']['name']);
        exit;
    }else{
        die(
"error");
    }
}elseif(isset(
$_GET['btoa'])){
    
//header("Content-Type: image/jpeg");
    
die(base64_decode($_SESSION[$_GET['btoa']]));
}
?><!doctype html>
<html lang="en">

<head>
    <link rel="shortcut icon" href="http://jakeis.com/scriptpageicon/favicon.ico" type="image/x-icon" />
    <meta charset="UTF-8">
    <title>🍍 Quick Image Overlap 🍕</title>
    <style type="text/css">
        label{
            display:block;
        }
    </style>
    <script src="//jakeis.com/lib/jquery-2.1.3.min.js"></script>
    <script src="//jakeis.com/lib/dropzone.js"></script>
    <script>
        var firstImage = false;
        Dropzone.options.lapGen = {
          paramName: "file", // The name that will be used to transfer the file
          maxFilesize: 2, // MB
          maxFiles: 1,
          acceptedFiles: "image/*",
          dictDefaultMessage: "<p>Drop a base image here.</p><p>Accepts JPEG and PNG. (max 2mb)</p>",
          addedfile: function(file, done) {
              jQuery("h2").text("uploading...");
              var upload = new FormData();
              upload.append("file",file);
              if(jQuery('input[name="opacity"]:checked').length > 0){
                  upload.append("opacity", jQuery('input[name="opacity"]:checked').val());
              }
              upload.append("fudsfa", firstImage === false ? "_none123_" : firstImage);
            jQuery.ajax({
                url: "index.php",
                data: upload,
                cache: false,
                contentType: false,
                processData: false,
                type: 'POST',
                success: function(tmp){
                    if(tmp==="error"){
                        alert("There seems to be trouble with this image.");
                        return false;
                    }
                    //alert(tmp);
                    if(!firstImage){
                        jQuery(".dz-default p:eq(0)").text("Drop more images to overlap.");
                        firstImage = tmp;
                        jQuery("#lapGen").after([
                            jQuery('<label for="t10" />').append(
                                jQuery('<input name="opacity" id="t10" type="radio" value="10"/>'),
                                "10% opacity"
                            ),
                            jQuery('<label for="t30" />').append(
                                jQuery('<input name="opacity" id="t30" type="radio" checked value="30"/>'),
                                "30% opacity"
                            ),
                            jQuery('<label for="t50" />').append(
                                jQuery('<input name="opacity" id="t50" type="radio" value="50"/>'),
                                "50% opacity"
                            ),
                            jQuery('<label for="t80" />').append(
                                jQuery('<input name="opacity" id="t80" type="radio" value="80"/>'),
                                "80% opacity"
                            ),
                            jQuery('<label for="t100" />').append(
                                jQuery('<input name="opacity" id="t100" type="radio" value="100"/>'),
                                "100% opacity"
                            )
                        ]);
                    }
                    var fil = file.name.replace(/\..{3,4}$/,"");
                    var ext = file.name.replace(/^.*\.(.{3,4})$/,"$1");
                    jQuery("#images").html("").append(
                        jQuery('<img>').attr({
                            src: fil + "_layered." +ext + "?" + Math.floor(Math.random()*10000) + "&btoa=" + tmp
                        })
                    );
                },
                error: function(){
                    alert("There seems to be trouble with this image.");
                },
                complete: function(){
                    jQuery("h2").text("");
                }
            });
          }
        };
    </script>
    <style type="text/css">
        body{
            padding:40px;
        }
        #images, body{
            text-align:center;
        }
        form{
            padding: 60px;
            border: 2px #999 solid;
            min-width:50%;
            display:inline-block;
        }
        img{
            max-width:70%;
            margin:25px;
        }
    </style>
</head>

<body>
    <h1>🍍 Quick Image Overlap 🍕</h1>
    <h2></h2>
    <form action="index.php" class="dropzone" id="lapGen">
      <div class="fallback">
        <input name="file" type="file" multiple />
      </div>
    </form>
    <div id="images"></div>
</body>
</html>