1 <?php
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 class NetworkGraph
47 {
48 49 50
51
52 53 54
55 protected $intNumberInputs;
56
57 58 59
60 protected $intNumberHiddenLayers;
61
62 63 64
65 protected $intNumberNeuronsOfHiddenLayer;
66
67 68 69
70 protected $intNumberOfOutputs;
71
72 73 74
75 protected $handleImage;
76
77 78 79
80 protected $handleColorNeuronInput;
81
82 83 84
85 protected $handleColorNeuronHidden;
86
87 88 89
90 protected $handleColorNeuronOutput;
91
92 93 94
95 protected $handleColorNeuronBorder;
96
97 98 99
100 protected $handleColorBackground;
101
102 103 104
105 protected $handleColorConnection;
106
107 108 109
110 protected $intMaxNeuronsPerLayer;
111
112 113 114
115 protected $intLayerDistance = 250;
116
117 118 119
120 protected $intNeuronDistance = 50;
121
122
123
124 125 126 127 128 129 130 131 132
133 public function __construct($intNumberInputs, $intNumberHiddenLayers, $intNumberNeuronsOfHiddenLayer, $intNumberOfOutputs)
134 {
135 $this->intNumberInputs = $intNumberInputs;
136
137 $this->intNumberHiddenLayers = $intNumberHiddenLayers;
138
139 $this->intNumberNeuronsOfHiddenLayer = $intNumberNeuronsOfHiddenLayer;
140
141 $this->intNumberOfOutputs = $intNumberOfOutputs;
142
143 $this->intMaxNeuronsPerLayer = $intNumberInputs;
144
145 $this->createImage();
146
147 $this->drawNetwork();
148 }
149
150 151 152 153 154 155
156 protected function drawNetwork()
157 {
158 $this->drawConnections();
159
160 $this->drawInputNeurons();
161
162 $this->drawHiddenNeurons();
163
164 $this->drawOutputNeurons();
165 }
166
167 168 169 170 171
172 protected function drawConnections()
173 {
174 $this->drawConnectionsInputHidden();
175
176 $this->drawConnectionsHiddens();
177
178 $this->drawConnectionsHiddenOutput();
179 }
180
181 182 183
184 protected function drawConnectionsInputHidden()
185 {
186 $intYPosHiddenStart = $this->calculateYPosStart($this->intNumberNeuronsOfHiddenLayer);
187
188 $intYPosInputStart = $this->calculateYPosStart($this->intNumberInputs);
189
190 for($intIndexInput = 0; $intIndexInput < $this->intNumberInputs; $intIndexInput++)
191 for($intIndexHidden = 0; $intIndexHidden < $this->intNumberNeuronsOfHiddenLayer; $intIndexHidden++)
192 {
193 $intXPosInput = 100;
194
195 $intYPosInput = $intYPosInputStart + $this->intNeuronDistance * $intIndexInput;
196
197 $intXPosHidden = 100 + $this->intLayerDistance;
198
199 $intYPosHidden = $intYPosHiddenStart + $this->intNeuronDistance * $intIndexHidden;
200
201 imageline($this->handleImage, $intXPosInput, $intYPosInput, $intXPosHidden, $intYPosHidden, $this->handleColorConnection);
202 }
203 }
204
205 206 207
208 protected function drawConnectionsHiddenOutput()
209 {
210 for($intIndexLayer = 0; $intIndexLayer < $this->intNumberHiddenLayers; $intIndexLayer++)
211 $intXPosHidden = 100 + $this->intLayerDistance + $this->intLayerDistance * $intIndexLayer;
212
213 $intYPosHiddenStart = $this->calculateYPosStart($this->intNumberNeuronsOfHiddenLayer);
214
215 $intYPosOutputStart = $this->calculateYPosStart($this->intNumberOfOutputs);
216
217 for($intIndexOutput = 0; $intIndexOutput < $this->intNumberOfOutputs; $intIndexOutput++)
218 for($intIndexHidden = 0; $intIndexHidden < $this->intNumberNeuronsOfHiddenLayer; $intIndexHidden++)
219 {
220 $intXPosHidden = $intXPosHidden;
221
222 $intYPosHidden = $intYPosHiddenStart + $this->intNeuronDistance * $intIndexHidden;
223
224 $intXPosOutput = $intXPosHidden + $this->intLayerDistance;
225
226 $intYPosOutput = $intYPosOutputStart + $this->intNeuronDistance * $intIndexOutput;
227
228 imageline($this->handleImage, $intXPosHidden, $intYPosHidden, $intXPosOutput, $intYPosOutput, $this->handleColorConnection);
229 }
230 }
231
232 233 234
235 protected function drawConnectionsHiddens()
236 {
237 if($this->intNumberHiddenLayers <= 1)
238 return;
239
240 $intYPosHiddenStart = $this->calculateYPosStart($this->intNumberNeuronsOfHiddenLayer);
241
242 for($intIndexLayer = 1; $intIndexLayer < $this->intNumberHiddenLayers; $intIndexLayer++)
243 for($intIndexHidden1 = 0; $intIndexHidden1 < $this->intNumberNeuronsOfHiddenLayer; $intIndexHidden1++)
244 for($intIndexHidden2 = 0; $intIndexHidden2 < $this->intNumberNeuronsOfHiddenLayer; $intIndexHidden2++)
245 {
246 $intXPosHidden1 = 100 + $this->intLayerDistance + $this->intLayerDistance * $intIndexLayer - $this->intLayerDistance;
247
248 $intYPosHidden1 = $intYPosHiddenStart + $this->intNeuronDistance * $intIndexHidden1;
249
250 $intXPosHidden2 = 100 + $this->intLayerDistance + $this->intLayerDistance * $intIndexLayer;
251
252 $intYPosHidden2 = $intYPosHiddenStart + $this->intNeuronDistance * $intIndexHidden2;
253
254 imageline($this->handleImage, $intXPosHidden1, $intYPosHidden1, $intXPosHidden2, $intYPosHidden2, $this->handleColorConnection);
255 }
256 }
257
258 259 260
261 protected function drawInputNeurons()
262 {
263 $intYPosInputStart = $this->calculateYPosStart($this->intNumberInputs);
264
265 for($intIndex = 0; $intIndex < $this->intNumberInputs; $intIndex++)
266 {
267 imagefilledellipse($this->handleImage, 100, $intYPosInputStart + $this->intNeuronDistance * $intIndex, 30, 30, $this->handleColorNeuronInput);
268
269 imageellipse($this->handleImage, 100, $intYPosInputStart + $this->intNeuronDistance * $intIndex, 30, 30, $this->handleColorNeuronBorder);
270 }
271 }
272
273 274 275
276 protected function drawHiddenNeurons()
277 {
278 $intYPosHiddenStart = $this->calculateYPosStart($this->intNumberNeuronsOfHiddenLayer);
279
280 for($intIndexLayer = 0; $intIndexLayer < $this->intNumberHiddenLayers; $intIndexLayer++)
281 for($intIndexNeuron = 0; $intIndexNeuron < $this->intNumberNeuronsOfHiddenLayer; $intIndexNeuron++)
282 {
283 imagefilledellipse($this->handleImage, 100 + $this->intLayerDistance + $this->intLayerDistance * $intIndexLayer, $intYPosHiddenStart + $this->intNeuronDistance * $intIndexNeuron, 30, 30, $this->handleColorNeuronHidden);
284
285 imageellipse($this->handleImage, 100 + $this->intLayerDistance + $this->intLayerDistance * $intIndexLayer, $intYPosHiddenStart + $this->intNeuronDistance * $intIndexNeuron, 30, 30, $this->handleColorNeuronBorder);
286 }
287 }
288
289 290 291
292 protected function drawOutputNeurons()
293 {
294 for($intIndexLayer = 0; $intIndexLayer < $this->intNumberHiddenLayers; $intIndexLayer++)
295 $xpos = 100 + $this->intLayerDistance + $this->intLayerDistance * $intIndexLayer;
296
297 $yposStart = $this->calculateYPosStart($this->intNumberOfOutputs);
298
299 for($intIndexNeuron = 0; $intIndexNeuron < $this->intNumberOfOutputs; $intIndexNeuron++)
300 {
301 imagefilledellipse($this->handleImage, $xpos + $this->intLayerDistance, $yposStart + $this->intNeuronDistance * $intIndexNeuron, 30, 30, $this->handleColorNeuronOutput);
302
303 imageellipse($this->handleImage, $xpos + $this->intLayerDistance, $yposStart + $this->intNeuronDistance * $intIndexNeuron, 30, 30, $this->handleColorNeuronBorder);
304 }
305 }
306
307 308 309 310 311 312
313 protected function createImage()
314 {
315 $this->handleImage = imagecreatetruecolor($this->calculateImageWidth(), $this->calculateImageHeight());
316
317 $this->setColors();
318
319 $this->setBackground();
320 }
321
322 protected function setColors()
323 {
324 $this->handleColorBackground = imagecolorallocate($this->handleImage, 200, 200, 200);
325
326 $this->handleColorNeuronInput = imagecolorallocate($this->handleImage, 0, 255, 0);
327
328 $this->handleColorNeuronHidden = imagecolorallocate($this->handleImage, 255, 0, 0);
329
330 $this->handleColorNeuronOutput = imagecolorallocate($this->handleImage, 0, 0, 255);
331
332 $this->handleColorConnection = imagecolorallocate($this->handleImage, 155, 255, 155);
333
334 $this->handleColorNeuronBorder = imagecolorallocate($this->handleImage, 0, 0, 0);
335 }
336
337 protected function setBackground()
338 {
339 imagefill($this->handleImage, 0, 0, $this->handleColorBackground);
340 }
341
342 343 344 345 346
347 public function getImage()
348 {
349 ob_start();
350
351 imagepng($this->handleImage);
352
353 $binReturn = ob_get_contents();
354
355 ob_end_clean();
356
357 return $binReturn;
358 }
359
360 361 362 363 364
365 public function printImage()
366 {
367 header('Content-type: image/png');
368
369 print $this->getImage();
370 }
371
372 373 374 375
376 protected function calculateYPosStart($intNumberNeurons)
377 {
378 $v1 = $this->intMaxNeuronsPerLayer * $this->intNeuronDistance / 2;
379
380 $v2 = $intNumberNeurons * $this->intNeuronDistance / 2;
381
382 return $v1 - $v2 + $this->intNeuronDistance;
383 }
384
385 386 387
388 protected function calculateImageHeight()
389 {
390 return (int)($this->intMaxNeuronsPerLayer * $this->intNeuronDistance + $this->intNeuronDistance);
391 }
392
393 394 395
396 protected function calculateImageWidth()
397 {
398 return (int)(($this->intNumberHiddenLayers + 2) * $this->intLayerDistance);
399 }
400
401 402 403 404 405 406
407 public function saveToFile($strFilename)
408 {
409 file_put_contents($strFilename, $this->getImage());
410 }
411 }
412