<?php
session_start();
	
	// Paths of images
	$privPath = "../Images/transPPin.png";
	$pubPath = "../Images/transPin.png";
	$imgPath = "../uploads/".$_POST['fileName'];
	
	// Hidden variables that need to be passed through the Session
	$meta_data = $_SESSION['meta_data'];
	//echo "$meta_data <br>";
	
	$meta_data = $meta_data.$_POST['fileName'].';';
	$meta_data = $meta_data.$_POST['theDate'].';';
	$meta_data = $meta_data.$_POST['theTime'].';';
	$meta_data = $meta_data.$_POST['theLat'].';';
	$meta_data = $meta_data.$_POST['theLong'].';';
	$meta_data = $meta_data.$_POST['theCity'].';';
	
	$meta_data = $meta_data.$_POST['view'].';';
	$_SESSION['meta_data'] = $meta_data;
	
	//echo $_SESSION['meta_data'];
	//echo "<br>";
	
	$e = trim($_POST['view']);
	// Load images from file paths
	if($e == 'private')
		$img1 = imagecreatefrompng($privPath);
	else
		$img1 = imagecreatefrompng($pubPath);

	// Load the image that needs to be put into a pin
	$img2 = imagecreatefromjpeg($imgPath);

	// Get dimensions of the images
	$size1 = getimagesize($pubPath);//this size does not matter as both the public/private pins have the same dimensions
	$size2 = getimagesize($imgPath);
	//$size2 = getimagesize('Images/small_Pin.png');
	
	// Scale the image to fit into the pin
	$resized = imagescale($img2, $size1[0], $size1[1]/1.25);

	// Create a new blank canvas with combined width and maximum height
	$canvas = imagecreatetruecolor($size1[0], max($size1[1], $size1[1]));
	$transparency = imagecolorallocatealpha($img1, 0, 0, 0, 127);
	imagefill($canvas, 0, 0, $transparency);
	
	// Save the alpha values to preserve transparency
	imagealphablending($img1, false);
	imagesavealpha($img1, true);
	
	//create the transparency color and fill the background with it
	
	// Copy the first image to the canvas (at coordinates 0, 0)
	imagecopy($canvas, $resized, (imagesx($resized[0])/2), (imagesy($resized[1])/2), 0, 0, $size1[0], $size1[1]);
	
	// Copy the second image on top of the first image
	imagecopy($canvas, $img1, 0, 0, 0, 0, $size2[0], $size2[1]);
		
	// Determine the name of the new combined image
	$pinPath = "../uploads/".pathinfo($imgPath, PATHINFO_FILENAME)."Pin.png";
	//echo $imgPath; echo "<br>";
	//echo $pinPath; 
	
	// Save the new image to a file
	//header('Content-Type: image/png');
	imagepng($canvas, $pinPath); //the file path needs to have the name of the file

	//create the other type of pin
	if($e == 'private')
		$img1 = imagecreatefrompng($pubPath);
	else
		$img1 = imagecreatefrompng($privPath);
	
	//throw the pin onto the canvas
	imagecopy($canvas, $img1, 0, 0, 0, 0, $size2[0], $size2[1]);
	
	//make the path for the photo
	$pinPath = "../uploads/".pathinfo($imgPath, PATHINFO_FILENAME)."PPin.png";
	
	//save the pin
	imagepng($canvas, $pinPath);
	
	// Free up memory
	imagedestroy($canvas);
	imagedestroy($img1);
	imagedestroy($img2);
	
	echo '<script>window.location.href = "createPhoto.php"</script>';
?>

<!-- <a href="../liveIndex.php">Return</a> -->