-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.php
More file actions
81 lines (71 loc) · 3.25 KB
/
Copy pathserver.php
File metadata and controls
81 lines (71 loc) · 3.25 KB
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
<?php
require 'vendor/autoload.php';
function processing(\Intervention\Image\Image $img) {
}
$filename = __DIR__.'/storage/lamantin.png';
$imageManager = new \Intervention\Image\ImageManager();
$img = $imageManager->make($filename);
$getImageProcess = new React\ChildProcess\Process(
'avconv -i rtmp://live.cctv.teorema.info/hvga/benua-lamantin -frames 1 -f image2 -c:v png '.$filename.' 2>&1'
);
$getImageProcess->on('exit', function($exitCode, $termSignal) use($imageManager, &$img, $filename) {
echo "Get Image done \n";
$img->destroy();
/* Delete the circular reference before instantiate a new Image */
unset($img->getDriver()->encoder->image);
$img = $imageManager->make($filename);
});
$appLoop = React\EventLoop\Factory::create();
$appLoop->addPeriodicTimer(5, function () {
$memory = memory_get_usage() / 1024;
$formatted = number_format($memory, 3).'K';
echo "Current memory usage: {$formatted}\n";
});
$appLoop->addPeriodicTimer(5, function ($timer) use ($getImageProcess) {
if (!$getImageProcess->isRunning()) {
echo "Get Image started \n";
$getImageProcess->start($timer->getLoop());
}
});
$socket = new React\Socket\Server($appLoop);
$http = new React\Http\Server($socket, $appLoop);
$router = new FastRoute\RouteCollector(new FastRoute\RouteParser\Std(), new FastRoute\DataGenerator\GroupCountBased());
$router->addRoute('get', '/img', function(\React\Http\Request $request, \React\Http\Response $response)use(&$img){
$response->writeHead(200, ['Content-Type', 'image/png']);
$response->end($img->encode('png'));
});
$router->addRoute('get', '/colors', function(\React\Http\Request $request, \React\Http\Response $response)use(&$img){
$colorMap = [];
for ($i = $img->getHeight(); $i >= 0; $i--) {
for ($j = $img->getWidth(); $j >= 0; $j--) {
$hexcolor = $img->pickColor($j, $i, 'hex');
if (!isset($colorMap[$hexcolor])) {
$colorMap[$hexcolor] = 0;
}
$colorMap[$hexcolor]++;
}
}
ksort($colorMap);
$response->writeHead(200, ['Content-Type', 'text/html']);
//$response->end('<pre>'.print_r([$img->getHeight(), $img->getWidth()], true).'</pre>');
$response->end('<pre>'.print_r($colorMap, true).'</pre>');
});
$dispatcher = new FastRoute\Dispatcher\GroupCountBased($router->getData());
$http->on('request', function (\React\Http\Request $request, \React\Http\Response $response) use (&$dispatcher) {
$routeInfo = $dispatcher->dispatch(strtolower($request->getMethod()), $request->getPath());
switch ($routeInfo[0]) {
case FastRoute\Dispatcher::NOT_FOUND:
$response->writeHead(404, []);
break;
case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
$response->writeHead(405, []);
break;
case FastRoute\Dispatcher::FOUND:
call_user_func_array($routeInfo[1], ['request' => $request, 'response' => $response ]+$routeInfo[2]);
break;
}
$response->end();
});
echo "Server running at http://127.0.0.1:1337\n";
$socket->listen(1337);
$appLoop->run();