/
Image.php
361 lines (319 loc) · 13.9 KB
/
Image.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
/*
* Copyright (C) 2024 Xibo Signage Ltd
*
* Xibo - Digital Signage - https://xibosignage.com
*
* This file is part of Xibo.
*
* Xibo is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* Xibo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Xibo. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Xibo\Widget;
use Intervention\Image\Exception\NotReadableException;
use Intervention\Image\ImageManagerStatic as Img;
use Respect\Validation\Validator as v;
use Slim\Http\Response as Response;
use Slim\Http\ServerRequest as Request;
use Xibo\Support\Exception\InvalidArgumentException;
/**
* Class Image
* @package Xibo\Widget
*/
class Image extends ModuleWidget
{
/** @inheritdoc */
public function settingsForm()
{
return 'image-form-settings';
}
/** @inheritdoc */
public function settings(Request $request, Response $response): Response
{
$this->module->settings['defaultScaleTypeId'] = $this->getSanitizer($request->getParams())->getString('defaultScaleTypeId');
return parent::settings($request, $response);
}
/** @inheritdoc */
public function setDefaultWidgetOptions()
{
parent::setDefaultWidgetOptions();
$this->setOption('scaleType', $this->getSetting('defaultScaleTypeId', 'center'));
}
/**
* @inheritDoc
*/
public function layoutDesignerJavaScript()
{
// We use the same javascript as the data set view designer
return 'image-designer-javascript';
}
/**
* Edit an Image Widget
* @SWG\Put(
* path="/playlist/widget/{widgetId}?image",
* operationId="WidgetImageEdit",
* tags={"widget"},
* summary="Parameters for editing existing image on a layout",
* description="For uploading new image files, please refer to POST /library documentation.
* For assigning existing image file to a Playlist please see POST /playlist/library/assign/{playlistId} documentation.
* This call will replace existing Widget object, all not supplied parameters will be set to default.",
* @SWG\Parameter(
* name="widgetId",
* in="path",
* description="The WidgetId ID",
* type="integer",
* required=true
* ),
* @SWG\Parameter(
* name="name",
* in="formData",
* description="Optional Widget Name",
* type="string",
* required=false
* ),
* @SWG\Parameter(
* name="duration",
* in="formData",
* description="The Widget Duration",
* type="integer",
* required=false
* ),
* @SWG\Parameter(
* name="useDuration",
* in="formData",
* description="Select 1 only if you will provide duration parameter as well",
* type="integer",
* required=false
* ),
* @SWG\Parameter(
* name="scaleTypeId",
* in="formData",
* description="Select scale type available options: center, stretch",
* type="string",
* required=false
* ),
* @SWG\Parameter(
* name="alignId",
* in="formData",
* description="Horizontal alignment - left, center, bottom",
* type="string",
* required=false
* ),
* @SWG\Parameter(
* name="valignId",
* in="formData",
* description="Vertical alignment - top, middle, bottom",
* type="string",
* required=false
* ),
* @SWG\Parameter(
* name="enableStat",
* in="formData",
* description="The option (On, Off, Inherit) to enable the collection of Widget Proof of Play statistics",
* type="string",
* required=false
* ),
* @SWG\Response(
* response=201,
* description="successful operation",
* @SWG\Schema(ref="#/definitions/Widget"),
* @SWG\Header(
* header="Location",
* description="Location of the new widget",
* type="string"
* )
* )
* )
*
* @inheritDoc
*/
public function edit(Request $request, Response $response): Response
{
$sanitizedParams = $this->getSanitizer($request->getParams());
// Set the properties specific to Images
$this->setDuration($sanitizedParams->getInt('duration', ['default' => $this->getDuration()]));
$this->setUseDuration($sanitizedParams->getCheckbox('useDuration'));
$this->setOption('name', $sanitizedParams->getString('name'));
$this->setOption('scaleType', $sanitizedParams->getString('scaleTypeId', ['default' => 'center']));
$this->setOption('align', $sanitizedParams->getString('alignId', ['default' => 'center']));
$this->setOption('valign', $sanitizedParams->getString('valignId', ['default' => 'middle']));
$this->setOption('enableStat', $sanitizedParams->getString('enableStat'));
$this->isValid();
$this->saveWidget();
return $response;
}
/** @inheritdoc */
public function preview($width, $height, $scaleOverride = 0)
{
if ($this->module->previewEnabled == 0) {
return parent::preview($width, $height, $scaleOverride);
}
$proportional = ($this->getOption('scaleType') !== 'stretch') ? 1 : 0;
$fit = $this->getOption('scaleType') === 'fit' ? 1 : 0;
$align = $this->getOption('align', 'center');
$vAlign = $this->getOption('valign', 'middle');
$url = $this->urlFor('library.download', [
'regionId' => $this->region->regionId,
'id' => $this->getMediaId()
]) . '?preview=1&width=' . $width . '&height=' . $height . '&proportional=' . $proportional . '&fit=' . $fit;
// Show the image - scaled to the aspect ratio of this region (get from GET)
return '<div style="display:table; width:100%; height: ' . $height . 'px">
<div style="text-align:' . $align . '; display: table-cell; vertical-align: ' . $vAlign . ';">
<img src="' . $url . '" />
</div>
</div>';
}
/** @inheritDoc */
public function download(Request $request, Response $response): Response
{
$sanitizedParams = $this->getSanitizer($request->getQueryParams());
$this->getLog()->debug('Image Module: download for ' . $this->getMediaId());
$libraryLocation = $this->getConfig()->getSetting('LIBRARY_LOCATION');
$media = $this->mediaFactory->getById($this->getMediaId());
$filePath = $libraryLocation . $media->storedAs;
$this->getLog()->debug('Media Returned: ' . $media->storedAs);
$width = intval($sanitizedParams->getDouble('width'));
$height = intval($sanitizedParams->getDouble('height'));
// Get the extension if we can (module files do not have an extension stored with them)
if (stripos($media->storedAs, '.') > -1) {
$extension = explode('.', $media->storedAs)[1];
} else if (stripos($media->fileName, '.')) {
$explode = explode('.', $media->fileName);
$extension = $explode[count($explode) - 1];
} else {
$extension = null;
}
// Preview or download?
if ($sanitizedParams->getCheckbox('preview')) {
// What type of download are we?
$isThumb = $sanitizedParams->getCheckbox('isThumb') == 1;
// We expect the preview to load, manipulate and output a thumbnail (even on error).
// Therefore, we need to end output buffering and wipe any output so far.
// this means that we do not buffer the image output into memory
while (ob_get_level() > 0) {
ob_end_clean();
}
// Preview (we output the file to the browser with image headers)
try {
Img::configure(['driver' => 'gd']);
// Do we want a thumbnail
if ($isThumb) {
// Thumbnail
$thumbPath = $libraryLocation . 'tn_' . $media->storedAs;
$proportional = true;
$width = 120;
$height = 120;
$this->getLog()->debug('Thumbnail: ' . $thumbPath . ' requested');
// Does the thumbnail exist already?
$img = null;
$regenerate = true;
if (file_exists($thumbPath)) {
$img = Img::make($filePath);
if ($img->width() === $width || $img->height() === $height) {
// Correct cache
$regenerate = false;
}
}
if ($regenerate) {
// Get the full image and make a thumbnail
$img = Img::make($filePath);
$img->resize($width, $height, function ($constraint) use ($proportional) {
if ($proportional) {
$constraint->aspectRatio();
}
});
$img->save($thumbPath);
}
// Output Etag
$response = $this->cacheProvider->withEtag($response, md5_file($thumbPath));
} else {
// Not a thumbnail, output the whole file
$proportional = !$sanitizedParams->hasParam('proportional')
|| $sanitizedParams->getCheckbox('proportional') == 1;
$fit = $proportional && $sanitizedParams->getCheckbox('fit') === 1;
// only use upsize constraint, if we the requested dimensions are larger than resize limit.
$resizeLimit = $this->getConfig()->getSetting('DEFAULT_RESIZE_LIMIT', 6000);
$useUpsizeConstraint = max($width, $height) > $resizeLimit;
$this->getLog()->debug('Whole file: ' . $filePath
. ' requested with Width and Height ' . $width . ' x ' . $height
. ', proportional: ' . var_export($proportional, true)
. ', fit: ' . var_export($fit, true)
. ', upsizeConstraint ' . var_export($useUpsizeConstraint, true));
$img = Img::make($filePath);
// Output a specific width/height
if ($width > 0 && $height > 0) {
if ($fit) {
$img->fit($width, $height);
} else {
$img->resize($width, $height, function ($constraint) use ($proportional, $useUpsizeConstraint) {
if ($proportional) {
$constraint->aspectRatio();
}
if ($useUpsizeConstraint) {
$constraint->upsize();
}
});
}
}
}
// Output
$response = $this->cacheProvider->withExpires($response, '+1 week');
echo $img->encode($extension);
} catch (NotReadableException $notReadableException) {
$this->getLog()->debug($notReadableException->getTraceAsString());
$this->getLog()->error('Image not readable: ' . $notReadableException->getMessage());
// Output the thumbnail
$img = Img::make($this->getConfig()->uri('img/error.png', true));
if ($width != 0 || $height != 0) {
$img->resize($width, $height, function ($constraint) use ($proportional) {
if ($proportional) {
$constraint->aspectRatio();
}
});
}
echo $img->encode();
}
return $response;
} else {
// Download the file
return parent::download($request, $response);
}
}
/** @inheritDoc */
public function isValid()
{
if ($this->getMedia()->released == 0) {
$this->statusMessage = __('%s is pending conversion', $this->getMedia()->name);
return self::$STATUS_PLAYER;
} elseif ($this->getMedia()->released == 2) {
$resizeLimit = $this->getConfig()->getSetting('DEFAULT_RESIZE_LIMIT');
$this->statusMessage = __('%s is too large. Please ensure that none of the images in your layout are larger than %s pixels on their longest edge. Please check the allowed Resize Limit in Administration -> Settings', $this->getMedia()->name, $resizeLimit);
throw new InvalidArgumentException(__('%s is too large. Please ensure that none of the images in your layout are larger than %s pixels on their longest edge. Please check the allowed Resize Limit in Administration -> Settings', $this->getMedia()->name, $resizeLimit), 'name');
}
if (!v::intType()->min(1, true)->validate($this->getDuration())) {
throw new InvalidArgumentException(__('You must enter a duration.'), 'duration');
}
return self::$STATUS_VALID;
}
/**
* @inheritDoc
*/
public function getResource($displayId = 0)
{
return '';
}
public function hasThumbnail()
{
return true;
}
}