-
Notifications
You must be signed in to change notification settings - Fork 70
/
PhotoboothCapture.php
183 lines (169 loc) · 5.71 KB
/
PhotoboothCapture.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
<?php
namespace Photobooth;
use Photobooth\Logger\NamedLogger;
use Photobooth\Service\LoggerService;
/**
* Class PhotoboothCapture
*/
class PhotoboothCapture
{
public string $style;
public string $fileName;
public string $tmpFile;
public string $collageSubFile;
public int $collageNumber;
public int $collageLimit;
public string $demoFolder = __DIR__ . '/../resources/img/demo/';
public string $flipImage = 'off';
public string $captureCmd;
public NamedLogger $logger;
public int $debugLevel = 1;
/**
* PhotoboothCapture constructor.
*/
public function __construct()
{
$this->logger = LoggerService::getInstance()->getLogger('main');
$this->logger->debug(self::class);
}
/**
* Capture a demo image.
*/
public function captureDemo(): void
{
$this->logger->debug('Capture Demo', [
'demoFolder' => $this->demoFolder
]);
$demoFolder = $this->demoFolder;
$scannedFiles = scandir($demoFolder);
if ($scannedFiles !== false) {
$devImg = array_diff($scannedFiles, ['.', '..']);
copy($demoFolder . $devImg[array_rand($devImg)], $this->tmpFile);
} else {
$this->logger->error('Failed to scan demo folder for images!');
echo json_encode(['error' => 'Failed to scan demo folder for images!']);
die();
}
}
/**
* Capture an image from canvas data.
* @param string $data
*/
public function captureCanvas($data): void
{
$this->logger->debug('Capture Canvas');
try {
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
file_put_contents($this->tmpFile, $data);
if ($this->flipImage != 'off') {
$imageHandler = new Image();
$im = $imageHandler->createFromImage($this->tmpFile);
if (!$im instanceof \GdImage) {
throw new \Exception('Failed to create image resource from tmp image.');
}
$imageHandler->debugLevel = $this->debugLevel;
$imageHandler->jpegQuality = 100;
switch ($this->flipImage) {
case 'flip-horizontal':
imageflip($im, IMG_FLIP_HORIZONTAL);
break;
case 'flip-vertical':
imageflip($im, IMG_FLIP_VERTICAL);
break;
case 'flip-both':
imageflip($im, IMG_FLIP_BOTH);
break;
default:
break;
}
$imageHandler->saveJpeg($im, $this->tmpFile);
unset($im);
}
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
echo json_encode(['error' => $e->getMessage()]);
die();
}
}
/**
* Capture an image or video using a command.
*/
public function captureWithCmd(): void
{
$this->logger->debug('Capture with CMD', [
'cmd' => $this->captureCmd,
'tmpFile' => $this->tmpFile,
]);
//gphoto must be executed in a dir with write permission for other commands we stay in the api dir
if (substr($this->captureCmd, 0, strlen('gphoto')) === 'gphoto') {
chdir(dirname($this->tmpFile));
}
$cmd = sprintf($this->captureCmd, $this->tmpFile);
$cmd .= ' 2>&1'; //Redirect stderr to stdout, otherwise error messages get lost.
exec($cmd, $output, $returnValue);
if ($returnValue && ($this->debugLevel > 1 || $this->style === 'video')) {
$data = [
'error' => 'Capture command returned an error code.',
'cmd' => $cmd,
'returnValue' => $returnValue,
'output' => $output,
];
$this->logger->error('error', $data);
if ($this->style === 'video') {
echo json_encode($data);
die();
}
}
if ($this->style === 'video') {
$i = 0;
$processingTime = 300;
while ($i < $processingTime) {
if (file_exists($this->tmpFile)) {
break;
} else {
$i++;
usleep(100000);
}
}
}
if (!file_exists($this->tmpFile)) {
$data = [
'error' => 'File was not created',
'cmd' => $cmd,
'returnValue' => $returnValue,
'output' => $output,
];
if ($this->style === 'video') {
// remove all files that were created - all filenames start with the videos name
exec('rm -f ' . $this->tmpFile . '*');
}
$this->logger->error('error', $data);
echo json_encode($data);
die();
}
}
/**
* Return information about the successful capture process
*/
public function returnData(): array
{
if ($this->style === 'collage') {
$data = [
'success' => 'collage',
'file' => $this->fileName,
'collage_file' => $this->collageSubFile,
'current' => $this->collageNumber,
'limit' => $this->collageLimit,
];
} else {
$data = [
'success' => $this->style,
'file' => $this->fileName
];
}
$this->logger->debug('returnData', $data);
return $data;
}
}