1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2022-2023, Matias De lellis |
4
|
|
|
* |
5
|
|
|
* @author Matias De lellis <[email protected]> |
6
|
|
|
* |
7
|
|
|
* @license AGPL-3.0-or-later |
8
|
|
|
* |
9
|
|
|
* This code is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
11
|
|
|
* as published by the Free Software Foundation. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Affero General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
19
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace OCA\FaceRecognition\Helper; |
24
|
|
|
|
25
|
|
|
use OCP\Files\File; |
26
|
|
|
use OCP\Http\Client\IClientService; |
27
|
|
|
use OCP\IConfig; |
28
|
|
|
use OCP\IImage; |
29
|
|
|
|
30
|
|
|
use OC\StreamImage; |
|
|
|
|
31
|
|
|
|
32
|
|
|
class Imaginary { |
33
|
|
|
|
34
|
|
|
/** @var IConfig */ |
35
|
|
|
private $config; |
36
|
|
|
|
37
|
|
|
/** @var IClientService */ |
38
|
|
|
private $service; |
39
|
|
|
|
40
|
5 |
|
public function __construct() { |
41
|
5 |
|
$this->config = \OC::$server->get(IConfig::class); |
42
|
5 |
|
$this->service = \OC::$server->get(IClientService::class); |
43
|
|
|
} |
44
|
|
|
|
45
|
5 |
|
public function isEnabled(): bool { |
46
|
5 |
|
$imaginaryUrl = $this->config->getSystemValueString('preview_imaginary_url', 'invalid'); |
47
|
5 |
|
return ($imaginaryUrl !== 'invalid'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getUrl(): ?string { |
51
|
|
|
$imaginaryUrl = $this->config->getSystemValueString('preview_imaginary_url', 'invalid'); |
52
|
|
|
if ($imaginaryUrl === 'invalid') |
53
|
|
|
return null; |
54
|
|
|
|
55
|
|
|
return rtrim($imaginaryUrl, '/'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string imaginary version |
60
|
|
|
*/ |
61
|
|
|
public function getVersion(): ?string { |
62
|
|
|
$imaginaryUrl = $this->getUrl(); |
63
|
|
|
if (!$imaginaryUrl) { |
64
|
|
|
throw new \RuntimeException('Try to use imaginary without valid url'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$httpClient = $this->service->newClient(); |
68
|
|
|
|
69
|
|
|
try { |
70
|
|
|
$response = $httpClient->get($imaginaryUrl . '/'); |
71
|
|
|
} catch (\Exception $e) { |
72
|
|
|
return null; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ($response->getStatusCode() !== 200) { |
76
|
|
|
return null; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$info = json_decode($response->getBody(), true); |
|
|
|
|
80
|
|
|
|
81
|
|
|
return $info['imaginary']; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return array Returns the array with the size of image. |
86
|
|
|
*/ |
87
|
|
|
public function getInfo(string $filepath): array { |
88
|
|
|
$imaginaryUrl = $this->getUrl(); |
89
|
|
|
if (!$imaginaryUrl) { |
90
|
|
|
throw new \RuntimeException('Try to use imaginary without valid url'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$httpClient = $this->service->newClient(); |
94
|
|
|
|
95
|
|
|
$options = []; |
96
|
|
|
$options['multipart'] = [[ |
97
|
|
|
'name' => 'file', |
98
|
|
|
'contents' => file_get_contents($filepath), |
99
|
|
|
'filename' => basename($filepath), |
100
|
|
|
]]; |
101
|
|
|
|
102
|
|
|
$response = $httpClient->post($imaginaryUrl . '/info', $options); |
103
|
|
|
|
104
|
|
|
if ($response->getStatusCode() !== 200) { |
105
|
|
|
throw new \RuntimeException('Error getting image information in Imaginary: ' . json_decode($response->getBody())['message']); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$info = json_decode($response->getBody(), true); |
109
|
|
|
|
110
|
|
|
$type = $info['type']; |
111
|
|
|
//NOTE: Imaginary has problems rorating heic images. Issue #662 |
112
|
|
|
$autorotate = ($info['orientation'] > 4 && $type != 'heif'); |
113
|
|
|
|
114
|
|
|
return [ |
115
|
|
|
'type' => $type, |
116
|
|
|
'autorotate' => $autorotate, |
117
|
|
|
// Rotates the size, since it is important and Imaginary do not do that. |
118
|
|
|
'width' => $autorotate ? $info['height'] : $info['width'], |
119
|
|
|
'height' => $autorotate ? $info['width'] : $info['height'] |
120
|
|
|
]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return string|resource Returns the resized image |
125
|
|
|
*/ |
126
|
|
|
public function getResized(string $filepath, int $width, int $height, bool $autorotate, string $mimeType) { |
127
|
|
|
|
128
|
|
|
$imaginaryUrl = $this->getUrl(); |
129
|
|
|
if (!$imaginaryUrl) { |
130
|
|
|
throw new \RuntimeException('Try to use imaginary without valid url'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$httpClient = $this->service->newClient(); |
134
|
|
|
|
135
|
|
|
switch ($mimeType) { |
136
|
|
|
case 'image/png': |
137
|
|
|
$type = 'png'; |
138
|
|
|
break; |
139
|
|
|
default: |
140
|
|
|
$type = 'jpeg'; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$operations = []; |
144
|
|
|
|
145
|
|
|
if ($autorotate) { |
146
|
|
|
$operations[] = [ |
147
|
|
|
'operation' => 'autorotate', |
148
|
|
|
]; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$operations[] = [ |
152
|
|
|
'operation' => 'resize', |
153
|
|
|
'params' => [ |
154
|
|
|
'width' => $width, |
155
|
|
|
'height' => $height, |
156
|
|
|
'stripmeta' => 'true', |
157
|
|
|
'type' => $type, |
158
|
|
|
'norotation' => 'true', |
159
|
|
|
'force' => 'true' |
160
|
|
|
] |
161
|
|
|
]; |
162
|
|
|
|
163
|
|
|
$response = $httpClient->post( |
164
|
|
|
$imaginaryUrl . '/pipeline', [ |
165
|
|
|
'query' => ['operations' => json_encode($operations)], |
166
|
|
|
'body' => file_get_contents($filepath), |
167
|
|
|
'nextcloud' => ['allow_local_address' => true], |
168
|
|
|
]); |
169
|
|
|
|
170
|
|
|
if ($response->getStatusCode() !== 200) { |
171
|
|
|
throw new \RuntimeException('Error generating temporary image in Imaginary: ' . json_decode($response->getBody())['message']); |
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $response->getBody(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
} |
178
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths