<?php
 $banner=$_FILES['banner']['name']; 
 $expbanner=explode('.',$banner);
 $bannerexptype=$expbanner[1];
 date_default_timezone_set('Australia/Melbourne');
 $date = date('m/d/Yh:i:sa', time());
 $rand=rand(350,750);
 $bannername="img.jpg";
$file_name = $_FILES['banner']['name'];
list($width, $height, $type, $attr) = getimagesize( $file_name );
if ( $width > $maxDim || $height > $maxDim ) {
    $target_filename = $bannername;
    $ratio = $width/$height;
    if( $ratio > 1) {
        $new_width = $rand;
        $new_height = $rand/$ratio;
    } else {
        $new_width = $maxDim*$ratio;
        $new_height = $maxDim;
    }
    $src = imagecreatefromstring( file_get_contents( $file_name ) );
    $dst = imagecreatetruecolor( $new_width, $new_height );
    imagecopyresampled( $dst, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
    imagedestroy( $src );
    imagepng( $dst, $target_filename ); // adjust format as needed
    imagedestroy( $dst );
}


 $encname=$date.$rand;
 $bannerpath="img/".$bannername;
if(file_exists($bannername)){
        unlink($bannername);
    }

 move_uploaded_file($_FILES["banner"]["tmp_name"],$bannerpath);
?>
                                
<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />
    <meta charset="UTF-8" />
    <link rel="stylesheet" type="text/css" href="styles.css" />
    <script src="FileSaver.min.js"></script>
    <script src="canvas-toBlob.js"></script>
  </head>

  <body> <form action="" method="post" enctype="multipart/form-data">
 <input type="file" name="banner" >
 <input type="submit" value="submit">
 </form>
    <img id="image1" height="<?php echo  $rand;?>"  width="<?php echo  $rand;?>" src="img/img.jpg" />

    <script>
      //create new canvas object
      let c = document.createElement("canvas");
      ctx = c.getContext("2d");

      //create new image object
      let img1 = new Image();

      //onload event listener to process the image data after it was loaded
      img1.onload = function() {

        document.getElementById("image1").remove();
        
       var height = Math.floor((Math.random() * 99) + 1);
       var width = Math.floor((Math.random() * 155) + 1);
        
  function random() {
    return "XXXXXX".replace(/X/g, function() {
      var seed = "a0b1c2c2d3e4f56789";
      return seed.charAt(Math.floor(Math.random() * seed.length))
    })
  };
        
        
       var h = Math.floor((Math.random() * 20) + 1);
       var w = Math.floor((Math.random() * 20) + 1);
        
        //set the canvas size and draw loaded image
        
        c.width = img1.width;
        c.height = img1.height;
        ctx.globalAlpha = 0.99
        ctx.drawImage(img1,h,w);
        ctx.fillStyle = "#00" + random();
        ctx.fillRect(h, w, h, w);
        

        let img2 = new Image(<?php echo  $rand;?>, <?php echo  $rand;?>);
        img2.src = c.toDataURL("image/jpeg");
        document.body.appendChild(img2);

        //popup download dialog
        c.toBlob(function(blob) {
          saveAs(blob, ""+h+""+w+".jpg");
        });
      };

      //start loading image
      img1.src = document.getElementById("image1").src;
    </script>
  </body>
</html>
