<?php
// define constants
define('SOURCE_DIR', '../assemblymembers/');
define('THUMBS_DIR', '../assemblymembers/thumbs/');
define('MAX_WIDTH', 350);
define('MAX_HEIGHT', 350);
// process the uploaded image
if (is_uploaded_file($_FILES['image']['tmp_name'])) {
$original = $_FILES['image']['tmp_name'];

// begin by getting the details of the original
list($width, $height, $type) = getimagesize($original);
// calculate the scaling ratio
if ($width <= MAX_WIDTH && $height <= MAX_HEIGHT) {
$ratio = 1;
}
elseif ($width > $height) {
$ratio = MAX_WIDTH/$width;
}
else {
$ratio = MAX_HEIGHT/$height;
}
// strip the extension off the image filename
$imagetypes = array('/\.gif$/', '/\.jpg$/', '/\.jpeg$/', '/\.png$/');
$name = preg_replace($imagetypes, '', basename($original));
$_FILES['image']['name']= str_replace(" ","_",$_FILES['image']['name']);
// move the temporary file to the upload folder
if(!file_exists(UPLOAD_DIR.$_FILES['image']['name'])){
$moved = @ move_uploaded_file($original, 
UPLOAD_DIR.$_FILES['image']['name']);
if ($moved) {
$result = $_FILES['image']['name'].' successfully uploaded; ';
$original = UPLOAD_DIR.$_FILES['image']['name'];

}
else {
$result = 'Problem uploading '.$_FILES['image']['name'].'; ';
}
}
else{
	//echo "<strong>Image already exists in the database. Please upload a new one</strong>
//			<a href='my_imgupload_both.php'>back to upload form </a>
//		";
//		exit();
	ini_set('date.timezone','Europe/London');
	$now = date('d-m-Y-His');
	$moved = move_uploaded_file($original, UPLOAD_DIR.$now.$_FILES['image']['name']);
		if ($moved) {
			$result = $_FILES['image']['name'].' successfully uploaded; ';
			$original = UPLOAD_DIR.$now.$_FILES['image']['name'];
			$file = $now.$_FILES['image']['name'];
		}
}
// create an image resource for the original
switch($type) {
case 1:
$source = @ imagecreatefromgif($original);
if (!$source) {
$result = 'Cannot process GIF files. Please use JPEG or PNG.';
}
break;
case 2:
$source = imagecreatefromjpeg($original);
break;
case 3:
$source = imagecreatefrompng($original);
break;
default:
$source = NULL;
$result = 'Cannot identify file type.';
}
// make sure the image resource is OK
if (!$source) {

$result = 'Problem copying original';
}
else {
// calculate the dimensions of the thumbnail
$thumb_width = round($width * $ratio);
$thumb_height = round($height * $ratio);
// create an image resource for the thumbnail
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
// create the resized copy
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $thumb_width, 
$thumb_height, $width, $height);
// save the resized copy
switch($type) {
case 1:
if (function_exists('imagegif')) {
$success = imagegif($thumb, THUMBS_DIR.$name.'_thb.gif');
$thumb_name = $name.'_thb.gif';
}
else {
$success = imagejpeg($thumb, THUMBS_DIR.$name.'_thb.jpg',50);
$thumb_name = $name.'_thb.jpg';
}
break;
case 2:
$success = imagejpeg($thumb, THUMBS_DIR.$name.'_thb.jpg', 100);
$thumb_name = $name.'_thb.jpg';
break;
case 3:
$success = imagepng($thumb, THUMBS_DIR.$name.'_thb.png');
$thumb_name = $name.'_thb.png';
}
if ($success) {
	$result = "$thumb_name created";

	$insertSQL = sprintf("INSERT INTO electoralareas (submetroID, electoralarea) VALUES (%s, %s)",
	                       
	                       GetSQLValueString($_POST['submetroID'], "int"),
						   GetSQLValueString($_POST['electoralarea'], "text")/* ,
						   GetSQLValueString($curr_time, "text") */);
						   
		  $Result1 = mysqli_query($conn_ama, $insertSQL) or die(mysqli_error($conn_ama));
		  $electoralareaID = mysqli_insert_id($conn_ama);
		  
		  if($Result1){
				$insertSQL1 = sprintf("INSERT INTO assemblymembers (electoralareaID, fname, mname, lname,image,thumbnail) VALUES (%s, %s, %s, %s, %s, %s)",
							GetSQLValueString($electoralareaID, "int"),
							GetSQLValueString($_POST['fname'], "text"),
							GetSQLValueString($_POST['mname'], "text"),
							GetSQLValueString($_POST['lname'], "text"),
							GetSQLValueString($file, "text"),
							GetSQLValueString($thumb_name, "text"));

				$Result2 = mysqli_query($conn_ama, $insertSQL1) or die(mysqli_error($conn_ama));
				if($Result2){
					echo "datasent";
				}
		  }
		  else{
			echo 'sub-metro-error';
		  }	  	
	}
else {
	$result = 'thumbnail-error';
}
// remove the image resources from memory
imagedestroy($source);
imagedestroy($thumb);
}
}
?>