a37b47674e42fb117956c1392060e4a397c642b8
[pixel-calc.git] / public_html / upload.php
1 <?php
2
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));
7
8 $red2 = hexdec(substr($rgb2,0,2));
9 $green2 = hexdec(substr($rgb2,2,2));
10 $blue2 = hexdec(substr($rgb2,4,2));
11
12 return sqrt(pow($red1 - $red2, 2) + pow($green1 - $green2, 2) + pow($blue1 - $blue2, 2)) ;
13 }
14
15 function closest_colour($rgb, array $colours) {
16 $smallest_diff = 766;
17 $closest = 'none';
18
19 foreach($colours as $hex => $name)
20 {
21 if(colourDiff($rgb, $hex) < $smallest_diff) {
22 $smallest_diff = colourDiff($rgb, $hex);
23 $closest = $hex;
24 }
25 }
26
27 return $closest;
28 }
29
30 function array_to_table($array) {
31 $headings = $array[0];
32 unset($array[0]);
33 $output = '<table class="table"><tr>';
34
35 foreach($headings as $heading) {
36 $output .= '<th>' . $heading . '</th>';
37 }
38
39 $output .= '</tr>';
40
41 foreach($array as $row) {
42 $output .= '<tr>';
43
44 foreach($row as $column) {
45 $output .= '<td>' . $column . '</td>';
46 }
47
48 $output .= '</tr>';
49 }
50
51 $output .= '</table>';
52 return $output;
53 }
54
55 function image_to_array($image) {
56 $dimensions = getimagesize($image);
57 $image = imagecreatefrompng($image);
58 $array = array();
59
60 for($i=0; $i<$dimensions[1]; $i++) {
61 for($j=0; $j<$dimensions[0]; $j++) {
62 $rgb = imagecolorsforindex($image, imagecolorat($image, $j, $i));
63 $r = $rgb['red'];
64 $g = $rgb['green'];
65 $b = $rgb['blue'];
66 $a = $rgb['alpha'];
67
68 $hex =
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);
72
73 if($a == 127) $hex = "NONE";
74
75 $array[$i][$j] = $hex;
76 }
77 }
78
79 return $array;
80 }
81
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++) {
87 $output .= '<tr>';
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>";
91 }
92
93 $output .= '</tr>';
94 }
95
96 $output .= '</table>';
97
98 return $output;
99 }
100
101 function generate_colourmap($array, $new_palette) {
102 $width = count($array[0]);
103 $height = count($array);
104 $colourMap = array('NONE' => 'NONE');
105
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);
111 }
112 }
113
114 return $colourMap;
115 }
116
117 function count_pixels($array) {
118 $width = count($array[0]);
119 $height = count($array);
120 $count = 0;
121
122 for($i=0; $i<$height; $i++) {
123 for($j=0; $j<$width; $j++) {
124 if($array[$i][$j] !== 'NONE') $count++;
125 }
126 }
127
128 return $count;
129 }
130
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;
135
136 return array_to_table(
137 array(
138 array(
139 'Num blocks',
140 'No of ' . $wood_length . 'm lengths required',
141 '% used',
142 'Cost @ ' . $cost_per_length . ' per length',
143 '% Price'
144 ),
145 array(
146 $num_blocks,
147 $lengths_required,
148 round($lumber_percent,4),
149 $total_lumber_cost,
150 round($lumber_percent*$cost_per_length,4)
151 )
152 )
153 );
154 }
155
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;
160 }
161
162 function colour_count($array, $colour_map = NULL) {
163 $width = count($array[0]);
164 $height = count($array);
165 $colour_count = array();
166
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;
171 }
172 }
173
174 return $colour_count;
175 }
176
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>';
179
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>';
184 }
185
186 $output .= '</table>';
187
188 return $output;
189 }
190
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];
194 $total_cans = 0;
195
196 foreach($colour_count as $colour => $count) {
197 $total_cans += ceil($count * $block_size * $block_size / $can_coverage);
198 }
199
200 foreach($discount_matrix as $num_needed => $cost) {
201 if($total_cans <= $num_needed)
202 $cost_per_can = $cost;
203 }
204
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>';
211 }
212
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>';
214
215 return $output;
216 }
217
218 function spray_can_cost($colour_count, $block_size, $can_coverage, $coats, $discount_matrix, $percent_cost = FALSE) {
219 $cost_per_can = $discount_matrix[0];
220 $total_cans = 0;
221
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);
224 }
225
226 foreach($discount_matrix as $num_needed => $cost) {
227 if($total_cans <= $num_needed)
228 $cost_per_can = $cost;
229 }
230
231 return round($percent_cost ? end($discount_matrix)*$total_cans : $total_cans*$cost_per_can,4);
232 }
233
234 if(isset($_POST["submit"]) || isset($_GET['sprite'])) {
235 if(isset($_POST['submit'])) {
236 if(isset($_FILES['fileToUpload'])) {
237 $image = $_FILES['fileToUpload']['tmp_name'];
238 }
239
240 if(isset($_POST['sprite'])) {
241 $image = "uploads/" . $_POST['sprite'] . '.png';
242 }
243 }
244
245 if(isset($_GET['sprite'])) {
246 $image = 'uploads/' . $_GET['sprite'] . '.png';
247 }
248
249 $hash = substr(md5_file($image), 0, 8);
250
251 $ironlak_colours = array(
252 "532f18" => "Earth",
253 "753e00" => "Swiss",
254 "be854b" => "Mocha",
255 "e5ba83" => "Cuppa",
256 "f9e6c4" => "Irwin",
257 "fcf2e0" => "Fraser",
258
259 "be854b" => "Acacia",
260 "e5b53a" => "Tanami",
261 "f5d29c" => "Oat",
262 "ffdc01" => "Blaze",
263 "ffe04e" => "Pineapple Park",
264 "fffac4" => "Sorbet",
265
266 "de791b" => "Dieci Poes",
267 "f47d31" => "Halloween",
268 "f58427" => "Tues Afterburn",
269 "faae5d" => "Bowen",
270 "fdd29d" => "Smoothie",
271 "fcbc5e" => "Eureka",
272 "fed378" => "Butter",
273 "feefbb" => "Bondi",
274
275 "571b1e" => "Sirum Black-Red",
276 "8b0a02" => "Matador",
277 "d20d44" => "Soviet",
278 "e51a23" => "Autumn",
279 "cd4552" => "Isla Rose",
280 "f2797e" => "Guava",
281
282 "9c0159" => "Moberry",
283 "ea4498" => "Flirt",
284 "f38ab4" => "Potion",
285 "f6aec4" => "Delicious",
286 "f9cbd7" => "Crush",
287 "be76ad" => "Dusk",
288 "dd9ba5" => "Fantasia",
289 "fde6e0" => "Teegs Love",
290
291 "7e3e98" => "Pose Sushi",
292 "592049" => "Venom",
293 "78486a" => "Vino",
294 "c2739d" => "Furious",
295 "d4aed1" => "Gypsy",
296
297 "0a024e" => "Panther",
298 "62609a" => "Eggplant",
299 "7f76b7" => "Phantom",
300 "7d81be" => "Sofles Violence",
301 "8ba4d4" => "Granny",
302
303 "231f20" => "Augor's Blackout",
304 "00447b" => "Midnight",
305 "0068a9" => "Phat1's True Royal",
306 "0082c8" => "Smurf",
307 "00aeef" => "Enue's Bonggg!",
308 "78bde8" => "Torquay",
309 "89d3f4" => "Atmosphere",
310 "b9e1f7" => "Ozone",
311
312 "006b6e" => "Neverland",
313 "007b85" => "Hunter",
314 "00b5cb" => "Reef",
315 "2abdb1" => "Frazetta",
316 "8fd1c5" => "Linz Iceberg",
317 "c0e3da" => "Placid",
318 "70c6a2" => "Jante",
319
320 "006224" => "Huey",
321 "3f9537" => "Field",
322 "6cb43f" => "Cameleon",
323 "b3d78b" => "Reals Sublime",
324 "e6eebc" => "Whizbang",
325
326 "514e25" => "Askew's Olivia",
327 "677717" => "Guacamole",
328 "9ea615" => "Gangrene",
329 "ac9601" => "Banos Asbestos",
330 "d4d110" => "Kryptonite",
331 "f4eb0a" => "Keen",
332 "fcf26f" => "Nitro",
333
334 "434c3d" => "Lazy Grey",
335 "a0a1a5" => "Battleship",
336 "c5c6c8" => "Washington",
337 "f0f1f2" => "Smoulder",
338
339 "a29161" => "Gold",
340 "a8a9ad" => "Bright Chrome",
341
342 "ffffff" => "Whitest Possible",
343 "000000" => "Blackest Possible"
344 );
345
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
353 );
354
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'));
362 }
363
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);
370
371 $lumber_breakdown = lumber_breakdown($vars['wood_cost'], $vars['wood_length'], $vars['wood_size'], count_pixels($pixel_array));
372
373 $original_colour_table = colour_count_table(colour_count($pixel_array));
374 $ironlak_colour_table = colour_count_table(colour_count($pixel_array, $colour_map));
375
376 $spray_can_breakdown = colour_breakdown(colour_count($pixel_array, $colour_map), $vars['wood_size'], 2.2, 3, $ironlak_colours, $discount_matrix);
377
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);
382
383 $markup = $vars['markup'];
384
385 $filename = 'uploads/' . $hash . '.png';
386
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));
391 }
392
393 if(!isset($_GET['sprite'])) {
394 header("location: upload.php?sprite=$hash");
395 }
396
397 require_once('./breakdown.html');
398 }
399 ?>