Better UI
[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(!empty($_FILES['file'])) {
235 $image = $_FILES['file']['tmp_name'];
236 $hash = substr(md5_file($image), 0, 8);
237 $filename = 'uploads/' . $hash . '.png';
238
239 if(!file_exists($filename)) {
240 move_uploaded_file($_FILES["file"]["tmp_name"], $filename);
241 }
242
243 die($hash);
244 }
245
246 if(isset($_POST['hash'])) {
247 $hash = $_POST['hash'];
248
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'];
254
255 $fp = fopen('uploads/'. $hash . '.txt', 'w');
256 fwrite($fp, serialize($vars));
257
258 header("Location: upload.php?sprite=$hash");
259 }
260
261 if(isset($_GET['sprite'])) {
262 $hash = $_GET['sprite'];
263 $image = 'uploads/' . $_GET['sprite'] . '.png';
264
265 $ironlak_colours = array(
266 "532f18" => "Earth",
267 "753e00" => "Swiss",
268 "be854b" => "Mocha",
269 "e5ba83" => "Cuppa",
270 "f9e6c4" => "Irwin",
271 "fcf2e0" => "Fraser",
272
273 "be854b" => "Acacia",
274 "e5b53a" => "Tanami",
275 "f5d29c" => "Oat",
276 "ffdc01" => "Blaze",
277 "ffe04e" => "Pineapple Park",
278 "fffac4" => "Sorbet",
279
280 "de791b" => "Dieci Poes",
281 "f47d31" => "Halloween",
282 "f58427" => "Tues Afterburn",
283 "faae5d" => "Bowen",
284 "fdd29d" => "Smoothie",
285 "fcbc5e" => "Eureka",
286 "fed378" => "Butter",
287 "feefbb" => "Bondi",
288
289 "571b1e" => "Sirum Black-Red",
290 "8b0a02" => "Matador",
291 "d20d44" => "Soviet",
292 "e51a23" => "Autumn",
293 "cd4552" => "Isla Rose",
294 "f2797e" => "Guava",
295
296 "9c0159" => "Moberry",
297 "ea4498" => "Flirt",
298 "f38ab4" => "Potion",
299 "f6aec4" => "Delicious",
300 "f9cbd7" => "Crush",
301 "be76ad" => "Dusk",
302 "dd9ba5" => "Fantasia",
303 "fde6e0" => "Teegs Love",
304
305 "7e3e98" => "Pose Sushi",
306 "592049" => "Venom",
307 "78486a" => "Vino",
308 "c2739d" => "Furious",
309 "d4aed1" => "Gypsy",
310
311 "0a024e" => "Panther",
312 "62609a" => "Eggplant",
313 "7f76b7" => "Phantom",
314 "7d81be" => "Sofles Violence",
315 "8ba4d4" => "Granny",
316
317 "231f20" => "Augor's Blackout",
318 "00447b" => "Midnight",
319 "0068a9" => "Phat1's True Royal",
320 "0082c8" => "Smurf",
321 "00aeef" => "Enue's Bonggg!",
322 "78bde8" => "Torquay",
323 "89d3f4" => "Atmosphere",
324 "b9e1f7" => "Ozone",
325
326 "006b6e" => "Neverland",
327 "007b85" => "Hunter",
328 "00b5cb" => "Reef",
329 "2abdb1" => "Frazetta",
330 "8fd1c5" => "Linz Iceberg",
331 "c0e3da" => "Placid",
332 "70c6a2" => "Jante",
333
334 "006224" => "Huey",
335 "3f9537" => "Field",
336 "6cb43f" => "Cameleon",
337 "b3d78b" => "Reals Sublime",
338 "e6eebc" => "Whizbang",
339
340 "514e25" => "Askew's Olivia",
341 "677717" => "Guacamole",
342 "9ea615" => "Gangrene",
343 "ac9601" => "Banos Asbestos",
344 "d4d110" => "Kryptonite",
345 "f4eb0a" => "Keen",
346 "fcf26f" => "Nitro",
347
348 "434c3d" => "Lazy Grey",
349 "a0a1a5" => "Battleship",
350 "c5c6c8" => "Washington",
351 "f0f1f2" => "Smoulder",
352
353 "a29161" => "Gold",
354 "a8a9ad" => "Bright Chrome",
355
356 "ffffff" => "Whitest Possible",
357 "000000" => "Blackest Possible"
358 );
359
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
367 );
368
369 $vars= unserialize(file_get_contents('uploads/'.$hash.'.txt'));
370
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);
377
378 $lumber_breakdown = lumber_breakdown($vars['wood_cost'], $vars['wood_length'], $vars['wood_size'], count_pixels($pixel_array));
379
380 $original_colour_table = colour_count_table(colour_count($pixel_array));
381 $ironlak_colour_table = colour_count_table(colour_count($pixel_array, $colour_map));
382
383 $spray_can_breakdown = colour_breakdown(colour_count($pixel_array, $colour_map), $vars['wood_size'], 2.2, 3, $ironlak_colours, $discount_matrix);
384
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'];
391
392 require_once('./breakdown.html');
393 }
394 ?>