Image editor PNG
Ik had zelf een PHP/Ajax based imageeditor gedownload. Nu heb ik uren ervoor nodig gehad om het in te bouwen in mijn systeem, nu heb ik het dan eindelijk werken.
Echter support dit systeem alleen JPEG, dus dacht ik, ik haal het betreffende stuk uit het bestand, en plaats het in image.jpeg.php. Ik koppieerde het, naar image.png.php en replaced alle jpeg tot png. In het hoofd bestand controlleerde ik dan de image op de mimetype en include zo het juiste bestand. Helaas is het in praktijk niet zo makkelijk.
JPEG werkt nogsteeds uitstekend, maar png wil niet werken. Dit is de source:
Code (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
239
240
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
239
240
<?php
switch($action){
case 'viewOriginal':
copy($originalDirectory.$imageName, $editDirectory.$imageName);
break;
case 'save':
copy($editDirectory.$imageName, $originalDirectory.$imageName);
break;
case 'resize': // additional required params: w, h
$out_w = $_GET['w'];
$out_h = $_GET['h'];
if (!is_numeric($out_w) || $out_w < 1 || $out_w > 2000 || !is_numeric($out_h) || $out_h < 1 || $out_h > 2000) { exit; }
list($in_w, $in_h) = getimagesize($editDirectory.$imageName);
$in = imagecreatefrompng($editDirectory.$imageName);
$out = imagecreatetruecolor($out_w, $out_h);
imagecopyresampled($out, $in, 0, 0, 0, 0, $out_w, $out_h, $in_w, $in_h);
imagepng($out, $editDirectory.$imageName, 100);
imagedestroy($in);
imagedestroy($out);
break;
case 'rotate': // additional required params: degrees (90, 180 or 270)
$degrees = $_GET['degrees'];
$in = imagecreatefrompng($editDirectory.$imageName);
$out = imagerotate($in, $degrees, 0);
imagepng($out, $editDirectory.$imageName, 100);
imagedestroy($in);
imagedestroy($out);
if (($degrees != 90 && $degrees != 180 && $degrees != 270)) { exit; }
$in = imagecreatefrompng($editDirectory.$imageName);
if ($degrees == 180){
$out = imagerotate($in, $degrees, 180);
}else{ // 90 or 270
$x = imagesx($in);
$y = imagesy ($in);
$max = max($x, $y);
$square = imagecreatetruecolor($max, $max);
imagecopy($square, $in, 0, 0, 0, 0, $x, $y);
$square = imageRotate($square, $degrees, 0);
$out = imagecreatetruecolor($y, $x);
if ($degrees == 90) {
imagecopy($out, $square, 0, 0, 0, $max - $x, $y, $x);
} elseif ($degrees == 270) {
imagecopy($out, $square, 0, 0, $max - $y, 0, $y, $x);
}
imagedestroy($square);
}
imagepng($out, $editDirectory.$imageName, 100);
imagedestroy($in);
imagedestroy($out);
break;
case 'crop': // additional required params: x, y, w, h
$x = $_GET['x'];
$y = $_GET['y'];
$w = $_GET['w'];
$h = $_GET['h'];
if (!is_numeric($x) || !is_numeric($y) || !is_numeric($w) || !is_numeric($h)) { exit; }
$in = imagecreatefrompng($editDirectory.$imageName);
$out = imagecreatetruecolor($w, $h);
imagecopyresampled($out, $in, 0, 0, $x, $y, $w, $h, $w, $h);
imagepng($out, $editDirectory.$imageName, 100);
imagedestroy($in);
imagedestroy($out);
break;
case 'reduceSaturation':
$in = imagecreatefrompng($editDirectory.$imageName);
$x_in = $_GET['x'];
$y_in = $_GET['y'];
$w = $_GET['w'];
$h = $_GET['h'];
for ($x=$x_in; $x<$x_in+$w; $x++) {
for ($y=$y_in; $y<$y_in+$h; $y++) {
$rgb = imagecolorat($in, $x, $y);
$rgb = imagecolorsforindex($in, $rgb);
$grey = ($rgb['red'] + $rgb['green'] + $rgb['blue']) / 3;
imagesetpixel($in, $x, $y, imagecolorallocate($in, $grey, $grey, $grey));
}
}
imagepng($in, $editDirectory.$imageName, 100);
imagedestroy($in);
break;
case 'invert':
$in = imagecreatefrompng($editDirectory.$imageName);
$x_in = $_GET['x'];
$y_in = $_GET['y'];
$w = $_GET['w'];
$h = $_GET['h'];
for ($x=$x_in; $x<$x_in+$w; $x++) {
for ($y=$y_in; $y<$y_in+$h; $y++) {
$rgb = imagecolorat($in, $x, $y);
$rgb = imagecolorsforindex($in, $rgb);
$r = 255 - $rgb['red'];
$g = 255 - $rgb['green'];
$b = 255 - $rgb['blue'];
imagesetpixel($in, $x, $y, imagecolorallocate($in, $r, $g, $b));
}
}
imagepng($in, $editDirectory.$imageName, 100);
imagedestroy($in);
break;
case 'sharpen':
$k = 0.3; // Sharpen (max 1.0)
$in = imagecreatefrompng($editDirectory.$imageName);
$w = imagesx($in);
$h = imagesy ($in);
$x_in = $_GET['x'];
$y_in = $_GET['y'];
$w_in = $_GET['w'];
$h_in = $_GET['h'];
$out = imagecreatetruecolor($w_in-2, $h_in-2);
for($x = $x_in+1; $x < $x_in+$w_in-1; $x++) {
for($y = $y_in+1; $y < $y_in+$h_in-1; $y++) {
$rgb[1][0] = imagecolorat($in, $x, $y-1);
$rgb[0][1] = imagecolorat($in, $x-1, $y);
$rgb[1][1] = imagecolorat($in, $x, $y);
$rgb[2][1] = imagecolorat($in, $x+1, $y);
$rgb[1][2] = imagecolorat($in, $x, $y+1);
$r = -$k *(($rgb[1][0] >> 16) & 0xFF) +
-$k *(($rgb[0][1] >> 16) & 0xFF) +
(1+4*$k) *(($rgb[1][1] >> 16) & 0xFF) +
-$k *(($rgb[2][1] >> 16) & 0xFF) +
-$k *(($rgb[1][2] >> 16) & 0xFF) ;
$g = -$k *(($rgb[1][0] >> 8) & 0xFF) +
-$k *(($rgb[0][1] >> 8) & 0xFF) +
(1+4*$k) *(($rgb[1][1] >> 8) & 0xFF) +
-$k *(($rgb[2][1] >> 8) & 0xFF) +
-$k *(($rgb[1][2] >> 8) & 0xFF) ;
$b = -$k *($rgb[1][0] & 0xFF) +
-$k *($rgb[0][1] & 0xFF) +
(1+4*$k) *($rgb[1][1] & 0xFF) +
-$k *($rgb[2][1] & 0xFF) +
-$k *($rgb[1][2] & 0xFF) ;
$r = min(255, max(0, $r));
$g = min(255, max(0, $g));
$b = min(255, max(0, $b));
if(!isset($cols[$r][$g][$b]))
{
$cols[$r][$g][$b] = imagecolorallocate($out, $r, $g, $b);
}
imagesetpixel($out, $x-$x_in-1, $y-$y_in-1, $cols[$r][$g][$b]);
}
}
$out2 = imagecreatetruecolor($w_in, $h_in);
imagecopyresized($out2, $out, 0, 0, 0, 0, $w_in, $h_in, $w_in-2, $h_in-2);
imagedestroy($out);
imagecopy($in, $out2, $x_in, $y_in, 0, 0, $w_in, $h_in);
imagedestroy($out2);
imagepng($in, $editDirectory.$imageName, 100);
imagedestroy($in);
break;
case 'contrast':
$brightness=0;
$maxb=0;
$minb=255;
$im = imagecreatefrompng($editDirectory.$imageName);
$w = imagesx($im);
$h = imagesy ($im);
for ($x=0; $x<$w; $x++) {
for ($y=0; $y<$h; $y++) {
$rgb=imagecolorat($im, $x, $y);
$rgb=imagecolorsforindex($im, $rgb);
$grey=0.2125*$rgb['red']+0.7154*$rgb['green']+0.0721*$rgb['blue'];
$brightness+=$grey;
if ($grey>$maxb) $maxb=$grey;
if ($grey<$minb) $minb=$grey;
}
}
$brightness=$brightness/($w*$h);
$minb=$brightness/($brightness-$minb);
$maxb=(255-$brightness)/($maxb-$brightness);
$contrast=min($minb, $maxb);
for ($x=0; $x<$w; $x++) {
for ($y=0; $y<$h; $y++) {
$rgb=imagecolorat($im, $x, $y);
$rgb=imagecolorsforindex($im, $rgb);
imagesetpixel($im, $x, $y,
65536*floor(min($rgb['red']*$contrast, 255))+
256*floor(min($rgb['green']*$contrast, 255))+
floor(min($rgb['blue']*$contrast, 255)));
}
}
imagepng($im, $editDirectory.$imageName, 100);
imagedestroy($im);
break;
case 'sepia':
$in = imagecreatefrompng($editDirectory.$imageName);
$sepia = imagecreatefrompng($editDirectory.$imageName);
$sx = imagesx($in);
$sy = imagesy($in);
$percent = $_GET['percent'];
$filter = imagecreatetruecolor($sx, $sy);
$c = imagecolorallocate($filter, 100, 50, 50);
imagefilledrectangle($filter, 0, 0, $sx, $sy, $c);
imagecopymerge($in, $filter, 0, 0, 0, 0, $sx, $sy, $percent);
imagecopy($sepia, $in, $_GET['x'], $_GET['y'], $_GET['x'], $_GET['y'], $_GET['w'], $_GET['h']);
imagedestroy($in);
imagepng($sepia, $editDirectory.$imageName, 100);
imagedestroy($sepia);
break;
case 'tint':
$r = $_GET['r'];
$g = $_GET['g'];
$b = $_GET['b'];
$in = imagecreatefrompng($editDirectory.$imageName);
$tint = imagecreatefrompng($editDirectory.$imageName);
$x_in = $_GET['x'];
$y_in = $_GET['y'];
$w = $_GET['w'];
$h = $_GET['h'];
for ($x=$x_in; $x<$x_in+$w; $x++) {
for ($y=$y_in; $y<$y_in+$h; $y++) {
$rgb = imagecolorat($in, $x, $y);
$rgb = imagecolorsforindex($in, $rgb);
$c = ($rgb['red'] + $rgb['green'] + $rgb['blue']) / 3;
imagesetpixel($in, $x, $y, imagecolorallocate($in, max($r,$c), max($g,$c), max($b,$c)));
}
}
imagecopy($tint, $in, $_GET['x'], $_GET['y'], $_GET['x'], $_GET['y'], $_GET['w'], $_GET['h']);
imagedestroy($in);
imagepng($tint, $editDirectory.$imageName, 100);
imagedestroy($tint);
break;
}
?>
switch($action){
case 'viewOriginal':
copy($originalDirectory.$imageName, $editDirectory.$imageName);
break;
case 'save':
copy($editDirectory.$imageName, $originalDirectory.$imageName);
break;
case 'resize': // additional required params: w, h
$out_w = $_GET['w'];
$out_h = $_GET['h'];
if (!is_numeric($out_w) || $out_w < 1 || $out_w > 2000 || !is_numeric($out_h) || $out_h < 1 || $out_h > 2000) { exit; }
list($in_w, $in_h) = getimagesize($editDirectory.$imageName);
$in = imagecreatefrompng($editDirectory.$imageName);
$out = imagecreatetruecolor($out_w, $out_h);
imagecopyresampled($out, $in, 0, 0, 0, 0, $out_w, $out_h, $in_w, $in_h);
imagepng($out, $editDirectory.$imageName, 100);
imagedestroy($in);
imagedestroy($out);
break;
case 'rotate': // additional required params: degrees (90, 180 or 270)
$degrees = $_GET['degrees'];
$in = imagecreatefrompng($editDirectory.$imageName);
$out = imagerotate($in, $degrees, 0);
imagepng($out, $editDirectory.$imageName, 100);
imagedestroy($in);
imagedestroy($out);
if (($degrees != 90 && $degrees != 180 && $degrees != 270)) { exit; }
$in = imagecreatefrompng($editDirectory.$imageName);
if ($degrees == 180){
$out = imagerotate($in, $degrees, 180);
}else{ // 90 or 270
$x = imagesx($in);
$y = imagesy ($in);
$max = max($x, $y);
$square = imagecreatetruecolor($max, $max);
imagecopy($square, $in, 0, 0, 0, 0, $x, $y);
$square = imageRotate($square, $degrees, 0);
$out = imagecreatetruecolor($y, $x);
if ($degrees == 90) {
imagecopy($out, $square, 0, 0, 0, $max - $x, $y, $x);
} elseif ($degrees == 270) {
imagecopy($out, $square, 0, 0, $max - $y, 0, $y, $x);
}
imagedestroy($square);
}
imagepng($out, $editDirectory.$imageName, 100);
imagedestroy($in);
imagedestroy($out);
break;
case 'crop': // additional required params: x, y, w, h
$x = $_GET['x'];
$y = $_GET['y'];
$w = $_GET['w'];
$h = $_GET['h'];
if (!is_numeric($x) || !is_numeric($y) || !is_numeric($w) || !is_numeric($h)) { exit; }
$in = imagecreatefrompng($editDirectory.$imageName);
$out = imagecreatetruecolor($w, $h);
imagecopyresampled($out, $in, 0, 0, $x, $y, $w, $h, $w, $h);
imagepng($out, $editDirectory.$imageName, 100);
imagedestroy($in);
imagedestroy($out);
break;
case 'reduceSaturation':
$in = imagecreatefrompng($editDirectory.$imageName);
$x_in = $_GET['x'];
$y_in = $_GET['y'];
$w = $_GET['w'];
$h = $_GET['h'];
for ($x=$x_in; $x<$x_in+$w; $x++) {
for ($y=$y_in; $y<$y_in+$h; $y++) {
$rgb = imagecolorat($in, $x, $y);
$rgb = imagecolorsforindex($in, $rgb);
$grey = ($rgb['red'] + $rgb['green'] + $rgb['blue']) / 3;
imagesetpixel($in, $x, $y, imagecolorallocate($in, $grey, $grey, $grey));
}
}
imagepng($in, $editDirectory.$imageName, 100);
imagedestroy($in);
break;
case 'invert':
$in = imagecreatefrompng($editDirectory.$imageName);
$x_in = $_GET['x'];
$y_in = $_GET['y'];
$w = $_GET['w'];
$h = $_GET['h'];
for ($x=$x_in; $x<$x_in+$w; $x++) {
for ($y=$y_in; $y<$y_in+$h; $y++) {
$rgb = imagecolorat($in, $x, $y);
$rgb = imagecolorsforindex($in, $rgb);
$r = 255 - $rgb['red'];
$g = 255 - $rgb['green'];
$b = 255 - $rgb['blue'];
imagesetpixel($in, $x, $y, imagecolorallocate($in, $r, $g, $b));
}
}
imagepng($in, $editDirectory.$imageName, 100);
imagedestroy($in);
break;
case 'sharpen':
$k = 0.3; // Sharpen (max 1.0)
$in = imagecreatefrompng($editDirectory.$imageName);
$w = imagesx($in);
$h = imagesy ($in);
$x_in = $_GET['x'];
$y_in = $_GET['y'];
$w_in = $_GET['w'];
$h_in = $_GET['h'];
$out = imagecreatetruecolor($w_in-2, $h_in-2);
for($x = $x_in+1; $x < $x_in+$w_in-1; $x++) {
for($y = $y_in+1; $y < $y_in+$h_in-1; $y++) {
$rgb[1][0] = imagecolorat($in, $x, $y-1);
$rgb[0][1] = imagecolorat($in, $x-1, $y);
$rgb[1][1] = imagecolorat($in, $x, $y);
$rgb[2][1] = imagecolorat($in, $x+1, $y);
$rgb[1][2] = imagecolorat($in, $x, $y+1);
$r = -$k *(($rgb[1][0] >> 16) & 0xFF) +
-$k *(($rgb[0][1] >> 16) & 0xFF) +
(1+4*$k) *(($rgb[1][1] >> 16) & 0xFF) +
-$k *(($rgb[2][1] >> 16) & 0xFF) +
-$k *(($rgb[1][2] >> 16) & 0xFF) ;
$g = -$k *(($rgb[1][0] >> 8) & 0xFF) +
-$k *(($rgb[0][1] >> 8) & 0xFF) +
(1+4*$k) *(($rgb[1][1] >> 8) & 0xFF) +
-$k *(($rgb[2][1] >> 8) & 0xFF) +
-$k *(($rgb[1][2] >> 8) & 0xFF) ;
$b = -$k *($rgb[1][0] & 0xFF) +
-$k *($rgb[0][1] & 0xFF) +
(1+4*$k) *($rgb[1][1] & 0xFF) +
-$k *($rgb[2][1] & 0xFF) +
-$k *($rgb[1][2] & 0xFF) ;
$r = min(255, max(0, $r));
$g = min(255, max(0, $g));
$b = min(255, max(0, $b));
if(!isset($cols[$r][$g][$b]))
{
$cols[$r][$g][$b] = imagecolorallocate($out, $r, $g, $b);
}
imagesetpixel($out, $x-$x_in-1, $y-$y_in-1, $cols[$r][$g][$b]);
}
}
$out2 = imagecreatetruecolor($w_in, $h_in);
imagecopyresized($out2, $out, 0, 0, 0, 0, $w_in, $h_in, $w_in-2, $h_in-2);
imagedestroy($out);
imagecopy($in, $out2, $x_in, $y_in, 0, 0, $w_in, $h_in);
imagedestroy($out2);
imagepng($in, $editDirectory.$imageName, 100);
imagedestroy($in);
break;
case 'contrast':
$brightness=0;
$maxb=0;
$minb=255;
$im = imagecreatefrompng($editDirectory.$imageName);
$w = imagesx($im);
$h = imagesy ($im);
for ($x=0; $x<$w; $x++) {
for ($y=0; $y<$h; $y++) {
$rgb=imagecolorat($im, $x, $y);
$rgb=imagecolorsforindex($im, $rgb);
$grey=0.2125*$rgb['red']+0.7154*$rgb['green']+0.0721*$rgb['blue'];
$brightness+=$grey;
if ($grey>$maxb) $maxb=$grey;
if ($grey<$minb) $minb=$grey;
}
}
$brightness=$brightness/($w*$h);
$minb=$brightness/($brightness-$minb);
$maxb=(255-$brightness)/($maxb-$brightness);
$contrast=min($minb, $maxb);
for ($x=0; $x<$w; $x++) {
for ($y=0; $y<$h; $y++) {
$rgb=imagecolorat($im, $x, $y);
$rgb=imagecolorsforindex($im, $rgb);
imagesetpixel($im, $x, $y,
65536*floor(min($rgb['red']*$contrast, 255))+
256*floor(min($rgb['green']*$contrast, 255))+
floor(min($rgb['blue']*$contrast, 255)));
}
}
imagepng($im, $editDirectory.$imageName, 100);
imagedestroy($im);
break;
case 'sepia':
$in = imagecreatefrompng($editDirectory.$imageName);
$sepia = imagecreatefrompng($editDirectory.$imageName);
$sx = imagesx($in);
$sy = imagesy($in);
$percent = $_GET['percent'];
$filter = imagecreatetruecolor($sx, $sy);
$c = imagecolorallocate($filter, 100, 50, 50);
imagefilledrectangle($filter, 0, 0, $sx, $sy, $c);
imagecopymerge($in, $filter, 0, 0, 0, 0, $sx, $sy, $percent);
imagecopy($sepia, $in, $_GET['x'], $_GET['y'], $_GET['x'], $_GET['y'], $_GET['w'], $_GET['h']);
imagedestroy($in);
imagepng($sepia, $editDirectory.$imageName, 100);
imagedestroy($sepia);
break;
case 'tint':
$r = $_GET['r'];
$g = $_GET['g'];
$b = $_GET['b'];
$in = imagecreatefrompng($editDirectory.$imageName);
$tint = imagecreatefrompng($editDirectory.$imageName);
$x_in = $_GET['x'];
$y_in = $_GET['y'];
$w = $_GET['w'];
$h = $_GET['h'];
for ($x=$x_in; $x<$x_in+$w; $x++) {
for ($y=$y_in; $y<$y_in+$h; $y++) {
$rgb = imagecolorat($in, $x, $y);
$rgb = imagecolorsforindex($in, $rgb);
$c = ($rgb['red'] + $rgb['green'] + $rgb['blue']) / 3;
imagesetpixel($in, $x, $y, imagecolorallocate($in, max($r,$c), max($g,$c), max($b,$c)));
}
}
imagecopy($tint, $in, $_GET['x'], $_GET['y'], $_GET['x'], $_GET['y'], $_GET['w'], $_GET['h']);
imagedestroy($in);
imagepng($tint, $editDirectory.$imageName, 100);
imagedestroy($tint);
break;
}
?>
Zou iemand me misschien kunnen helpen hiermee?
Groet, Vlerknozem
Gewijzigd op 01/01/1970 01:00:00 door Koen Bokern
Als je de code eens voor de helft inkort, dus een paar honderd lege regels verwijderd, dan wordt het al een stuk leesbaarder. Hier en daar een lege regel voor de leesbaarheid is een goede gewoonte, maar wanneer 60% van je code uit lege regels bestaat, doe je toch wel iets fout...
En ik zie niet iedereen 488 regels lezen eer dat ze een foutje zien ...
Krijg je errors?
en verder.. wat is de error. of wat werkt er niet?
Ik krijg geen errors door ivm het feit dat ik met Ajax werk. Hoe kan ik de errors in een logfile schrijven?
Door error-reporting aan te zetten moet het wel lukken denk ik.