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(!empty($_FILES['file'])) {
235 $image = $_FILES['file']['tmp_name'];
236 $hash = substr(md5_file($image), 0, 8);
237 $filename = 'uploads/' . $hash . '.png';
239 if(!file_exists($filename)) {
240 move_uploaded_file($_FILES["file"]["tmp_name"], $filename);
246 if(isset($_POST['hash'])) {
247 $hash = $_POST['hash'];
249 $vars['spritename'] = $_POST['spritename'];
250 $vars['wood_size'] = $_POST['wood_size'];
251 $vars['wood_length'] = $_POST['wood_length'];
252 $vars['wood_cost'] = $_POST['wood_cost'];
253 $vars['markup'] = $_POST['markup'];
255 $fp = fopen('uploads/'. $hash . '.txt', 'w');
256 fwrite($fp, serialize($vars));
258 header("Location: upload.php?sprite=$hash");
261 if(isset($_GET['sprite'])) {
262 $hash = $_GET['sprite'];
263 $image = 'uploads/' . $_GET['sprite'] . '.png';
265 $ironlak_colours = array(
271 "fcf2e0" => "Fraser",
273 "be854b" => "Acacia",
274 "e5b53a" => "Tanami",
277 "ffe04e" => "Pineapple Park",
278 "fffac4" => "Sorbet",
280 "de791b" => "Dieci Poes",
281 "f47d31" => "Halloween",
282 "f58427" => "Tues Afterburn",
284 "fdd29d" => "Smoothie",
285 "fcbc5e" => "Eureka",
286 "fed378" => "Butter",
289 "571b1e" => "Sirum Black-Red",
290 "8b0a02" => "Matador",
291 "d20d44" => "Soviet",
292 "e51a23" => "Autumn",
293 "cd4552" => "Isla Rose",
296 "9c0159" => "Moberry",
298 "f38ab4" => "Potion",
299 "f6aec4" => "Delicious",
302 "dd9ba5" => "Fantasia",
303 "fde6e0" => "Teegs Love",
305 "7e3e98" => "Pose Sushi",
308 "c2739d" => "Furious",
311 "0a024e" => "Panther",
312 "62609a" => "Eggplant",
313 "7f76b7" => "Phantom",
314 "7d81be" => "Sofles Violence",
315 "8ba4d4" => "Granny",
317 "231f20" => "Augor's Blackout",
318 "00447b" => "Midnight",
319 "0068a9" => "Phat1's True Royal",
321 "00aeef" => "Enue's Bonggg!",
322 "78bde8" => "Torquay",
323 "89d3f4" => "Atmosphere",
326 "006b6e" => "Neverland",
327 "007b85" => "Hunter",
329 "2abdb1" => "Frazetta",
330 "8fd1c5" => "Linz Iceberg",
331 "c0e3da" => "Placid",
336 "6cb43f" => "Cameleon",
337 "b3d78b" => "Reals Sublime",
338 "e6eebc" => "Whizbang",
340 "514e25" => "Askew's Olivia",
341 "677717" => "Guacamole",
342 "9ea615" => "Gangrene",
343 "ac9601" => "Banos Asbestos",
344 "d4d110" => "Kryptonite",
348 "434c3d" => "Lazy Grey",
349 "a0a1a5" => "Battleship",
350 "c5c6c8" => "Washington",
351 "f0f1f2" => "Smoulder",
354 "a8a9ad" => "Bright Chrome",
356 "ffffff" => "Whitest Possible",
357 "000000" => "Blackest Possible"
360 $discount_matrix = array(
361 0 => 6.36, //120+ cans
362 199 => 6.6, //60-199 cans
363 59 => 6.76, //36-59 cans
364 35 => 7, //12-35 cans
365 11 => 7.16, //6-11 cans
366 5 => 7.95 // 0-5 cans
369 $vars= unserialize(file_get_contents('uploads/'.$hash.'.txt'));
371 $pixel_array = image_to_array($image);
372 $sprite_width_cm = count($pixel_array[0]) * $vars['wood_size'] * 100 . "cm";
373 $sprite_height_cm = count($pixel_array) * $vars['wood_size'] * 100 . "cm";
374 $colour_map = generate_colourmap($pixel_array, $ironlak_colours);
375 $original_sprite = array_to_sprite($pixel_array);
376 $ironlak_sprite = array_to_sprite($pixel_array, $colour_map);
378 $lumber_breakdown = lumber_breakdown($vars['wood_cost'], $vars['wood_length'], $vars['wood_size'], count_pixels($pixel_array));
380 $original_colour_table = colour_count_table(colour_count($pixel_array));
381 $ironlak_colour_table = colour_count_table(colour_count($pixel_array, $colour_map));
383 $spray_can_breakdown = colour_breakdown(colour_count($pixel_array, $colour_map), $vars['wood_size'], 2.2, 3, $ironlak_colours, $discount_matrix);
385 $cost_of_cans = spray_can_cost(colour_count($pixel_array, $colour_map), $vars['wood_size'], 2.2, 3, $discount_matrix);
386 $value_of_can_used = spray_can_cost(colour_count($pixel_array, $colour_map), $vars['wood_size'], 2.2, 3, $discount_matrix, TRUE
);
387 $cost_of_lumber = lumber_cost($vars['wood_cost'], $vars['wood_length'], $vars['wood_size'], count_pixels($pixel_array));
388 $value_of_lumber_used = lumber_cost($vars['wood_cost'], $vars['wood_length'], $vars['wood_size'], count_pixels($pixel_array), TRUE
);
389 $name = $vars['spritename'];
390 $markup = $vars['markup'];
392 require_once('./breakdown.html');