forked from Sleepcap/vDiplomacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapresize.php
238 lines (206 loc) · 7.98 KB
/
mapresize.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
// For security reasons fileupload and all variables are ussually discarded in header.php. Save this in constants.
$uploadtmp =(isset($_FILES['imgfile']['tmp_name']))? $_FILES['imgfile']['tmp_name'] : '' ; // the tmp-filename from PHP
define('UPTMP',$uploadtmp);
require_once('header.php');
if ($_SERVER['REQUEST_METHOD'] != "POST") {
libHTML::starthtml();
print '<div class="content">';
$edit=true;
if (!($User->type['Admin']))
if (!(array_key_exists($User->username, Config::$devs)))
$edit = 'false';
// Admins only
if ($edit=true) {
print '<li class="formlisttitle"><form enctype="multipart/form-data" action="'.$_SERVER['PHP_SELF'].'" method="POST">';
print '<input type="hidden" name="MAX_FILE_SIZE" value="100000" />';
print 'Choose a picture in PNG-format to upload: <input name="imgfile" type="file" />';
print '<li class="formlisttitle">Target resolution: Width (max 2000): <input type="text" name="new_x" size="4">';
print ' - Height (max 2000): <input type="text" name="new_y" size="4"><br>';
print '<li class="formlisttitle"><input type="submit" value="Upload File" />';
print '</form>';
} else {
print "Admins and devs only";
}
print '</div>';
libHTML::footer();
} else {
include_once ("lib/cache.php");
set_time_limit(190);
ini_set('memory_limit','100M');
$new_x = (isset($_POST['new_x'])) ? (int)$_POST['new_x'] : '250'; //
$new_y = (isset($_POST['new_y'])) ? (int)$_POST['new_y'] : '250'; //
if ($new_x < 100) $new_x=100;
if ($new_x > 2000) $new_x=2000;
if ($new_y < 100) $new_y=100;
if ($new_y > 2000) $new_y=2000;
$img=imagecreatefromstring(file_get_contents(UPTMP));
if (imageistruecolor($img))
imagetruecolortopalette($img, false, 200);
$width=imagesx($img);
$height=imagesy($img);
// Output original Image:
imagepng($img, libCache::dirID('users',$User->id).'/resize_orig.png');
// Create BW:
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
for ( $x=0; ($x < $width); $x++) {
for ($y=0; ($y<$height); $y++) {
$rgb = imagecolorat($img, $x, $y);
$cols = imagecolorsforindex($img, $rgb);
if (($cols['red'] < 10) &&($cols['green'] < 10) &&($cols['blue'] < 10) )
imagesetpixel($img,$x,$y,$black);
else
imagesetpixel($img,$x,$y,$white);
}
}
imagepng($img,libCache::dirID('users',$User->id).'/resize_bw.png');
// Color the territories:
for ( $x=0; ($x < $width); $x++) {
for ($y=0; ($y<$height); $y++) {
$rgb = imagecolorat($img, $x, $y);
if ($rgb == $white)
{
$r=rand(0,255); $g=rand(0,255); $b=rand(0,255);
$col=imagecolorallocate($img,$r,$g,$b);
if (!$col)
$col = imageColorClosest($img, $r, $g, $b);
imagefilltoborder($img,$x,$y,$black,$col);
}
}
}
imagepng($img,libCache::dirID('users',$User->id).'/resize_col.png');
// Remove the borders:
for ($i=0; $i<7; $i++) {
for ($y=0; ($y < $height-1); $y++) {
for ( $x=0; ($x < $width); $x++) {
if ((imagecolorat($img, $x, $y)) == $black) {
imagesetpixel($img,$x,$y,imagecolorat($img, $x, $y+1));
}
}
}
for ( $x=0; ($x < $width-1); $x++) {
for ($y=0; ($y<$height); $y++) {
if ((imagecolorat($img, $x, $y)) == $black) {
imagesetpixel($img,$x,$y,imagecolorat($img, $x+1, $y));
}
}
}
}
imagepng($img,libCache::dirID('users',$User->id).'/resize_wo_br.png');
// create new picture
$img_new=imagecreate($new_x,$new_y);
imagecopyresized($img_new,$img,0,0,0,0,$new_x,$new_y,$width,$height);
imagedestroy($img);
imagepng($img_new,libCache::dirID('users',$User->id).'/resize_new.png');
// Add the borders:
$black=imagecolorallocate($img_new,0,0,0);
for ( $x=0; ($x < $new_x); $x++) {
for ($y=0; ($y<$new_y -1 ); $y++) {
$col1=imagecolorat($img_new, $x, $y);
$col2=imagecolorat($img_new, $x, $y+1);
if (($col1 != $col2) && ($col1 != $black)) {
imagesetpixel($img_new,$x,$y,$black);
}
}
}
for ($y=0; ($y < $new_y -1); $y++) {
for ( $x=0; ($x < $new_x-1); $x++) {
$col1=imagecolorat($img_new, $x, $y);
$col2=imagecolorat($img_new, $x+1, $y);
if (($col1 != $col2) && ($col1 != $black) && ($col2 != $black)) {
imagesetpixel($img_new,$x,$y,$black);
}
}
}
imagepng($img_new,libCache::dirID('users',$User->id).'/resize_new_br.png');
// Enhance the borders:
// Soften the edges:
for ( $x=1; ($x < $new_x -1 ); $x++) {
for ($y=1; ($y<$new_y -1 ); $y++) {
$col1=imagecolorat($img_new, $x, $y-1);
$col2=imagecolorat($img_new, $x-1, $y);
$col3=imagecolorat($img_new, $x, $y);
$col4=imagecolorat($img_new, $x+1, $y);
$col5=imagecolorat($img_new, $x, $y+1);
if ($col3 == $black) {
if (($col1 == $black) && ($col2 == $black))
imagesetpixel($img_new,$x,$y,$col4);
if (($col5 == $black) && ($col2 == $black))
imagesetpixel($img_new,$x,$y,$col4);
if (($col1 == $black) && ($col4 == $black))
imagesetpixel($img_new,$x,$y,$col2);
if (($col5 == $black) && ($col4 == $black))
imagesetpixel($img_new,$x,$y,$col2);
}
}
}
// Enhance the stairs (a bit)
for ( $x=1; ($x < $new_x - 3 ); $x++) {
for ($y=1; ($y<$new_y - 3 ); $y++) {
$col1=imagecolorat($img_new, $x, $y );
$col2=imagecolorat($img_new, $x+1, $y );
$col3=imagecolorat($img_new, $x+2, $y );
$col4=imagecolorat($img_new, $x, $y+1);
$col5=imagecolorat($img_new, $x+1, $y+1);
$col6=imagecolorat($img_new, $x+2, $y+1);
$col7=imagecolorat($img_new, $x, $y+2);
$col8=imagecolorat($img_new, $x+1, $y+2);
$col9=imagecolorat($img_new, $x+2, $y+2);
$chk1=imagecolorat($img_new, $x-1, $y );
$chk2=imagecolorat($img_new, $x , $y-1);
$chk3=imagecolorat($img_new, $x+2, $y-1);
$chk4=imagecolorat($img_new, $x+3, $y );
$chk5=imagecolorat($img_new, $x+3, $y+2);
$chk6=imagecolorat($img_new, $x+2, $y+3);
$chk7=imagecolorat($img_new, $x , $y+3);
$chk8=imagecolorat($img_new, $x-1, $y+2);
if (($col2 == $black) && ($col3 == $black) && ($col4 == $black) && ($col7 == $black) && ($col1 != $black)) {
if (($chk1 != $black) && ($chk2 != $black)) {
imagesetpixel($img_new,$x+1,$y ,$col1);
imagesetpixel($img_new,$x ,$y+1,$col1);
imagesetpixel($img_new,$x+1,$y+1,$black);
}}
if (($col7 == $black) && ($col8 == $black) && ($col6 == $black) && ($col3 == $black) && ($col9 != $black)) {
if (($chk5 != $black) && ($chk6 != $black)) {
imagesetpixel($img_new,$x+2,$y+1,$col9);
imagesetpixel($img_new,$x+1,$y+2,$col9);
imagesetpixel($img_new,$x+1,$y+1,$black);
}}
if (($col1 == $black) && ($col2 == $black) && ($col6 == $black) && ($col9 == $black) && ($col3 != $black)) {
if (($chk3 != $black) && ($chk4 != $black)) {
imagesetpixel($img_new,$x+1,$y ,$col3);
imagesetpixel($img_new,$x+2,$y+1,$col3);
imagesetpixel($img_new,$x+1,$y+1,$black);
}}
if (($col1 == $black) && ($col4 == $black) && ($col8 == $black) && ($col9 == $black) && ($col7 != $black)) {
if (($chk8 != $black) && ($chk7 != $black)) {
imagesetpixel($img_new,$x ,$y+1,$col7);
imagesetpixel($img_new,$x+1,$y+2,$col7);
imagesetpixel($img_new,$x+1,$y+1,$black);
}}
}
}
imagepng($img_new,libCache::dirID('users',$User->id).'/resize_new_br_enh.png');
imagedestroy($img_new);
require_once('header.php');
libHTML::starthtml();
print '<div class="content">';
print "Original:<hr>";
print '<img src="'.libCache::dirID('users',$User->id).'/resize_orig.png">';
print "<hr>BW:<hr>";
print '<img src="'.libCache::dirID('users',$User->id).'/resize_bw.png" >';
print "<hr>Col:<hr>";
print '<img src="'.libCache::dirID('users',$User->id).'/resize_col.png">';
print "<hr>Borders removed:<hr>";
print '<img src="'.libCache::dirID('users',$User->id).'/resize_wo_br.png">';
print "<hr>New size:<hr>";
print '<img src="'.libCache::dirID('users',$User->id).'/resize_new.png">';
print "<hr>Borders:<hr>";
print '<img src="'.libCache::dirID('users',$User->id).'/resize_new_br.png">';
print "<hr>Enhanched borders:<hr>";
print '<img src="'.libCache::dirID('users',$User->id).'/resize_new_br_enh.png">';
print '</div>';
libHTML::footer();
}
?>