-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnanoPhotosProvider.JSON.class.php
517 lines (457 loc) · 17.3 KB
/
nanoPhotosProvider.JSON.class.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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
<?php
/**
* nanoPhotosProvider add-on for nanoGALLERY
*
* This is an add-on for nanoGALLERY (image gallery - http://nanogallery.brisbois.fr).
* This PHP application will publish your images and albums from a PHP webserver to nanoGALLERY.
* The content is provided on demand, one album at one time.
* Thumbnails are generated automatically.
*
* License: For personal, non-profit organizations, or open source projects (without any kind of fee), you may use nanoGALLERY for free.
* -------- ALL OTHER USES REQUIRE THE PURCHASE OF A PROFESSIONAL LICENSE.
*
*
* PHP 5.2+
* @version 0.9.3
* @author Christophe BRISBOIS - http://www.brisbois.fr/
* @Contributor Ruplahlava - https://github.com/Ruplahlava
* @Contributor EelcoA - https://github.com/EelcoA
* @Contributor eae710 - https://github.com/eae710
* @copyright Copyright 2015
* @license CC BY-NC 3.0
* @link https://github.com/Kris-B/nanoPhotosProvider
* @Support https://github.com/Kris-B/nanoPhotosProvider/issues
*
*/
require './nanoPhotosProvider.Encoding.php';
class galleryData
{
public $fullDir = '';
//public $images;
//public $URI;
}
class item
{
public $src = '';
public $srct = '';
public $title = '';
public $description = '';
public $ID = '';
public $albumID = '0';
public $kind = ''; // album, image
}
class galleryJSON
{
protected $config = array();
protected $data;
protected $albumID;
protected $album;
const CONFIG_FILE = './nanoPhotosProvider.cfg';
//const CONTENT_FOLDER = '/nanoPhotosContent';
public function __construct()
{
// retrieve the album ID in the URL
$this->album = '/';
$this->albumID = '';
if (isset($_GET['albumID'])) {
// $this->albumID = $_GET['albumID'];
$this->albumID = rawurldecode($_GET['albumID']);
}
if (!$this->albumID == '0' && $this->albumID != '' && $this->albumID != null) {
// $this->album='/'.utf8_decode(urldecode($this->albumID)).'/';
$this->album = '/' . $this->CustomDecode($this->albumID) . '/';
} else {
$this->albumID = '0';
}
$this->data = new galleryData();
$this->setConfig(self::CONFIG_FILE);
// $this->data->fullDir = __DIR__ . self::CONTENT_FOLDER . ($this->album);
$this->data->fullDir = __DIR__ . ($this->config['contentFolder']) . ($this->album);
$lstImages = array();
$lstAlbums = array();
$dh = opendir($this->data->fullDir);
// loop the folder to retrieve images and albums
if ($dh != false) {
while (false !== ($filename = readdir($dh))) {
$filenameU=strtoupper($filename);
if (is_file($this->data->fullDir . $filename) ) {
// it's a file
if ($filename != '.' &&
$filename != '..' &&
$filename != '_thumbnails' &&
preg_match("/\.(" . $this->config['fileExtensions'] . ")*$/i", $filename) &&
strpos($filename, $this->config['imageBlackListDetector']) == false )
{
$lstImages[] = $this->prepare_data($filename, 'IMAGE');
}
}
else {
// it's a folder
$files = glob($this->data->fullDir . $filename."/*.{".str_replace("|",",",$this->config['fileExtensions'])."}", GLOB_BRACE); // to check if folder contains images
if ($filename != '.' &&
$filename != '..' &&
$filename != '_thumbnails' &&
strpos($filename, $this->config['albumBlackListDetector']) == false &&
!empty($files) )
{
$lstAlbums[] = $this->prepare_data($filename, 'ALBUM');
}
}
}
closedir($dh);
}
// sort data
usort($lstAlbums, array('galleryJSON','compare'));
usort($lstImages, array('galleryJSON','compare'));
// return the data
header('Content-Type: application/json; charset=utf-8');
$output = json_encode(array_merge($lstAlbums, $lstImages)); // UTF-8 encoding is mandatory
if (isset($_GET['jsonp'])) {
// return in JSONP
echo $_GET['jsonp'] . '(' . $output . ')';
} else {
// return in JSON
echo $output;
}
}
protected function setConfig($filePath)
{
$config = parse_ini_file($filePath, true);
$this->config['contentFolder'] = $config['config']['contentFolder'];
$this->config['fileExtensions'] = $config['config']['fileExtensions'];
$this->config['sortOrder'] = strtoupper($config['config']['sortOrder']);
$this->config['titleDescSeparator'] = $config['config']['titleDescSeparator'];
$this->config['albumCoverDetector'] = $config['config']['albumCoverDetector'];
$this->config['albumBlackListDetector'] = strtoupper($config['config']['albumBlackListDetector']);
$this->config['imageBlackListDetector'] = $config['config']['imageBlackListDetector'];
if (isset($config['thumbnailSizes']['width']) && isset($config['thumbnailSizes']['height'])) {
$this->config['thumbnailsGenerate'] = true;
$this->config['thumbnailSizes']['width'] = $config['thumbnailSizes']['width'];
$this->config['thumbnailSizes']['height'] = $config['thumbnailSizes']['height'];
if (isset($config['thumbnailSizes']['crop'])) {
$this->config['thumbnailSizes']['crop'] = $config['thumbnailSizes']['crop'];
} else {
$this->config['thumbnailSizes']['crop'] = false;
}
} else {
$this->config['thumbnailsGenerate'] = false;
}
}
/**
* RETRIEVE THE COVER IMAGE (THUMBNAIL) OF ONE ALBUM (FOLDER)
*
* @param string $baseFolder
* @return string
*/
protected function GetAlbumCover($baseFolder)
{
// look for cover image
$files = glob($baseFolder . '/' . $this->config['albumCoverDetector'] . '*.*');
if (count($files) > 0) {
$i = basename($files[0]);
if (preg_match("/\.(" . $this->config['fileExtensions'] . ")*$/i", $i)) {
$tn = $this->GetThumbnail($baseFolder, $i);
if ($tn != '') {
return $tn;
}
}
}
// no cover image found --> use the first image for the cover
$i = $this->GetFirstImageFolder($baseFolder);
if ($i != '') {
$tn = $this->GetThumbnail($baseFolder, $i);
if ($tn != '') {
return $tn;
}
}
return '';
}
/**
* Retrieve the first image of one folder --> ALBUM THUMBNAIL
*
* @param string $folder
* @return string
*/
protected function GetFirstImageFolder($folder)
{
$image = '';
$dh = opendir($folder);
while (false !== ($filename = readdir($dh))) {
if (is_file($folder . '/' . $filename) && preg_match("/\.(" . $this->config['fileExtensions'] . ")*$/i", $filename)) {
$image = $filename;
break;
}
}
closedir($dh);
return $image;
}
/**
*
* @param object $a
* @param object $b
* @return int
*/
protected function compare($a, $b)
{
$al = strtolower($a->title);
$bl = strtolower($b->title);
if ($al == $bl) {
return 0;
}
$b = false;
switch ($this->config['sortOrder']) {
case 'DESC' :
if ($al < $bl) {
$b = true;
}
break;
case 'ASC':
default:
if ($al > $bl) {
$b = true;
}
break;
}
return ($b) ? +1 : -1;
}
/**
* RETRIEVE ONE IMAGE'S THUMBNAIL
*
* @param type $baseFolder
* @param type $filename
* @return type
*/
protected function GetThumbnail($baseFolder, $filename)
{
$tn = $baseFolder . '_thumbnails/' . $filename;
if (file_exists($tn)) {
//if( filemtime($tn) < filemtime($baseFolder.$filename) ) {
// image file is older as the thumbnail file
return '_thumbnails/' . $filename;
//}
}
// generate the thumbnail
$tn = $this->GenerateThumbnail($baseFolder, $filename);
if ($tn != '') {
return $tn;
}
// fallback: original image (no thumbnail)
return $filename;
}
/**
* GENERATE ONE THUMBNAIL
*
* @param type $baseFolder
* @param type $filename
* @return string
*/
protected function GenerateThumbnail($baseFolder, $filename)
{
if (!$this->config['thumbnailsGenerate'] == true ) {
return '';
}
$td = $baseFolder . '/_thumbnails';
if (!file_exists($td)) {
mkdir($td, 0777, true);
}
//$orgImage = imagecreatefromjpeg($baseFolder.'/'.$filename);
$size = getimagesize($baseFolder . '/' . $filename);
switch ($size['mime']) {
case 'image/jpeg':
$orgImage = imagecreatefromjpeg($baseFolder . '/' . $filename);
break;
case 'image/gif':
$orgImage = imagecreatefromgif($baseFolder . '/' . $filename);
break;
case 'image/png':
$orgImage = imagecreatefrompng($baseFolder . '/' . $filename);
break;
default:
return '';
break;
}
$width = $size[0];
$height = $size[1];
$tnFilename = $baseFolder . '/_thumbnails/' . $filename;
$thumbWidth = $this->config['thumbnailSizes']['width'];
$thumbHeight = $this->config['thumbnailSizes']['height'];
$originalAspect = $width / $height;
$thumbAspect = $thumbWidth / $thumbHeight;
if ($this->config['thumbnailSizes']['crop']) {
// CROP THE IMAGE
// some inspiration found in donkeyGallery (from Gix075) https://github.com/Gix075/donkeyGallery
if ($originalAspect >= $thumbAspect) {
// If image is wider than thumbnail (in aspect ratio sense)
$newHeight = $thumbHeight;
$newWidth = $width / ($height / $thumbHeight);
} else {
// If the thumbnail is wider than the image
$newWidth = $thumbWidth;
$newHeight = $height / ($width / $thumbWidth);
}
$thumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
// Resize and crop
imagecopyresampled($thumb, $orgImage, 0 - ($newWidth - $thumbWidth) / 2, // dest_x: Center the image horizontally
0 - ($newHeight - $thumbHeight) / 2, // dest-y: Center the image vertically
0, 0, // src_x, src_y
$newWidth, $newHeight, $width, $height);
} else {
// DO NOT CROP
if ($originalAspect >= $thumbAspect) {
$newHeight = $height / $width * $thumbWidth;
$newWidth = $thumbWidth;
} else {
$newWidth = $width / $height * $thumbHeight;
$newHeight = $thumbHeight;
}
$thumb = imagecreatetruecolor($newWidth, $newHeight);
// Resize
imagecopyresampled($thumb, $orgImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
}
switch ($size['mime']) {
case 'image/jpeg':
imagejpeg($thumb, $tnFilename, 90);
break;
case 'image/gif':
imagegif($thumb, $tnFilename);
break;
case 'image/png':
imagepng($thumb, $tnFilename, 1);
break;
}
return '_thumbnails/' . $filename;
}
/**
* Extract title and description from filename
*
* @param string $filename
* @param boolean $isImage
* @return \item
*/
protected function GetTitleDesc($filename, $isImage)
{
if ($isImage) {
$filename = $this->file_ext_strip($filename);
}
$oneItem = new item();
if (strpos($filename, $this->config['titleDescSeparator']) > 0) {
// title and description
$s = explode($this->config['titleDescSeparator'], $filename);
$oneItem->title = $this->CustomEncode($s[0]);
if ($isImage) {
$oneItem->description = $this->CustomEncode(preg_replace('/.[^.]*$/', '', $s[1]));
} else {
$oneItem->description = $this->CustomEncode($s[1]);
}
} else {
// only title
if ($isImage) {
$oneItem->title = $this->CustomEncode($filename); //(preg_replace('/.[^.]*$/', '', $filename));
} else {
$oneItem->title = $this->CustomEncode($filename);
}
$oneItem->description = '';
}
$oneItem->title = str_replace($this->config['albumCoverDetector'], '', $oneItem->title); // filter cover detector string
return $oneItem;
}
/**
* Returns only the file extension (without the period).
*
* @param string $filename
* @return string
*/
protected function file_ext($filename)
{
if (!preg_match('/./', $filename)) {
return '';
}
return preg_replace('/^.*./', '', $filename);
}
/**
* Returns the file name, less the extension.
*
* @param string $filename
* @return string
*/
protected function file_ext_strip($filename)
{
return preg_replace('/.[^.]*$/', '', $filename);
}
/**
*
* @param string $s
* @return string
*/
protected function CustomEncode($s)
{
return \ForceUTF8\Encoding::toUTF8(($s));
//return \ForceUTF8\Encoding::fixUTF8(($s));
}
/**
*
* @param type $s
* @return type
*/
protected function CustomDecode($s)
{
return utf8_decode($s);
// return $s;
}
protected function prepare_data($filename, $kind)
{
$oneItem = new item();
// if (is_file($this->data->fullDir . $filename) && preg_match("/\.(" . $this->config['fileExtensions'] . ")*$/i", $filename)) {
if ( $kind == 'IMAGE' ) {
// ONE IMAGE
$oneItem->kind = 'image';
$e = $this->GetTitleDesc($filename, true);
$oneItem->title = $e->title;
$oneItem->description = $e->description;
$oneItem->src = rawurlencode($this->CustomEncode($this->config['contentFolder'] . $this->album . '/' . $filename));
$tn = $this->GetThumbnail($this->data->fullDir, $filename);
$oneItem->srct = rawurlencode($this->CustomEncode($this->config['contentFolder'] . $this->album . $tn));
$size = getimagesize($this->data->fullDir . $tn);
$oneItem->imgtWidth = $size[0];
$oneItem->imgtHeight = $size[1];
$oneItem->albumID = rawurlencode($this->albumID);
if ($this->albumID == '0' || $this->albumID == '') {
$oneItem->ID = rawurlencode($this->CustomEncode($e->title));
} else {
$oneItem->ID = rawurlencode($this->albumID . $this->CustomEncode('/' . $e->title));
}
return $oneItem;
} else {
// ONE ALBUM
$oneItem->kind = 'album';
$e = $this->GetTitleDesc($filename, false);
$oneItem->title = $e->title;
$oneItem->description = $e->description;
$oneItem->albumID = rawurlencode($this->albumID);
if ($this->albumID == '0' || $this->albumID == '') {
// $oneItem->ID = $this->CustomEncode($filename);
$oneItem->ID = rawurlencode($this->CustomEncode($filename));
} else {
// $oneItem->ID = $this->albumID . $this->CustomEncode('/' . $filename);
$oneItem->ID = rawurlencode($this->albumID . $this->CustomEncode('/' . $filename));
}
$s = $this->GetAlbumCover($this->data->fullDir . $filename . '/');
if ($s != '') {
// a cover has been found
$path = '';
if ($this->albumID == '0') {
$path = $filename;
} else {
$path = $this->album . '/' . $filename;
}
$oneItem->srct = rawurlencode($this->CustomEncode($this->config['contentFolder'] .'/'. $path . '/' . $s));
// $oneItem->srct = $this->CustomEncode(self::CONTENT_FOLDER .'/'. $path . '/' . $s);
$size = getimagesize(__DIR__ . $this->config['contentFolder'] . '/' . $path . '/' . $s);
$oneItem->imgtWidth = $size[0];
$oneItem->imgtHeight = $size[1];
return $oneItem;
}
}
}
}
?>