-
Notifications
You must be signed in to change notification settings - Fork 2
/
Gallery.php
454 lines (421 loc) · 14.6 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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<?php
use Intervention\Image\ImageManager;
/**
* Pico dummy plugin - a template for plugins
*
* You're a plugin developer? This template may be helpful :-)
* Simply remove the events you don't need and add your own logic.
*
* @author Klas Gidlöv
* @link http://gidlov.com/en/code/pico-gallery
* @license http://opensource.org/licenses/MIT The MIT License
* @version 1.2.2
*/
final class Gallery extends AbstractPicoPlugin
{
private $requested_url;
private $pico_config;
private $gallery_config;
private $single_image;
private $requested_gallery;
private $current_gallery;
private $file;
private $root_dir;
private $content_dir;
private $config_dir;
private $themes_dir;
private $pre_thumb;
/**
* This plugin is enabled by default?
*
* @see AbstractPicoPlugin::$enabled
* @var boolean
*/
protected $enabled = true;
/**
* This plugin depends on ...
*
* @see AbstractPicoPlugin::$dependsOn
* @var string[]
*/
protected $dependsOn = array();
/**
* Triggered after Pico has loaded all available plugins
*
* This event is triggered nevertheless the plugin is enabled or not.
* It is NOT guaranteed that plugin dependencies are fulfilled!
*
* @see Pico::getPlugin()
* @see Pico::getPlugins()
* @param object[] &$plugins loaded plugin instances
* @return void
*/
public function onPluginsLoaded(array &$plugins)
{
$this->root_dir = $this->__call('getRootDir', array());
$this->pico_config_dir = $this->__call('getConfigDir', array());
$this->themes_dir = $this->__call('getThemesDir', array());
}
/**
* Triggered after Pico has read its configuration
*
* @see Pico::getConfig()
* @param array &$config array of config variables
* @return void
*/
public function onConfigLoaded(array &$config)
{
$this->pico_config = $config;
if (isset($config['gallery'])) {
$this->gallery_config = $config['gallery'];
}
if (null !== ($this->__call('getConfig', array('content_dir')))) {
$this->content_dir = $this->__call('getConfig', array('content_dir'));
}
}
/**
* Triggered after Pico has evaluated the request URL
*
* @see Pico::getRequestUrl()
* @param string &$url part of the URL describing the requested contents
* @return void
*/
public function onRequestUrl(&$url)
{
if (!isset($this->gallery_config)) {
return;
}
$this->requested_url = $url;
$this->requested_url = ($this->requested_url == '') ? 'index' : $this->requested_url;
$this->requested_gallery = array();
foreach (array_keys($this->gallery_config) as $gallery_name) {
if ($this->gallery_config[$gallery_name]['page'] == $this->requested_url) {
$this->requested_gallery[] = $gallery_name;
}
$url_strip = str_replace($this->gallery_config[$gallery_name]['page'].'/', '', $this->requested_url);
$url_part = explode('/', $url_strip);
if ($url_part[0] == $gallery_name) {
if (isset($url_part[1]) && $url_part[1] == 'flush' && isset($url_part[2]) && $url_part[2] == $this->gallery_config[$gallery_name]['flush']) {
$this->flush($gallery_name);
$url = '';
} elseif (isset($url_part[1]) && is_file($this->gallery_config[$gallery_name]['image_path'].'/'.$url_part[1])) {
$this->current_gallery = $gallery_name;
$this->single_image = $this->gallery_config[$gallery_name]['image_path'].'/'.$url_part[1];
unset($url_part[count($url_part)-1]);
$this->file = $this->content_dir.$this->gallery_config[$gallery_name]['page'].$this->pico_config['content_ext'];
}
}
}
}
/**
* Triggered after Pico has discovered the content file to serve
*
* @see Pico::getBaseUrl()
* @see Pico::getRequestFile()
* @param string &$file absolute path to the content file to serve
* @return void
*/
public function onRequestFile(&$file)
{
// your code
}
/**
* Triggered before Pico reads the contents of the file to serve
*
* @see Pico::loadFileContent()
* @see DummyPlugin::onContentLoaded()
* @param string &$file path to the file which contents will be read
* @return void
*/
public function onContentLoading(&$file)
{
if ($this->file) {
$file = $this->file;
}
}
/**
* Triggered after Pico has read the contents of the file to serve
*
* @see Pico::getRawContent()
* @param string &$rawContent raw file contents
* @return void
*/
public function onContentLoaded(&$rawContent)
{
// your code
}
/**
* Triggered before Pico reads the contents of a 404 file
*
* @see Pico::load404Content()
* @see DummyPlugin::on404ContentLoaded()
* @param string &$file path to the file which contents were requested
* @return void
*/
public function on404ContentLoading(&$file)
{
// your code
}
/**
* Triggered after Pico has read the contents of the 404 file
*
* @see Pico::getRawContent()
* @param string &$rawContent raw file contents
* @return void
*/
public function on404ContentLoaded(&$rawContent)
{
// your code
}
/**
* Triggered when Pico reads its known meta header fields
*
* @see Pico::getMetaHeaders()
* @param string[] &$headers list of known meta header
* fields; the array value specifies the YAML key to search for, the
* array key is later used to access the found value
* @return void
*/
public function onMetaHeaders(array &$headers)
{
// your code
}
/**
* Triggered before Pico parses the meta header
*
* @see Pico::parseFileMeta()
* @see DummyPlugin::onMetaParsed()
* @param string &$rawContent raw file contents
* @param string[] &$headers known meta header fields
* @return void
*/
public function onMetaParsing(&$rawContent, array &$headers)
{
// your code
}
/**
* Triggered after Pico has parsed the meta header
*
* @see Pico::getFileMeta()
* @param string[] &$meta parsed meta data
* @return void
*/
public function onMetaParsed(array &$meta)
{
// your code
}
/**
* Triggered before Pico parses the pages content
*
* @see Pico::prepareFileContent()
* @see DummyPlugin::prepareFileContent()
* @see DummyPlugin::onContentParsed()
* @param string &$rawContent raw file contents
* @return void
*/
public function onContentParsing(&$rawContent)
{
// your code
}
/**
* Triggered after Pico has prepared the raw file contents for parsing
*
* @see Pico::parseFileContent()
* @see DummyPlugin::onContentParsed()
* @param string &$content prepared file contents for parsing
* @return void
*/
public function onContentPrepared(&$content)
{
// your code
}
/**
* Triggered after Pico has parsed the contents of the file to serve
*
* @see Pico::getFileContent()
* @param string &$content parsed contents
* @return void
*/
public function onContentParsed(&$content)
{
// your code
}
/**
* Triggered before Pico reads all known pages
*
* @see Pico::readPages()
* @see DummyPlugin::onSinglePageLoaded()
* @see DummyPlugin::onPagesLoaded()
* @return void
*/
public function onPagesLoading()
{
// your code
}
/**
* Triggered when Pico reads a single page from the list of all known pages
*
* The `$pageData` parameter consists of the following values:
*
* | Array key | Type | Description |
* | -------------- | ------ | ---------------------------------------- |
* | id | string | relative path to the content file |
* | url | string | URL to the page |
* | title | string | title of the page (YAML header) |
* | description | string | description of the page (YAML header) |
* | author | string | author of the page (YAML header) |
* | time | string | timestamp derived from the Date header |
* | date | string | date of the page (YAML header) |
* | date_formatted | string | formatted date of the page |
* | raw_content | string | raw, not yet parsed contents of the page |
* | meta | string | parsed meta data of the page |
*
* @see DummyPlugin::onPagesLoaded()
* @param array &$pageData data of the loaded page
* @return void
*/
public function onSinglePageLoaded(array &$pageData)
{
// your code
}
/**
* Triggered after Pico has read all known pages
*
* See {@link DummyPlugin::onSinglePageLoaded()} for details about the
* structure of the page data.
*
* @see Pico::getPages()
* @see Pico::getCurrentPage()
* @see Pico::getPreviousPage()
* @see Pico::getNextPage()
* @param array[] &$pages data of all known pages
* @param array|null &$currentPage data of the page being served
* @param array|null &$previousPage data of the previous page
* @param array|null &$nextPage data of the next page
* @return void
*/
public function onPagesLoaded(
array &$pages,
array &$currentPage = null,
array &$previousPage = null,
array &$nextPage = null
) {
// your code
}
/**
* Triggered before Pico registers the twig template engine
*
* @return void
*/
public function onTwigRegistration()
{
// your code
}
/**
* Triggered before Pico renders the page
*
* @see Pico::getTwig()
* @see DummyPlugin::onPageRendered()
* @param Twig_Environment &$twig twig template engine
* @param array &$twigVariables template variables
* @param string &$templateName file name of the template
* @return void
*/
public function onPageRendering(Twig_Environment &$twig, array &$twigVariables, &$templateName)
{
// your code
}
/**
* Triggered after Pico has rendered the page
*
* @param string &$output contents which will be sent to the user
* @return void
*/
public function onPageRendered(&$output)
{
if ($this->requested_gallery) {
// Get the gallery.
foreach ($this->requested_gallery as $gallery_name) {
if (preg_match('/%'.$gallery_name.'%/mi', $output, $match) && $match[0]) {
$output = str_replace('%'.$gallery_name.'%', $this->markup($gallery_name), $output);
}
}
} elseif ($this->single_image && $this->current_gallery) {
// Get the image.
foreach (array_keys($this->gallery_config) as $gallery_name) {
$replace = $gallery_name == $this->current_gallery ? $this->markup($this->current_gallery) : '';
$output = str_replace('%'.$gallery_name.'%', $replace, $output);
}
}
}
protected function flush($name)
{
$image_path = $this->gallery_config[$name]['image_path'];
$thumb_path = $this->gallery_config[$name]['thumb_path'];
if (!is_dir($image_path)) {
return;
}
$files = glob($thumb_path.DIRECTORY_SEPARATOR.'*');
foreach($files as $file) {
// Delete all files in the thumbnail folder.
if (is_file($file)) {
unlink($file);
}
}
foreach(glob($image_path.'/{*.jpg,*.png,*.gif}', GLOB_BRACE) as $image) {
// Generate new thumbnails.
$size = isset($this->gallery_config[$name]['thumb_size']) ? $this->gallery_config[$name]['thumb_size'] : array(200, 200);
$file_name = basename($image);
$img = new ImageManager();
$img->make($image)
->fit($size[0], $size[1])
->save($thumb_path.DIRECTORY_SEPARATOR.'thumb_'.$file_name, 80);
}
header('Location: ' . $this->pico_config['base_url'].$this->gallery_config[$name]['page']);
}
protected function get(string $name, string $config, string $default = '')
{
if (isset($this->gallery_config[$name][$config])) {
return $this->gallery_config[$name][$config];
} else {
return $default;
}
}
protected function markup($name)
{
$return = '';
$image_path = $this->gallery_config[$name]['image_path'];
$thumb_path = $this->gallery_config[$name]['thumb_path'];
if ($this->single_image) {
// Markup for a image
$return .= $this->get($name, 'before_image');
$return .= '<img src="'.$this->pico_config['base_url'].'/'.$this->single_image.'" alt="'.$this->get($name, 'alt_class').'" class="'.$this->get($name, 'image_class').'">'."\n";
$return .= $this->get($name, 'after_image');
//$return .= $after_image;
return $return;
} else {
// Markup for a gallery.
$return .= $this->get($name, 'before_gallery');
$gallery = glob($image_path.'/{*.jpg,*.png,*.gif}', GLOB_BRACE);
if (isset($this->gallery_config[$name]['sort_by']) && $this->gallery_config[$name]['sort_by'] == 'random') {
shuffle($gallery);
}
if (isset($this->gallery_config[$name]['order_by']) && $this->gallery_config[$name]['order_by'] == 'reverse') {
array_reverse($gallery);
}
foreach($gallery as $image) {
$alt_image = (isset($this->gallery_config[$name]['alt_image'])) ? $this->gallery_config[$name]['alt_image'] : pathinfo($image, PATHINFO_FILENAME);
if (isset($this->gallery_config[$name]['exclude']) && in_array(substr($image, strrpos($image, '/')+1), $this->gallery_config[$name]['exclude'])) {
// Skip files in the exclude array.
continue;
}
$file_name = basename($image);
$return .= $this->get($name, 'before_thumbnail');
$return .= '<a href="'.$this->pico_config['base_url'].''.$this->requested_url.'/'.$name.'/'.$file_name.'" class="'.$this->get($name, 'thumbnail_link_class').'"><img src="'.$this->pico_config['base_url'].''.$thumb_path.'/'.'thumb_'.$file_name.'" alt="'.$alt_image.'" class="'.$this->get($name, 'thumbnail_image_class').'"></a>'."\n";
$return .= $this->get($name, 'after_thumbnail');
}
$return .= $this->get($name, 'after_gallery');
return $return;
}
}
}