-
Notifications
You must be signed in to change notification settings - Fork 4
/
Gallery.php
111 lines (101 loc) · 3.32 KB
/
Gallery.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
<?php
namespace Sunlight;
use Kuria\Url\Url;
use Sunlight\Image\ImageException;
use Sunlight\Image\ImageLoader;
use Sunlight\Image\ImageService;
use Sunlight\Image\ImageStorage;
use Sunlight\Image\ImageTransformer;
use Sunlight\Util\StringGenerator;
use Sunlight\Util\UrlHelper;
class Gallery
{
/**
* Render a gallery image
*
* @param array $img image data
* @param string|null $lightboxid lightbox group ID, if any
* @param array $resizeOptions {@see ImageTransformer::resize()}
*/
static function renderImage(array $img, ?string $lightboxid, array $resizeOptions): string
{
if (UrlHelper::isAbsolute($img['full'])) {
$fullUrl = $img['full'];
$fullFile = null;
} else {
$fullUrl = Router::path($img['full']);
$fullFile = SL_ROOT . $img['full'];
}
if (!empty($img['prev'])) {
if (UrlHelper::isAbsolute($img['prev'])) {
$prevUrl = $img['prev'];
} else {
$prevUrl = Router::path($img['prev']);
}
} elseif ($fullFile !== null) {
$prevUrl = Router::file(ImageService::getThumbnail('gallery', $fullFile, $resizeOptions));
} else {
$prevUrl = $fullUrl;
}
if ($img['title']) {
$alt = $img['title'];
} elseif ($fullFile) {
$alt = basename($fullFile);
} else {
$alt = basename(Url::parse($fullUrl)->getPath());
}
$image = Extend::buffer('gallery.image.render', [
'image' => $img,
'full_url' => $fullUrl,
'full_file' => $fullFile,
'prev_url' => $prevUrl,
'alt' => $alt,
'lightbox_id' => $lightboxid
]);
if ($image === '') {
$image = '<a'
. ' href="' . _e($fullUrl) . '" target="_blank"'
. (isset($lightboxid) ? Extend::buffer('image.lightbox', ['group' => 'gal_' . $lightboxid]) : '')
. (($img['title']) ? ' title="' . _e($img['title']) . '"' : '')
. '>'
. '<img'
. ' src="' . _e($prevUrl) . '"'
. ' alt="' . _e($alt) . '"'
. '>'
. "</a>\n";
}
return $image;
}
/**
* Upload a gallery image
*
* Returns image web path or NULL on failure.
*/
static function uploadImage(
string $source,
string $originalFilename,
string $storageDir,
?ImageException &$exception
): ?string {
$uid = StringGenerator::generateUniqueHash();
$format = ImageLoader::getFormat($originalFilename);
return ImageService::process(
'gallery',
$source,
ImageStorage::getPath($storageDir, $uid, $format),
[
'resize' => [
'mode' => ImageTransformer::RESIZE_FIT,
'keep_smaller' => true,
'w' => Settings::get('galuploadresize_w'),
'h' => Settings::get('galuploadresize_h'),
],
'write' => ['jpg_quality' => 95],
'format' => $format,
],
$exception
)
? ImageStorage::getWebPath($storageDir, $uid, $format)
: null;
}
}