Better UI
[pixel-calc.git] / public_html / upload.php
index a37b476..dc93beb 100644 (file)
@@ -231,22 +231,36 @@ function spray_can_cost($colour_count, $block_size, $can_coverage, $coats, $disc
     return round($percent_cost ? end($discount_matrix)*$total_cans : $total_cans*$cost_per_can,4);
 }
 
-if(isset($_POST["submit"]) || isset($_GET['sprite'])) {
-    if(isset($_POST['submit'])) {
-        if(isset($_FILES['fileToUpload'])) {
-            $image = $_FILES['fileToUpload']['tmp_name'];
-        }
-        
-        if(isset($_POST['sprite'])) {
-            $image = "uploads/" . $_POST['sprite'] . '.png';
-        }
-    }
+if(!empty($_FILES['file'])) {
+    $image = $_FILES['file']['tmp_name'];
+    $hash = substr(md5_file($image), 0, 8);
+    $filename = 'uploads/' . $hash . '.png';
     
-    if(isset($_GET['sprite'])) {
-        $image = 'uploads/' . $_GET['sprite'] . '.png';
+    if(!file_exists($filename)) {
+        move_uploaded_file($_FILES["file"]["tmp_name"], $filename);
     }
+    
+    die($hash);
+}
 
-    $hash = substr(md5_file($image), 0, 8);
+if(isset($_POST['hash'])) {
+    $hash = $_POST['hash'];
+    
+    $vars['spritename'] = $_POST['spritename'];
+    $vars['wood_size'] = $_POST['wood_size'];
+    $vars['wood_length'] = $_POST['wood_length'];
+    $vars['wood_cost'] = $_POST['wood_cost'];
+    $vars['markup'] = $_POST['markup'];
+    
+    $fp = fopen('uploads/'. $hash . '.txt', 'w');
+    fwrite($fp, serialize($vars));
+    
+    header("Location: upload.php?sprite=$hash");
+}
+
+if(isset($_GET['sprite'])) {
+    $hash = $_GET['sprite'];
+    $image = 'uploads/' . $_GET['sprite'] . '.png';
     
     $ironlak_colours = array(
         "532f18" => "Earth",
@@ -351,15 +365,8 @@ if(isset($_POST["submit"]) || isset($_GET['sprite'])) {
         11 => 7.16, //6-11 cans
         5 => 7.95 // 0-5 cans
     );
-    
-    if(isset($_POST['submit'])) {
-        $vars['wood_size'] = $_POST['wood_size'];
-        $vars['wood_length'] = $_POST['wood_length'];
-        $vars['wood_cost'] = $_POST['wood_cost'];
-        $vars['markup'] = $_POST['markup'];
-    } elseif(file_exists('uploads/'.$hash.'.txt')) {
-        $vars=  unserialize(file_get_contents('uploads/'.$hash.'.txt'));
-    }
+
+    $vars=  unserialize(file_get_contents('uploads/'.$hash.'.txt'));
     
     $pixel_array = image_to_array($image);
     $sprite_width_cm = count($pixel_array[0]) * $vars['wood_size'] * 100 . "cm";
@@ -379,20 +386,8 @@ if(isset($_POST["submit"]) || isset($_GET['sprite'])) {
     $value_of_can_used = spray_can_cost(colour_count($pixel_array, $colour_map), $vars['wood_size'], 2.2, 3, $discount_matrix, TRUE);
     $cost_of_lumber = lumber_cost($vars['wood_cost'], $vars['wood_length'], $vars['wood_size'], count_pixels($pixel_array));
     $value_of_lumber_used = lumber_cost($vars['wood_cost'], $vars['wood_length'], $vars['wood_size'], count_pixels($pixel_array), TRUE);
-    
+    $name = $vars['spritename'];
     $markup = $vars['markup'];
-
-    $filename = 'uploads/' . $hash . '.png';
-    
-    if(!file_exists($filename)) {
-        move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $filename);
-        $fp = fopen('uploads/'. $hash . '.txt', 'w');
-        fwrite($fp, serialize($vars));
-    }
-    
-    if(!isset($_GET['sprite'])) {
-        header("location: upload.php?sprite=$hash");
-    }
     
     require_once('./breakdown.html');
 }