a37b47674e42fb117956c1392060e4a397c642b8
3 function colourDiff($rgb1, $rgb2) {
4 $red1 = hexdec(substr($rgb1,0,2));
5 $green1 = hexdec(substr($rgb1,2,2));
6 $blue1 = hexdec(substr($rgb1,4,2));
8 $red2 = hexdec(substr($rgb2,0,2));
9 $green2 = hexdec(substr($rgb2,2,2));
10 $blue2 = hexdec(substr($rgb2,4,2));
12 return sqrt(pow($red1 - $red2, 2) +
pow($green1 - $green2, 2) +
pow($blue1 - $blue2, 2)) ;
15 function closest_colour($rgb, array $colours) {
19 foreach($colours as $hex => $name)
21 if(colourDiff($rgb, $hex) < $smallest_diff) {
22 $smallest_diff = colourDiff($rgb, $hex);
30 function array_to_table($array) {
31 $headings = $array[0];
33 $output = '<table class="table"><tr>';
35 foreach($headings as $heading) {
36 $output .= '<th>' . $heading . '</th>';
41 foreach($array as $row) {
44 foreach($row as $column) {
45 $output .= '<td>' . $column . '</td>';
51 $output .= '</table>';
55 function image_to_array($image) {
56 $dimensions = getimagesize($image);
57 $image = imagecreatefrompng($image);
60 for($i=0; $i<$dimensions[1]; $i++
) {
61 for($j=0; $j<$dimensions[0]; $j++
) {
62 $rgb = imagecolorsforindex($image, imagecolorat($image, $j, $i));
69 str_pad(dechex($rgb['red']), 2, '0', STR_PAD_LEFT
) .
70 str_pad(dechex($rgb['green']), 2, '0', STR_PAD_LEFT
) .
71 str_pad(dechex($rgb['blue']), 2, '0', STR_PAD_LEFT
);
73 if($a == 127) $hex = "NONE";
75 $array[$i][$j] = $hex;
82 function array_to_sprite($array, $colourmap = NULL
) {
83 $width = count($array[0]);
84 $height = count($array);
85 $output = "<table border='0' cellpadding='0' cellspacing='0'>";
86 for($i=0; $i<$height; $i++
) {
88 for($j=0; $j<$width; $j++
) {
89 $hex = $colourmap ?
$colourmap[$array[$i][$j]] : $array[$i][$j];
90 $output .= $array[$i][$j] !== 'NONE' ?
"<td class='$hex' bgcolor='#$hex'></td>" : "<td bgcolor='#FFFFFF'></td>";
96 $output .= '</table>';
101 function generate_colourmap($array, $new_palette) {
102 $width = count($array[0]);
103 $height = count($array);
104 $colourMap = array('NONE' => 'NONE');
106 for($i=0; $i<$height; $i++
) {
107 for($j=0; $j<$width; $j++
) {
108 $colour = $array[$i][$j];
109 if(!isset($colourMap[$colour]))
110 $colourMap[$colour] = closest_colour($colour, $new_palette);
117 function count_pixels($array) {
118 $width = count($array[0]);
119 $height = count($array);
122 for($i=0; $i<$height; $i++
) {
123 for($j=0; $j<$width; $j++
) {
124 if($array[$i][$j] !== 'NONE') $count++
;
131 function lumber_breakdown($cost_per_length, $wood_length, $wood_size, $num_blocks) {
132 $lumber_percent = $num_blocks*$wood_size/$wood_length;
133 $lengths_required = ceil($lumber_percent);
134 $total_lumber_cost = $lengths_required * $cost_per_length;
136 return array_to_table(
140 'No of ' . $wood_length . 'm lengths required',
142 'Cost @ ' . $cost_per_length . ' per length',
148 round($lumber_percent,4),
150 round($lumber_percent*$cost_per_length,4)
156 function lumber_cost($cost_per_length, $wood_length, $wood_size, $num_blocks, $percent_cost = FALSE
) {
157 $percent = $num_blocks*$wood_size/$wood_length;
158 $lengths_required = ceil($percent);
159 return $percent_cost ?
$percent * $cost_per_length : $lengths_required * $cost_per_length;
162 function colour_count($array, $colour_map = NULL
) {
163 $width = count($array[0]);
164 $height = count($array);
165 $colour_count = array();
167 for($i=0; $i<$height; $i++
) {
168 for($j=0; $j<$width; $j++
) {
169 $colour = isset($colour_map[$array[$i][$j]]) ?
$colour_map[$array[$i][$j]] : $array[$i][$j];
170 $colour_count[$colour] = isset($colour_count[$colour]) ?
$colour_count[$colour]+
1 : 1;
174 return $colour_count;
177 function colour_count_table($colour_count) {
178 $output = '<table class="table table-hover table-condensed"><tr><th>Colour</th><th>RGB</th><th>Num blocks</th></tr>';
180 foreach($colour_count as $colour => $count) {
181 //<tr class='colour-row' id='$colour'><td bgcolor='$colour'></td><td>$colour</td><td>$qty</td></tr>
182 if($colour != 'NONE')
183 $output .= '<tr class="colour-row" colour="' . $colour . '"><td bgcolor="' . $colour . '"></td><td>' . $colour . '</td><td>' . $count . '</td></tr>';
186 $output .= '</table>';
191 function colour_breakdown($colour_count, $block_size, $can_coverage, $coats, $colour_names, $discount_matrix) {
192 $output = '<table class="table table-hover table-condensed"><tr><th>Colour</th><th>Name</th><th>Num cans</th><th>Can %</th><th>Full price</th><th>% Price</th></tr>';
193 $cost_per_can = $discount_matrix[0];
196 foreach($colour_count as $colour => $count) {
197 $total_cans +
= ceil($count * $block_size * $block_size / $can_coverage);
200 foreach($discount_matrix as $num_needed => $cost) {
201 if($total_cans <= $num_needed)
202 $cost_per_can = $cost;
205 foreach($colour_count as $colour => $count) {
206 $can_percent = $count * $block_size * $block_size * $coats / $can_coverage;
207 $num_needed = ceil($can_percent);
208 $running_cost_percent = isset($running_cost_percent) ?
$running_cost_percent +
$can_percent*end($discount_matrix) : $can_percent*end($discount_matrix);
209 if(isset($colour_names[$colour]))
210 $output .= '<tr class="colour-row" colour="' . $colour . '"><td bgcolor="' . $colour . '"></td><td>' . $colour_names[$colour] . '</td><td>' . $num_needed . '</td><td>' . round($can_percent,4) . '</td><td>' . $cost_per_can*$num_needed . '</td><td>' . round($can_percent * $cost_per_can,4) . '</td></tr>';
213 $output .= '<tr><td colspan="2"></td><td colspan="2"><strong>' . $total_cans . '</strong></strong></td><td><strong>'. $total_cans*$cost_per_can . '</strong></td><td><strong>' . round($running_cost_percent, 4) . '</strong></td></table>';
218 function spray_can_cost($colour_count, $block_size, $can_coverage, $coats, $discount_matrix, $percent_cost = FALSE
) {
219 $cost_per_can = $discount_matrix[0];
222 foreach($colour_count as $colour => $count) {
223 $total_cans +
= $percent_cost ?
$count * $block_size * $block_size * $coats / $can_coverage : ceil($count * $block_size * $block_size / $can_coverage);
226 foreach($discount_matrix as $num_needed => $cost) {
227 if($total_cans <= $num_needed)
228 $cost_per_can = $cost;
231 return round($percent_cost ?
end($discount_matrix)*$total_cans : $total_cans*$cost_per_can,4);
234 if(isset($_POST["submit"]) ||
isset($_GET['sprite'])) {
235 if(isset($_POST['submit'])) {
236 if(isset($_FILES['fileToUpload'])) {
237 $image = $_FILES['fileToUpload']['tmp_name'];
240 if(isset($_POST['sprite'])) {
241 $image = "uploads/" . $_POST['sprite'] . '.png';
245 if(isset($_GET['sprite'])) {
246 $image = 'uploads/' . $_GET['sprite'] . '.png';
249 $hash = substr(md5_file($image), 0, 8);
251 $ironlak_colours = array(
257 "fcf2e0" => "Fraser",
259 "be854b" => "Acacia",
260 "e5b53a" => "Tanami",
263 "ffe04e" => "Pineapple Park",
264 "fffac4" => "Sorbet",
266 "de791b" => "Dieci Poes",
267 "f47d31" => "Halloween",
268 "f58427" => "Tues Afterburn",
270 "fdd29d" => "Smoothie",
271 "fcbc5e" => "Eureka",
272 "fed378" => "Butter",
275 "571b1e" => "Sirum Black-Red",
276 "8b0a02" => "Matador",
277 "d20d44" => "Soviet",
278 "e51a23" => "Autumn",
279 "cd4552" => "Isla Rose",
282 "9c0159" => "Moberry",
284 "f38ab4" => "Potion",
285 "f6aec4" => "Delicious",
288 "dd9ba5" => "Fantasia",
289 "fde6e0" => "Teegs Love",
291 "7e3e98" => "Pose Sushi",
294 "c2739d" => "Furious",
297 "0a024e" => "Panther",
298 "62609a" => "Eggplant",
299 "7f76b7" => "Phantom",
300 "7d81be" => "Sofles Violence",
301 "8ba4d4" => "Granny",
303 "231f20" => "Augor's Blackout",
304 "00447b" => "Midnight",
305 "0068a9" => "Phat1's True Royal",
307 "00aeef" => "Enue's Bonggg!",
308 "78bde8" => "Torquay",
309 "89d3f4" => "Atmosphere",
312 "006b6e" => "Neverland",
313 "007b85" => "Hunter",
315 "2abdb1" => "Frazetta",
316 "8fd1c5" => "Linz Iceberg",
317 "c0e3da" => "Placid",
322 "6cb43f" => "Cameleon",
323 "b3d78b" => "Reals Sublime",
324 "e6eebc" => "Whizbang",
326 "514e25" => "Askew's Olivia",
327 "677717" => "Guacamole",
328 "9ea615" => "Gangrene",
329 "ac9601" => "Banos Asbestos",
330 "d4d110" => "Kryptonite",
334 "434c3d" => "Lazy Grey",
335 "a0a1a5" => "Battleship",
336 "c5c6c8" => "Washington",
337 "f0f1f2" => "Smoulder",
340 "a8a9ad" => "Bright Chrome",
342 "ffffff" => "Whitest Possible",
343 "000000" => "Blackest Possible"
346 $discount_matrix = array(
347 0 => 6.36, //120+ cans
348 199 => 6.6, //60-199 cans
349 59 => 6.76, //36-59 cans
350 35 => 7, //12-35 cans
351 11 => 7.16, //6-11 cans
352 5 => 7.95 // 0-5 cans
355 if(isset($_POST['submit'])) {
356 $vars['wood_size'] = $_POST['wood_size'];
357 $vars['wood_length'] = $_POST['wood_length'];
358 $vars['wood_cost'] = $_POST['wood_cost'];
359 $vars['markup'] = $_POST['markup'];
360 } elseif(file_exists('uploads/'.$hash.'.txt')) {
361 $vars= unserialize(file_get_contents('uploads/'.$hash.'.txt'));
364 $pixel_array = image_to_array($image);
365 $sprite_width_cm = count($pixel_array[0]) * $vars['wood_size'] * 100 . "cm";
366 $sprite_height_cm = count($pixel_array) * $vars['wood_size'] * 100 . "cm";
367 $colour_map = generate_colourmap($pixel_array, $ironlak_colours);
368 $original_sprite = array_to_sprite($pixel_array);
369 $ironlak_sprite = array_to_sprite($pixel_array, $colour_map);
371 $lumber_breakdown = lumber_breakdown($vars['wood_cost'], $vars['wood_length'], $vars['wood_size'], count_pixels($pixel_array));
373 $original_colour_table = colour_count_table(colour_count($pixel_array));
374 $ironlak_colour_table = colour_count_table(colour_count($pixel_array, $colour_map));
376 $spray_can_breakdown = colour_breakdown(colour_count($pixel_array, $colour_map), $vars['wood_size'], 2.2, 3, $ironlak_colours, $discount_matrix);
378 $cost_of_cans = spray_can_cost(colour_count($pixel_array, $colour_map), $vars['wood_size'], 2.2, 3, $discount_matrix);
379 $value_of_can_used = spray_can_cost(colour_count($pixel_array, $colour_map), $vars['wood_size'], 2.2, 3, $discount_matrix, TRUE
);
380 $cost_of_lumber = lumber_cost($vars['wood_cost'], $vars['wood_length'], $vars['wood_size'], count_pixels($pixel_array));
381 $value_of_lumber_used = lumber_cost($vars['wood_cost'], $vars['wood_length'], $vars['wood_size'], count_pixels($pixel_array), TRUE
);
383 $markup = $vars['markup'];
385 $filename = 'uploads/' . $hash . '.png';
387 if(!file_exists($filename)) {
388 move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $filename);
389 $fp = fopen('uploads/'. $hash . '.txt', 'w');
390 fwrite($fp, serialize($vars));
393 if(!isset($_GET['sprite'])) {
394 header("location: upload.php?sprite=$hash");
397 require_once('./breakdown.html');