From 5a8c886562c811b8bffe1d05236dbb1af3892796 Mon Sep 17 00:00:00 2001 From: Cameron Ball Date: Tue, 21 Jul 2015 17:39:57 +0800 Subject: [PATCH] Big cleanup --- public_html/breakdown.html | 126 ++++++++++++ public_html/upload.php | 465 ++++++++++++++++++++++++--------------------- 2 files changed, 379 insertions(+), 212 deletions(-) create mode 100644 public_html/breakdown.html diff --git a/public_html/breakdown.html b/public_html/breakdown.html new file mode 100644 index 0000000..2e3c3c0 --- /dev/null +++ b/public_html/breakdown.html @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + + +
+

Wooden Sprites :)

+ + + +
+
+
+
+

Original

+
+
+
+
+

Ironlack approximation

+
+
+
+
+
+
+
+

Dimensions

+
+ +
+
 
+
+
+

Paint

+
+ +
+ +

Wood

+
+ +
+
+
+
+ nice meme :] +
+
+
+ + + + + diff --git a/public_html/upload.php b/public_html/upload.php index 1baa758..95dcdb7 100644 --- a/public_html/upload.php +++ b/public_html/upload.php @@ -1,26 +1,229 @@ - - - + $name) + { + if(colourDiff($rgb, $hex) < $smallest_diff) { + $smallest_diff = colourDiff($rgb, $hex); + $closest = $hex; + } + } + + return $closest; +} + +function array_to_table($array) { + $headings = $array[0]; + unset($array[0]); + $output = ''; + + foreach($headings as $heading) { + $output .= ''; + } + + $output .= ''; + + foreach($array as $row) { + $output .= ''; - + $hex = + str_pad(dechex($rgb['red']), 2, '0', STR_PAD_LEFT) . + str_pad(dechex($rgb['green']), 2, '0', STR_PAD_LEFT) . + str_pad(dechex($rgb['blue']), 2, '0', STR_PAD_LEFT); - + if($a == 127) $hex = "NONE"; + + $array[$i][$j] = $hex; + } + } - + return $array; +} -"; + for($i=0; $i<$height; $i++) { + $output .= ''; + for($j=0; $j<$width; $j++) { + $hex = $colourmap ? $colourmap[$array[$i][$j]] : $array[$i][$j]; + $output .= $array[$i][$j] !== 'NONE' ? "" : ""; + } + + $output .= ''; + } + + $output .= '
' . $heading . '
'; + + return $output; +} + +function generate_colourmap($array, $new_palette) { + $width = count($array[0]); + $height = count($array); + $colourMap = array('NONE' => 'NONE'); + + for($i=0; $i<$height; $i++) { + for($j=0; $j<$width; $j++) { + $colour = $array[$i][$j]; + if(!isset($colourMap[$colour])) + $colourMap[$colour] = closest_colour($colour, $new_palette); + } + } + + return $colourMap; +} + +function count_pixels($array) { + $width = count($array[0]); + $height = count($array); + $count = 0; + + for($i=0; $i<$height; $i++) { + for($j=0; $j<$width; $j++) { + if($array[$i][$j] !== 'NONE') $count++; + } + } + + return $count; +} + +function lumber_breakdown($cost_per_length, $wood_length, $wood_size, $num_blocks) { + $lengths_required = ceil($num_blocks*$wood_size/$wood_length); + $total_lumber_cost = $lengths_required * $cost_per_length; + + return array_to_table( + array( + array( + 'Num blocks', + 'No of ' . $wood_length . 'm lengths required', + 'Cost @ ' . $cost_per_length . ' per length' + ), + array( + $num_blocks, + $lengths_required, + $total_lumber_cost + ) + ) + ); +} + +function lumber_cost($cost_per_length, $wood_length, $wood_size, $num_blocks) { + $lengths_required = ceil($num_blocks*$wood_size/$wood_length); + return $lengths_required * $cost_per_length; +} + +function colour_count($array, $colour_map = NULL) { + $width = count($array[0]); + $height = count($array); + $colour_count = array(); + + for($i=0; $i<$height; $i++) { + for($j=0; $j<$width; $j++) { + $colour = isset($colour_map[$array[$i][$j]]) ? $colour_map[$array[$i][$j]] : $array[$i][$j]; + $colour_count[$colour] = isset($colour_count[$colour]) ? $colour_count[$colour]+1 : 1; + } + } + + return $colour_count; +} + +function colour_count_table($colour_count) { + $output = ''; + + foreach($colour_count as $colour => $count) { + // + if($colour != 'NONE') + $output .= ''; + } + + $output .= '
ColourRGBNum blocks
$colour$qty
' . $colour . '' . $count . '
'; + + return $output; +} + +function colour_breakdown($colour_count, $block_size, $can_coverage, $coats, $colour_names, $discount_matrix) { + $output = ''; + $cost_per_can = $discount_matrix[0]; + $total_cans = 0; + + foreach($colour_count as $colour => $count) { + $total_cans += ceil($count * $block_size * $block_size / $can_coverage); + } + + foreach($discount_matrix as $num_needed => $cost) { + if($total_cans <= $num_needed) + $cost_per_can = $cost; + } + + foreach($colour_count as $colour => $count) { + $can_percent = $count * $block_size * $block_size * $coats / $can_coverage; + $num_needed = ceil($can_percent); + if(isset($colour_names[$colour])) + $output .= ''; + } + + $output .= '
ColourNameNum cansCan %Full price% Price
' . $colour_names[$colour] . '' . $num_needed . '' . $can_percent . '' . $cost_per_can*$num_needed . '' . $can_percent * $cost_per_can . '
'; + + return $output; +} + +function spray_can_cost($colour_count, $block_size, $can_coverage, $coats, $discount_matrix, $percent_cost = FALSE) { + $cost_per_can = $discount_matrix[0]; + $total_cans = 0; + + foreach($colour_count as $colour => $count) { + $total_cans += $percent_cost ? $count * $block_size * $block_size * $coats / $can_coverage : ceil($count * $block_size * $block_size / $can_coverage); + } + + foreach($discount_matrix as $num_needed => $cost) { + if($total_cans <= $num_needed) + $cost_per_can = $cost; + } + + return $percent_cost ? end($discount_matrix)*$total_cans : $total_cans*$cost_per_can; +} -// Check if image file is a actual image or fake image if(isset($_POST["submit"])) { $ironlak_colours = array( "532f18" => "Earth", @@ -116,203 +319,41 @@ if(isset($_POST["submit"])) { "ffffff" => "Whitest Possible", "000000" => "Blackest Possible" ); - - function colourDiff($rgb1, $rgb2) { - $red1 = hexdec(substr($rgb1,0,2)); - $green1 = hexdec(substr($rgb1,2,2)); - $blue1 = hexdec(substr($rgb1,4,2)); - $red2 = hexdec(substr($rgb2,0,2)); - $green2 = hexdec(substr($rgb2,2,2)); - $blue2 = hexdec(substr($rgb2,4,2)); - - return sqrt(pow($red1 - $red2, 2) + pow($green1 - $green2, 2) + pow($blue1 - $blue2, 2)) ; - } + $discount_matrix = array( + 0 => 6.36, //120+ cans + 199 => 6.6, //60-199 cans + 59 => 6.76, //36-59 cans + 35 => 7, //12-35 cans + 11 => 7.16, //6-11 cans + 5 => 7.95 // 0-5 cans + ); - function closest_colour($rgb, array $colours) { - $smallest_diff = 766; - $closest = 'none'; - - foreach($colours as $hex => $name) - { - if(colourDiff($rgb, $hex) < $smallest_diff) { - $smallest_diff = colourDiff($rgb, $hex); - $closest = $hex; - } - } - - return $closest; - } - - $cost_per_length = $_POST['wood_cost']; - $wood_length = $_POST['wood_length']; - $wood_size = $_POST['wood_size']; - $num_cans = 0; - $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); - if($check !== false) { - $colour_count = array(); - $icolour_count = array(); - $image = imagecreatefrompng($_FILES["fileToUpload"]["tmp_name"]); - $ironlak_sprite = array(); - - echo "
Original sprite:
"; - - echo ""; - for($i=0; $i<$check[1]; $i++) { - echo ''; - for($j=0; $j<$check[0]; $j++) { - $rgb = imagecolorsforindex($image, imagecolorat($image, $j, $i)); - $r = $rgb['red']; - $g = $rgb['green']; - $b = $rgb['blue']; - $a = $rgb['alpha']; - $hex = - str_pad(dechex($rgb['red']), 2, '0', STR_PAD_LEFT) . - str_pad(dechex($rgb['green']), 2, '0', STR_PAD_LEFT) . - str_pad(dechex($rgb['blue']), 2, '0', STR_PAD_LEFT); - if($a == 127) $hex = "ffffff"; - - if($a == 0) { - echo ""; - $colour_count[$hex] = isset($colour_count[$hex]) ? $colour_count[$hex]+1 : 1; - } else { - echo ""; - } - - $ironlak_sprite[$i][$j] = closest_colour($hex, $ironlak_colours); - } - echo ''; - } - echo '
'; - - echo "
Ironlak sprite:
"; - - echo ""; - for($i=0; $i<$check[1]; $i++) { - echo ''; - for($j=0; $j<$check[0]; $j++) { - $rgb = imagecolorsforindex($image, imagecolorat($image, $j, $i)); - $r = $rgb['red']; - $g = $rgb['green']; - $b = $rgb['blue']; - $a = $rgb['alpha']; - $ihex = $ironlak_sprite[$i][$j]; - if($a == 127) $ihex = "ffffff"; - - if($a == 0) { - echo ""; - $icolour_count[$ihex] = isset($icolour_count[$ihex]) ? $icolour_count[$ihex]+1 : 1; - } else { - echo ""; - } - } - echo ''; - } - echo '
'; - echo '
'; - $total_blocks = array_sum($colour_count); - $lengths_required = ceil($total_blocks*$wood_size/$wood_length); - echo '
'; - echo '
'; - - echo 'Physical dimensions (metres)
'; - echo 'Width: ' . $check[0] * $wood_size . '
';; - echo 'Height: ' . $check[1] * $wood_size; - - echo '

