|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PatchFind\Graph; |
|
4
|
|
|
|
|
5
|
|
|
class Generate implements GenerateInterface{ |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* width of graph |
|
9
|
|
|
* @var int |
|
10
|
|
|
*/ |
|
11
|
|
|
private $x_w; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* height of graph |
|
15
|
|
|
* @var int |
|
16
|
|
|
*/ |
|
17
|
|
|
private $y_h; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* create final graph |
|
21
|
|
|
* |
|
22
|
|
|
* @param int $width |
|
23
|
|
|
* @param int $height |
|
24
|
|
|
* @return array |
|
25
|
|
|
*/ |
|
26
|
|
|
public function createGraph($width, $height){ |
|
27
|
|
|
$this->x_w = $width; |
|
28
|
|
|
$this->y_h = $height; |
|
29
|
|
|
|
|
30
|
|
|
$modX = ceil($width/2); |
|
31
|
|
|
$modY = ceil($height/2); |
|
32
|
|
|
|
|
33
|
|
|
$relations = array(); |
|
34
|
|
|
for($y = 0; $y <= $height; $y++){ |
|
35
|
|
|
for($x = 0; $x <= $width; $x++){ |
|
36
|
|
|
$k = $x; $k2 = $y; |
|
37
|
|
|
if($x<$modX) $k = $width-$x; |
|
38
|
|
|
if($y<$modY) $k2 = $height-$y; |
|
39
|
|
|
$k3 = $k; |
|
40
|
|
|
if($k2>$k) $k3 = $k2; |
|
41
|
|
|
|
|
42
|
|
|
$tmpX = $x-1; $tmpY = $y; |
|
43
|
|
|
if($this->checkParams($tmpX, $tmpY)) $relations["($tmpX,$tmpY)"]["($x,$y)"] = $k3; |
|
44
|
|
|
|
|
45
|
|
|
$tmpX2 = $x+1; $tmpY2 = $y; |
|
46
|
|
|
if($this->checkParams($tmpX2, $tmpY2)) $relations["($tmpX2,$tmpY2)"]["($x,$y)"] = $k3; |
|
47
|
|
|
|
|
48
|
|
|
$tmpX3 = $x; $tmpY3 = $y-1; |
|
49
|
|
|
if($this->checkParams($tmpX3, $tmpY3)) $relations["($tmpX3,$tmpY3)"]["($x,$y)"] = $k3; |
|
50
|
|
|
|
|
51
|
|
|
$tmpX4 = $x; $tmpY4 = $y+1; |
|
52
|
|
|
if($this->checkParams($tmpX4, $tmpY4)) $relations["($tmpX4,$tmpY4)"]["($x,$y)"] = $k3; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $relations; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* load graph from .jpg file |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $file |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
|
|
public function loadFromFile($file){ |
|
66
|
|
|
|
|
67
|
|
|
$res = @imagecreatefromjpeg($file); |
|
68
|
|
|
$relations = array(); |
|
69
|
|
|
|
|
70
|
|
|
if(!$res){ |
|
|
|
|
|
|
71
|
|
|
$width = imagesx($res); |
|
|
|
|
|
|
72
|
|
|
$height = imagesy($res); |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
$new_tab = array(); |
|
75
|
|
|
for($y = 0; $y <= $height; $y++){ |
|
76
|
|
|
for($x = 0; $x <= $width; $x++){ |
|
77
|
|
|
if($y==$height || $x==$width){ |
|
78
|
|
|
$color = 10; |
|
79
|
|
|
}else{ |
|
80
|
|
|
$color = imagecolorat($res, $x, $y); |
|
|
|
|
|
|
81
|
|
|
if(!$color) $color = 255; |
|
82
|
|
|
} |
|
83
|
|
|
$new_tab[$x][$y] = $color; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$this->x_w = $width; |
|
88
|
|
|
$this->y_h = $height; |
|
89
|
|
|
|
|
90
|
|
|
$modX = ceil($width/2); |
|
91
|
|
|
$modY = ceil($height/2); |
|
92
|
|
|
|
|
93
|
|
|
for($y = 0; $y <= $height; $y++){ |
|
94
|
|
|
for($x = 0; $x <= $width; $x++){ |
|
95
|
|
|
|
|
96
|
|
|
if($new_tab[$x][$y]<=100){ |
|
97
|
|
|
$k3 = 9999; |
|
98
|
|
|
}else{ |
|
99
|
|
|
$k = $x; $k2 = $y; |
|
100
|
|
|
if($x<$modX) $k = $width-$x; |
|
101
|
|
|
if($y<$modY) $k2 = $height-$y; |
|
102
|
|
|
$k3 = $k; |
|
103
|
|
|
if($k2>$k) $k3 = $k2; |
|
104
|
|
|
} |
|
105
|
|
|
$tmpX = $x-1; $tmpY = $y; |
|
106
|
|
|
if($this->checkParams($tmpX, $tmpY)) $relations["($tmpX,$tmpY)"]["($x,$y)"] = $k3; |
|
107
|
|
|
|
|
108
|
|
|
$tmpX2 = $x+1; $tmpY2 = $y; |
|
109
|
|
|
if($this->checkParams($tmpX2, $tmpY2)) $relations["($tmpX2,$tmpY2)"]["($x,$y)"] = $k3; |
|
110
|
|
|
|
|
111
|
|
|
$tmpX3 = $x; $tmpY3 = $y-1; |
|
112
|
|
|
if($this->checkParams($tmpX3, $tmpY3)) $relations["($tmpX3,$tmpY3)"]["($x,$y)"] = $k3; |
|
113
|
|
|
|
|
114
|
|
|
$tmpX4 = $x; $tmpY4 = $y+1; |
|
115
|
|
|
if($this->checkParams($tmpX4, $tmpY4)) $relations["($tmpX4,$tmpY4)"]["($x,$y)"] = $k3; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $relations; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* check input params |
|
125
|
|
|
* |
|
126
|
|
|
* @param int $width |
|
127
|
|
|
* @param int $height |
|
128
|
|
|
* @param int $tmpX |
|
129
|
|
|
* @param int $tmpY |
|
130
|
|
|
* @return bool |
|
131
|
|
|
*/ |
|
132
|
|
|
private function checkParams($tmpX, $tmpY){ |
|
133
|
|
|
if($tmpX >= 0 && $tmpY >= 0 && $tmpX <= $this->x_w && $tmpY <= $this->y_h){ |
|
134
|
|
|
$output = true; |
|
135
|
|
|
}else{ |
|
136
|
|
|
$output = false; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return $output; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
|