-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.php
355 lines (298 loc) · 8.79 KB
/
image.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
//I will totally improve the performance of this, later
$image = new Imagick('image.png');
// If 0 is provided as a width or height parameter,
// aspect ratio is maintained
//$image->thumbnailImage(100, 0);
//calculate size
$sliceWidth = 32;
$imageHeight = $image->getImageHeight();
$numSlices= $image->getImageWidth() / $sliceWidth;
//decide on num of test points
$testPoints = $imageHeight;
//grab points for each slice
$point = $image->getImagePixelColor(1,1);
//print_r($point->getColor());
$pixelArray = array();
for($x=0;$x<$numSlices;$x++){
for($y=0;$y<=$testPoints;$y++){
$left = $image->getImagePixelColor((0+$sliceWidth*$x),(0+(floor($imageHeight/$testPoints))*$y));
$right = $image->getImagePixelColor(($sliceWidth+$sliceWidth*$x-1),(0+(floor($imageHeight/$testPoints))*$y));
$pixelArray[$x]['left'][$y] = array($left->getColor(),'x'=>0+$sliceWidth*$x,'y'=>0+floor($imageHeight/$testPoints)*$y);
$pixelArray[$x]['right'][$y] = array($right->getColor(),'x'=>$sliceWidth+$sliceWidth*$x-1,'y'=>0+floor($imageHeight/$testPoints)*$y);
}
}
//print_r($pixelArray);
//compare points and create matrix
$scores = array();
$c=0;
foreach($pixelArray as $p){
//do something
$r=0;
$g=0;
$b=0;
$r1=0;
$g1=0;
$b1=0;
foreach($p['left'] as $left){
//do
$r=$r+$left[0]['r'];
$g=$g+$left[0]['g'];
$b=$b+$left[0]['b'];
}
foreach($p['right'] as $right){
//do
$r1=$r1+$right[0]['r'];
$g1=$g1+$right[0]['g'];
$b1=$b1+$right[0]['b'];
}
$scores[$c]=array($r=>$r1,$g=>$g1,$b=>$b1);
//scores[slice] = left => right
$c++;
}
//print_r($scores);
/*
DiffMatrix
[right][left]=score
*/
$diffMatrix2 = array();
for($counter1=0;$counter1<$numSlices;$counter1++){
for($counter2=0;$counter2<$numSlices;$counter2++){
if($counter1!=$counter2){
//get each pixel to pixel difference
$totalSliceDiff = 0;
for($x=0;$x<$imageHeight;$x++){
//get right side
$rightPixel = $pixelArray[$counter1]['right'][$x];
//get left side
$leftPixel = $pixelArray[$counter2]['left'][$x];
//if($x==1)
//echo '\n'.$rightPixel[0]['r'] .' ' . $leftPixel[0]['r'];
//compare and add to total
$totalSliceDiff = $totalSliceDiff + abs($leftPixel[0]['r']-$rightPixel[0]['r']);
$totalSliceDiff = $totalSliceDiff + abs($leftPixel[0]['g']-$rightPixel[0]['g']);
$totalSliceDiff = $totalSliceDiff + abs($leftPixel[0]['b']-$rightPixel[0]['b']);
}
$diffMatrix2[$counter1][$counter2]=$totalSliceDiff;
//echo "\nCounter1 = $counter1 and Counter2 = $counter2 and totalDiff = $totalSliceDiff";
}
}
}
$diffMatrix = array();
$count = count($scores);
for($counter1=0;$counter1<$count;$counter1++){
for($counter2=0;$counter2<$count;$counter2++){
//calc differences
if($counter1!=$counter2){
$diffMatrix[$counter1][$counter2]=calcDiff($scores[$counter1],$scores[$counter2]);
}
}
}
//print_r($diffMatrix);
$d2sum=array();
$d2count=0;
foreach($diffMatrix2 as $d2){
$d2sum[$d2count]=0;
$sum=0;
foreach($d2 as $k => $v){
$sum = $sum+ $d2sum[$d2count] + $v;
}
$d2sum[$d2count]=$sum/19;
$d2count++;
}
//print_r($d2sum);
//print_r($diffMatrix2);
$diffMatrix = $diffMatrix2;
//calc lowest diff for each
$lowestDiff = array();
$count = 0;
foreach($diffMatrix as $d){
//find lowest
$lowLoc = getLowest($d,"loc");
$lowVal = getLowest($d);
$lowestDiff[$count]['loc']=$lowLoc;
$lowestDiff[$count]['val']=$lowVal;
$count++;
}
//print_r($lowestDiff);
//test distance of lowest from average
$c=0;
foreach($lowestDiff as $ld){
//subtract average from LD
//echo "<br>\nAverage " . $d2sum[$c] . ' from Lowest' . $ld['val'] . ' = ' . ($d2sum[$c] - $ld['val']);
$c++;
}
//find highest right match (probably right end of image)
$highest=0;
$highestLoc=0;
$count=0;
foreach($lowestDiff as $d){
//find highest
if($highest==0){
$highest=$d['val'];
}
elseif($d['val']>$highest){
$highest=$d['val'];
$highestLoc=$count;
}
$count++;
}
//CHEATING! $highestLoc=9;
//echo "\nHighest: $highest at loc $highestLoc";
//work backwards from highest right match
$buildSumArray = array();
for($seed=0;$seed<$numSlices;$seed++){
$order = array($seed);
$buildSum = 0;
for($x=0;$x<($numSlices-1);$x++){
$left = buildOrder($order[0],$order,$diffMatrix);
/*$left = findLeft($lowestDiff,$order[0]);
echo '\nOrder: '.$order[0];*/
array_unshift($order,$left[0]);
$buildSum = $buildSum + $left[1];
}
$buildSumArray[$seed]=$buildSum;
}
//print_r($buildSumArray);
//print_r($order);
$order=array(findLowestNum($buildSumArray));
//print_r($order);
for($x=0;$x<($numSlices-1);$x++){
$left = buildOrder($order[0],$order,$diffMatrix);
/*$left = findLeft($lowestDiff,$order[0]);
echo '\nOrder: '.$order[0];*/
array_unshift($order,$left[0]);
}
//cut image into slices
$imageSlices = array();
for($x=0;$x<$numSlices;$x++){
$xloc=0+$sliceWidth*$x;
$yloc=0;
$temp = $image->clone();
$imageSlices[$x] = $temp->cropImage($sliceWidth,$imageHeight,$xloc,$yloc);
}
//create new image
$newImage = new Imagick();
$newImage->newImage($sliceWidth * $numSlices, $imageHeight, new ImagickPixel('red'));
$newImage->setImageFormat('png');
//put together most likely pieces and reassemble image
$newIM = new Imagick();
foreach($order as $o){
//echo '\nO = '.$o;
$xloc=0+$sliceWidth*$o;
$yloc=0;
$temp = new Imagick();
$temp = $image->clone();
$temp->cropImage($sliceWidth,$imageHeight,$xloc,$yloc);
$newIM->newImage($sliceWidth,$imageHeight, "red");
$newIM->setImage($temp);
//$newImage->compositeImage($imageSlices[$o],Imagick::COMPOSITE_DEFAULT,$xloc,$yloc);
}
$newIM->resetIterator();
$combined = new Imagick();
$combined = $newIM->appendImages(false);
$combined->newImage($sliceWidth*$numSlices,$imageHeight,"blue");
$combined->setImage($image);
$combined->resetIterator();
$combined2=$combined->appendImages(true);
/* Output the image */
$combined2->setImageFormat("png");
header('Content-type: image/png');
echo $combined2;
function findLowestNum($arr){
//find lowest number in array and returns location
$count=0;
$lowest = 0;
$lowestLoc=0;
foreach($arr as $a){
if($count==0){
$lowest=$a;
//echo "\n<br>Init";
}
if($a<$lowest){
//echo "\n<br>New Lowest: $a is lower than $lowest";
$lowest=$a;
$lowestLoc=$count;
}
$count++;
}
return $lowestLoc;
}
function buildOrder($rightMatch,$alreadyFound,$searchArray){
//find left match of $rightMatch, excluding locations in already found, in $searchArray (diffMatrix)
$bestLoc=0;
$bestVal=9999999999999;
foreach($searchArray as $k =>$v){
//make sure not found
$checkExists = array_search($k,$alreadyFound);
if($checkExists===0||$checkExists>0){
//do nothing, already exists
}else{
//do search
foreach($v as $k2 => $v2){
//search for match
if($k2==$rightMatch&&$v2<$bestVal){
//update best find
$bestLoc=$k;
$bestVal=$v2;
}
}
}
}
return array($bestLoc,$bestVal);
}
function findLeft($arr,$loc){
//find best left match
$bestLoc=-1;
$bestVal=999999999999;
foreach($arr as $k => $a){
if($a['loc']==$loc){
//echo "\nMATCH " . $a['val'] . ' ' . $a['loc'];
if($a['val']<$bestVal){
//echo "\nNEW BEST $bestVal > " . $a['val'];
$bestLoc = $k;
$bestVal=$a['val'];
}
}
}
return $bestLoc;
}
function getLowest($arr,$type = "val"){
//print_r($arr);
$lowest=9999999999;
$lowestLoc=0;
$x=0;
foreach($arr as $k => $v){
//
if($v<$lowest){
$lowest=$v;
$lowestLoc=$x;
}
$x++;
}
if($type=="loc"){
return $lowestLoc;
}
else{
return $lowest;
}
}
function calcDiff($arr1,$arr2){
$rgb1 = array();
$rbg2 = array();
//calc diff between arr1 (right) and arr2 (left)
foreach($arr1 as $a1){
//get right side
$rgb1[]=$a1;
}
foreach($arr2 as $a2 => $val){
//get left side
$rgb2[]=$a2;
}
$diff = 0;
$diff = $diff+ abs($rgb1[0]-$rgb2[0]);
$diff = $diff+ abs($rgb1[1]-$rgb2[1]);
$diff = $diff+ abs($rgb1[2]-$rgb2[2]);
return $diff;
}
?>