I am trying to create a feature on a website that allows a user to upload a pngimage using javascript and then over the top of that place a new pngimage using PHP gd that has transparent circles so that the image underneath will show though. I will then hopefully use the imagecopymerge function to merge them together. It would be great if this could all be done using AJAX, without the use of saving the users picture or the PHP gd image into a database. The user will then need to be able to save the new finished combined image to their account.
The main problem I am having is how to get the PHP gd image to be shown onto the canvas that the picture is on or any other canvas.
The main page
<div class= "box2">
<form enctype="multipart/form-data">
<h4>Step 1: Select an image to upload</h4>
<div class= "canvas">
<input type="file" id="imageLoader" name="imageLoader"/>
<br>
<canvas id="imageCanvas"></canvas>
<script>
var imageLoader = document.getElementById('imageLoader');
imageLoader.addEventListener('change', handleImage, false);
var canvas = document.getElementById('imageCanvas');
var ctx = canvas.getContext('2d');
function handleImage(e){
var reader = new FileReader();
reader.onload = function(event){
var img = new Image();
img.onload = function(){
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img,0,0);
}
img.src = event.target.result;
}
reader.readAsDataURL(e.target.files[0]);
}
</script>
</form>
</div>
<script>
$("document").ready(function(){
$(".stuff3").submit(function() {
event.preventDefault();
$.post("stuff3.php", $(".stuff3").serialize() , function(data){
$("#imageCanvas").html(data);
});
});
});
</script>
<form action= "stuff3.php" method= "post">
<h4>Step 2: Choose a Size</h4>
<input type="number" placeholder="Height" name="height" min="1" max="10" step="1" />
<input type="number" placeholder="Width" name="width" min="1" max="12" step="1" />
<button id="loginbutton" type="submit" value="Submit"> Create Size</button>
</form>
</div>
The PHP for creating the image
<?php
$h= $_POST['height'];
$w= $_POST['width'];
header('Content-type: image/png');
$png_image = imagecreate(100*$w, 100*$h);
$grey = imagecolorallocate($png_image, 245, 245, 245);
$im = imagecreatetruecolor(55, 30);
$green = imagecolorallocate($png_image, 255, 255, 255);
imagecolortransparent($im, $green);
imagefilltoborder($png_image, 0, 0, $grey, $grey);
$x=50;
$y=50;
$i;
$j;
for ($j=0;$j<=$h-1;$j++){
for ($i=0;$i<=$w;$i++) {
imagefilledellipse ($png_image, $x, $y, 75, 75, $green); // circle
$x+=100;
}
$x=50;
$y+=100;
}
imagepng($png_image);
imagedestroy($png_image);
?>