'; - - echo 'Wood cost breakdown
'; - echo ' - - - - - - - - - - -
Total blocksNo of ' . $wood_length . 'm lengths requiredCost @ ' . $cost_per_length . ' per length
' . $total_blocks . '' . $lengths_required . '' . $lengths_required * $cost_per_length . '
'; - - $wood_cost = $lengths_required * $cost_per_length; - $paint_cost = 0; - - echo '

'; - echo 'Colour cost breakdown

'; - echo '
Original colour table:
'; - - echo ' - - - - - '; - foreach($colour_count as $colour => $qty) { - echo ""; - } - - echo '
ColourRGBNum blucks
$colour$qty
'; - - echo '
Ironlak colour table:
'; - - echo ' - - - - - - '; - foreach($icolour_count as $colour => $qty) { - echo ""; - $num_cans+=+ceil(($qty * ($wood_size * $wood_size))/2.2); - } - - echo '
ColourNameCoverage (m^2)Cans needed
" . $ironlak_colours[$colour] . "" . $qty * ($wood_size * $wood_size) . "" . ceil(($qty * ($wood_size * $wood_size))/2.2) . "
'; - - echo '
'; - echo '
'; - - echo 'Paint cost:
'; - - $cost_per_can = 6.36; - if($num_cans <= 199 ) { - $cost_per_can = 6.60; - } - - if($num_cans <= 59 ) { - $cost_per_can = 6.76; - } - - if($num_cans <= 35 ) { - $cost_per_can = 7; - } - - if($num_cans <= 11 ) { - $cost_per_can = 7.16; - } - - if($num_cans <= 5 ) { - $cost_per_can = 7.65; - } - $paint_cost = $num_cans * $cost_per_can; - echo "$num_cans cans @ $cost_per_can each: $paint_cost"; - echo '

