<?php
include_once 'db.php';

if (isset($_POST["sended"]) && isset($_FILES["fileToUpload"])){
          $vypis = "";
          $error = 0;
          $name = $_POST['Name'];
          $surname = $_POST['Surname'];
          $address = $_POST['City'];
          $state = $_POST['State'];
          $YoB = $_POST['YearOfBirth'];
          $height = $_POST['Height'];
          $gender = $_POST['Gender']; 
          $skin = $_POST['SkinColor'];
          $eyeColor = $_POST['EyeColor'];
          $hairColor = $_POST['HairColor'];
          $dressUpper = $_POST['ClothesUpperHalf'];
          $dressLower = $_POST['ClothesLowerHalf'];
          if (isset($_POST['Beard']) && $_POST['Beard'] == "1") {$beard = 1;} else {$beard = 0;}
          if (isset($_POST['Scar']) && $_POST['Scar'] == "1") {$scar = 1;} else {$scar = 0;}
          if (isset($_POST['Tattoo']) && $_POST['Tattoo'] == "1") {$tattoo = 1;} else {$tattoo = 0;}
          
          $imageFileType = strtolower(pathinfo(basename($_FILES["fileToUpload"]["name"]),PATHINFO_EXTENSION)); 
          $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
          if($check !== false) {
              $error = 0;
          } else {
              $vypis .= "File is not an image<br>";
              $error = 1;
          }
          
          if ($_FILES["fileToUpload"]["size"] > 31457280) {
              $vypis .= "Sorry, your file is too large (Max 30MB)<br>";
              $error = 1;
          }
          // Allow certain file formats
          if($imageFileType != "jpg") {
              $vypis .= "Sorry, only JPG files are allowed<br>";
              $error = 1;
          }
                          
          if ($error == 0)
          {
          	$result = $mysqli->query("SELECT * FROM traits WHERE Name LIKE '$name' AND Surname LIKE '$surname';");
            if (mysqli_num_rows($result) == 0) {$accord = "";} else {$accord = strval((mysqli_num_rows($result))+1);}          
            $image = strtolower(iso2ascii($name))."_".strtolower(iso2ascii($surname)).$accord.".".$imageFileType;
            $imageSRC = "pictures/$image";
            list($w, $h) = getimagesize($_FILES['fileToUpload']['tmp_name']);
            resize (140,$accord,"_small",100);
            if ($h > 1200) {resize (1200,$accord,"",70);} else {move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $imageSRC);}
            $add = "INSERT INTO traits (`Name`, `Surname`, `City`, `State`, `YearOfBirth`, `Height`, `Gender`, `SkinColor`, `EyeColor`, `HairColor`, `Beard`, `Scar`, `Tattoo`, `ClothesUpperHalf`, `ColorUpperHalf`, `ClothesLowerHalf`, `ColorLowerHalf`) 
                    VALUES ('$name', '$surname', '$address', '$state', '$YoB', '$height', '$gender', '$skin', '$eyeColor', '$hairColor', '$beard',
                    '$scar', '$tattoo', '$dressUpper', '".Color('ColorUpperHalf')."', '$dressLower', '".Color('ColorLowerHalf')."');";
            if ($mysqli->query($add) === TRUE) {
                
            } else {
                $vypis .= "Error: " . $mysqli->error . "<br>";
            }
          }
            
            echo $vypis; 
          }
          
            function resize($height,$accord,$suffix,$quality){
              $imageFileType = strtolower(pathinfo(basename($_FILES["fileToUpload"]["name"]),PATHINFO_EXTENSION)); 
              list($w, $h) = getimagesize($_FILES["fileToUpload"]['tmp_name']);
              $width=($w/$h)*$height;
              $ratio = max($width/$w, $height/$h);
              $h = ceil($height / $ratio);
              $w = ceil($width / $ratio);
              $path = 'pictures/'.strtolower(iso2ascii($_POST['Name']))."_".strtolower(iso2ascii($_POST['Surname'])).$accord.$suffix.".".$imageFileType;
              $imgString = file_get_contents($_FILES['fileToUpload']['tmp_name']);
              $image = imagecreatefromstring($imgString);
              $tmp = imagecreatetruecolor($width, $height);
              imagecopyresampled($tmp, $image, 0, 0, 0, 0, $width, $height, $w, $h);
              imagejpeg($tmp, $path, $quality);
              /* cleanup memory */
              imagedestroy($image);
              imagedestroy($tmp);
            }  
            
            function Color($what)
            { 
              	$colors = "";      
              foreach($_POST["$what"] as $polozka) {
                $colors .= "$polozka,";
              }
              $colors = substr($colors, 0, strlen($colors)-1);
              return $colors;
            } 
?>