'; - - $total_cost = $wood_cost + $paint_cost; - echo '
Total cost (wood+paint): ' . $total_cost . '
'; + $pixel_array = image_to_array($_FILES["fileToUpload"]["tmp_name"]); + $sprite_width_px = count($pixel_array[0]); + $sprite_height_px = count($pixel_array); + $colour_map = generate_colourmap($pixel_array, $ironlak_colours); + $original_sprite = array_to_sprite($pixel_array); + $ironlak_sprite = array_to_sprite($pixel_array, $colour_map); + + $lumber_breakdown = lumber_breakdown($_POST['wood_cost'], $_POST['wood_length'], $_POST['wood_size'], count_pixels($pixel_array)); + + $original_colour_table = colour_count_table(colour_count($pixel_array)); + $ironlak_colour_table = colour_count_table(colour_count($pixel_array, $colour_map)); + + $spray_can_breakdown = colour_breakdown(colour_count($pixel_array, $colour_map), $_POST['wood_size'], 2.2, 3, $ironlak_colours, $discount_matrix); - } + $cost_of_cans = spray_can_cost(colour_count($pixel_array, $colour_map), $_POST['wood_size'], 2.2, 3, $discount_matrix); + $value_of_can_used = spray_can_cost(colour_count($pixel_array, $colour_map), $_POST['wood_size'], 2.2, 3, $discount_matrix, TRUE); +// echo $original_sprite; +// echo $original_colour_table; +// +// echo $ironlak_sprite; +// echo $ironlak_colour_table; +// +// echo $lumber_breakdown; +// echo $spray_can_breakdown; + + require_once('./breakdown.html'); } ?> - - - - - \ No newline at end of file -- 2.11.0