1212 * -------- ALL OTHER USES REQUIRE THE PURCHASE OF A COMMERCIAL LICENSE.
1313 *
1414 * PHP 5.2+
15- * @version 1.1 .0
15+ * @version 1.2 .0
1616 * @author Christophe BRISBOIS - http://www.brisbois.fr/
1717 * @copyright Copyright 2015+
1818 * @license GPL v3 and commercial
1919 * @link https://github.com/nanostudio-org/nanoPhotosProvider2
2020 * @Support https://github.com/nanostudio-org/nanoPhotosProvider2/issues
2121 *
22+ *
23+ * Thanks to:
24+ * - Kevin Robert Keegan - https://github.com/krkeegan
25+ * - Jesper Cockx - https://github.com/jespercockx
26+ * - bhartvigsen - https://github.com/bhartvigsen
2227 */
2328
2429require './nano_photos_provider2.encoding.php ' ;
@@ -42,10 +47,10 @@ class item
4247 public $ t_width = array (); // thumbnails width
4348 public $ t_height = array (); // thumbnails height
4449 public $ dc = '#888 ' ; // image dominant color
45- public $ mtime = '' ; // ctime of image or album
46- public $ ctime = '' ; // mtime of image or album
50+ public $ mtime = '' ; // modified time of image or album
51+ public $ ctime = '' ; // creation time of image or album
4752 public $ sort = '' ; // a sort string not displayed
48- // public $dcGIF = '#000'; // image dominant color
53+ // public $dcGIF = '#000'; // image dominant color
4954
5055
5156}
@@ -63,6 +68,7 @@ class galleryJSON
6368 protected $ currentItem ;
6469
6570 const CONFIG_FILE = './nano_photos_provider2.cfg ' ;
71+ const APP_VERSION = '1.2 ' ;
6672
6773 public function __construct ()
6874 {
@@ -71,6 +77,7 @@ public function __construct()
7177 $ this ->albumID = '' ;
7278 if (isset ($ _GET ['albumID ' ])) {
7379 $ this ->albumID = rawurldecode ($ _GET ['albumID ' ]);
80+
7481 }
7582 if (!$ this ->albumID == '0 ' && $ this ->albumID != '' && $ this ->albumID != null ) {
7683 $ this ->album = '/ ' . $ this ->CustomDecode ($ this ->albumID ) . '/ ' ;
@@ -100,6 +107,65 @@ public function __construct()
100107
101108 $ dh = opendir ($ this ->data ->fullDir );
102109
110+ // check if cached JSON is up to date
111+ $ JSON_file = $ this ->data ->fullDir . '_thumbnails/cache.json ' ;
112+ if (file_exists ( $ JSON_file )) {
113+ $ JSON_time = filemtime ($ JSON_file );
114+
115+ // loop to compare files/directories creation time
116+ if ($ dh != false ) {
117+
118+ $ uptodate = true ;
119+ while (false !== ($ filename = readdir ($ dh ))) {
120+ if (is_file ($ this ->data ->fullDir . $ filename ) ) {
121+ // it's a file
122+ if ($ filename != '. ' &&
123+ $ filename != '.. ' &&
124+ $ filename != '_thumbnails ' &&
125+ // preg_match - If the i modifier is set, letters in the pattern match both upper and lower case letters
126+ preg_match ("/\.( " . $ this ->config ['fileExtensions ' ] . ")*$/i " , $ filename ) &&
127+ strpos ($ filename , $ this ->config ['ignoreDetector ' ]) == false )
128+ {
129+ // check creation and modification time
130+ if ( filemtime ( $ this ->data ->fullDir . $ filename ) > $ JSON_time || filectime ( $ this ->data ->fullDir . $ filename ) > $ JSON_time ) {
131+ $ uptodate = false ;
132+ break ;
133+ }
134+ }
135+ }
136+ else {
137+ // it's a folder
138+ $ files = preg_grep ('~\.( ' .$ this ->config ['fileExtensions ' ].')$~i ' , scandir ($ this ->data ->fullDir . $ filename )); // to check if folder contains images
139+ if ($ filename != '. ' &&
140+ $ filename != '.. ' &&
141+ $ filename != '_thumbnails ' &&
142+ strpos ($ filename , $ this ->config ['ignoreDetector ' ]) == false &&
143+ !empty ($ files ) )
144+ {
145+ // check creation and modification time
146+ if ( filemtime ( $ this ->data ->fullDir . $ filename ) > $ JSON_time || filectime ( $ this ->data ->fullDir . $ filename ) > $ JSON_time ) {
147+ $ uptodate = false ;
148+ break ;
149+ }
150+ // $lstAlbums[] = $this->PrepareData($filename, 'ALBUM');
151+ }
152+ }
153+ }
154+
155+ if ( $ uptodate == true ) {
156+ // JSON cached file is uptodate -> use it
157+ $ objData = file_get_contents ($ JSON_file );
158+ $ response = unserialize ($ objData );
159+ if ( !empty ($ response ) ){
160+ closedir ($ dh );
161+ $ this ->SendData ($ response );
162+ exit ;
163+ }
164+ }
165+
166+ rewinddir ( $ dh );
167+ }
168+ }
103169 // loop the folder to retrieve images and albums
104170 if ($ dh != false ) {
105171 while (false !== ($ filename = readdir ($ dh ))) {
@@ -117,7 +183,7 @@ public function __construct()
117183 else {
118184 // it's a folder
119185 //$files = glob($this->data->fullDir . $filename."/*.{".str_replace("|",",",$this->config['fileExtensions'])."}", GLOB_BRACE); // to check if folder contains images - warning - glob is not supported by all platforms
120- $ files = preg_grep ('~\.( ' .$ this ->config ['fileExtensions ' ].')$~ ' , scandir ($ this ->data ->fullDir . $ filename )); // to check if folder contains images
186+ $ files = preg_grep ('~\.( ' .$ this ->config ['fileExtensions ' ].')$~i ' , scandir ($ this ->data ->fullDir . $ filename )); // to check if folder contains images
121187 if ($ filename != '. ' &&
122188 $ filename != '.. ' &&
123189 $ filename != '_thumbnails ' &&
@@ -130,14 +196,30 @@ public function __construct()
130196 }
131197 closedir ($ dh );
132198 }
199+ else {
200+ // album not found
201+ $ response = array ( 'nano_status ' => 'error ' , 'nano_message ' => 'album not found: ' . rawurlencode ($ this ->data ->fullDir ) );
202+ $ this ->SendData ($ response );
203+ exit ;
204+ }
133205
134206 // sort data
135- usort ($ lstAlbums , array ('galleryJSON ' ,'CompareAlbum ' ));
136- usort ($ lstImages , array ('galleryJSON ' ,'CompareFile ' ));
207+ // usort($lstAlbums, array('galleryJSON', 'Compare'));
208+ // usort($lstImages, array('galleryJSON', 'Compare'));
209+ usort ($ lstAlbums , array ('galleryJSON ' , 'CompareAlbum ' ));
210+ usort ($ lstImages , array ('galleryJSON ' , 'CompareFile ' ));
137211
138212 $ response = array ('nano_status ' => 'ok ' , 'nano_message ' => '' , 'album_content ' => array_merge ($ lstAlbums , $ lstImages ));
139213
140214 $ this ->SendData ($ response );
215+ // cache JSON to disk
216+ if (!file_exists ( $ this ->data ->fullDir . '_thumbnails ' )) {
217+ mkdir ( $ this ->data ->fullDir . '_thumbnails ' , 0755 , true );
218+ }
219+ $ fp = fopen ($ JSON_file , "w " );
220+ fwrite ($ fp , serialize ( $ response ));
221+ fclose ($ fp );
222+
141223 }
142224
143225 /**
@@ -196,6 +278,8 @@ protected function SendData( $response )
196278 // set the content-type header
197279 header ('Content-Type: application/json; charset=utf-8 ' );
198280
281+ // add app version
282+ $ response [nanophotosprovider] = self ::APP_VERSION ;
199283 // return the data
200284 $ output = json_encode ($ response ); // UTF-8 encoding is mandatory
201285 if (isset ($ _GET ['jsonp ' ])) {
@@ -218,13 +302,13 @@ protected function setConfig($filePath)
218302 $ this ->config ['sortOrder ' ] = strtoupper ($ config ['config ' ]['sortOrder ' ]);
219303 // Check if a different sort order is specified for albums
220304 if (array_key_exists ('sortOrderAlbum ' , $ config ['config ' ])){
221- $ this ->config ['sortOrderAlbum ' ] = strtoupper ($ config ['config ' ]['sortOrderAlbum ' ]);
305+ $ this ->config ['sortOrderAlbum ' ] = strtoupper ($ config ['config ' ]['sortOrderAlbum ' ]);
222306 }
223307 else {
224- $ this ->config ['sortOrderAlbum ' ] = strtoupper ($ config ['config ' ]['sortOrder ' ]);
308+ $ this ->config ['sortOrderAlbum ' ] = strtoupper ($ config ['config ' ]['sortOrder ' ]);
225309 }
226310 $ this ->config ['titleDescSeparator ' ] = $ config ['config ' ]['titleDescSeparator ' ];
227- $ this ->config ['sortPrefixSeparator ' ] = $ config ['config ' ]['sortPrefixSeparator ' ];
311+ $ this ->config ['sortPrefixSeparator ' ] = $ config ['config ' ]['sortPrefixSeparator ' ];
228312 $ this ->config ['albumCoverDetector ' ] = $ config ['config ' ]['albumCoverDetector ' ];
229313 $ this ->config ['ignoreDetector ' ] = strtoupper ($ config ['config ' ]['ignoreDetector ' ]);
230314
@@ -320,12 +404,14 @@ protected function GetFirstImageFolder($folder)
320404 return $ image ;
321405 }
322406
323- /**
324- *
407+
408+
409+ /**
410+ * FOR FILES
325411 * @param object $a
326412 * @param object $b
327413 * @return int
328- */
414+ */
329415 protected function CompareFile ($ a , $ b )
330416 {
331417 $ order_array = explode ("_ " , $ this ->config ['sortOrder ' ]);
@@ -369,9 +455,9 @@ protected function CompareFile($a, $b)
369455 }
370456 return ($ b ) ? +1 : -1 ;
371457 }
372-
458+
373459 /**
374- *
460+ * FOR ALBUMS
375461 * @param object $a
376462 * @param object $b
377463 * @return int
@@ -400,6 +486,7 @@ protected function CompareAlbum($a, $b)
400486 $ al = strtolower ($ a ->title );
401487 $ bl = strtolower ($ b ->title );
402488 }
489+
403490 if ($ al == $ bl ) {
404491 return 0 ;
405492 }
@@ -420,26 +507,25 @@ protected function CompareAlbum($a, $b)
420507 return ($ b ) ? +1 : -1 ;
421508 }
422509
510+ // Fix image orientation
423511 function image_fix_orientation (&$ image , &$ size , $ filename ) {
424- $ exif = exif_read_data ($ filename );
425-
426- if (!empty ($ exif ['Orientation ' ])) {
427- switch ($ exif ['Orientation ' ]) {
428- case 3 :
429- $ image = imagerotate ($ image , 180 , 0 );
430- break ;
431-
432- case 6 :
433- $ image = imagerotate ($ image , -90 , 0 );
434- list ($ size [0 ],$ size [1 ]) = array ($ size [1 ],$ size [0 ]);
435- break ;
436-
437- case 8 :
438- $ image = imagerotate ($ image , 90 , 0 );
439- list ($ size [0 ],$ size [1 ]) = array ($ size [1 ],$ size [0 ]);
440- break ;
441- }
512+
513+ $ exif = exif_read_data ($ filename );
514+ if ($ exif !== false && !empty ($ exif ['Orientation ' ])) {
515+ switch ($ exif ['Orientation ' ]) {
516+ case 3 :
517+ $ image = imagerotate ($ image , 180 , 0 );
518+ break ;
519+ case 6 :
520+ $ image = imagerotate ($ image , -90 , 0 );
521+ list ($ size [0 ],$ size [1 ]) = array ($ size [1 ],$ size [0 ]);
522+ break ;
523+ case 8 :
524+ $ image = imagerotate ($ image , 90 , 0 );
525+ list ($ size [0 ],$ size [1 ]) = array ($ size [1 ],$ size [0 ]);
526+ break ;
442527 }
528+ }
443529 }
444530
445531
@@ -593,9 +679,9 @@ protected function GetDominantColorsGIF( $img )
593679 return '' ;
594680 break ;
595681 }
596-
682+
597683 $ this ->image_fix_orientation ($ orgImage , $ size , $ img );
598-
684+
599685 $ width = $ size [0 ];
600686 $ height = $ size [1 ];
601687 $ thumb = imagecreate (3 , 3 );
@@ -700,8 +786,9 @@ protected function GenerateThumbnail2($baseFolder, $imagefilename, $thumbnailFil
700786 break ;
701787 }
702788 }
789+
703790 $ this ->image_fix_orientation ($ orgImage , $ size , $ baseFolder . $ imagefilename );
704-
791+
705792 $ width = $ size [0 ];
706793 $ height = $ size [1 ];
707794
@@ -881,7 +968,6 @@ protected function GetMetaData($filename, $isImage)
881968 }
882969 $ oneItem ->description = '' ;
883970 }
884-
885971 # Sort Using a Prefix from sortPrefixSeparator if present
886972 if (strpos ($ oneItem ->title , $ this ->config ['sortPrefixSeparator ' ]) > 0 ) {
887973 // split sort from title
@@ -899,8 +985,8 @@ protected function GetMetaData($filename, $isImage)
899985 // the title (=filename) is the ID
900986 $ oneItem ->ID = $ oneItem ->title ;
901987
902- // read meta data from external file (only images)
903- if ($ isImage ) {
988+ // read meta data from external file (images and albums )
989+ // if ($isImage) {
904990 if ( file_exists ( $ this ->data ->fullDir . '/ ' . $ filename . '.txt ' ) ) {
905991 $ myfile = fopen ($ this ->data ->fullDir . '/ ' . $ filename . '.txt ' , "r " ) or die ("Unable to open file! " );
906992 while (!feof ($ myfile )) {
@@ -917,7 +1003,7 @@ protected function GetMetaData($filename, $isImage)
9171003 fclose ($ myfile );
9181004 }
9191005
920- }
1006+ // }
9211007 return $ oneItem ;
9221008 }
9231009
@@ -1003,6 +1089,7 @@ protected function AlbumCountItems( $d )
10031089 if ($ filename != '. ' &&
10041090 $ filename != '.. ' &&
10051091 $ filename != '_thumbnails ' &&
1092+ preg_match ("/\.( " . $ this ->config ['fileExtensions ' ] . ")*$/i " , $ filename ) &&
10061093 strpos ($ filename , $ this ->config ['ignoreDetector ' ]) == false &&
10071094 !empty ($ filename ) )
10081095 {
@@ -1032,6 +1119,7 @@ protected function PrepareData($filename, $kind)
10321119 $ e = $ this ->GetMetaData ($ filename , true );
10331120 $ this ->currentItem ->title = $ e ->title ;
10341121 $ this ->currentItem ->description = $ e ->description ;
1122+ $ this ->currentItem ->tags = $ e ->tags ;
10351123 $ this ->currentItem ->sort = $ e ->sort ;
10361124 // $this->currentItem->src = rawurlencode($this->CustomEncode($this->config['contentFolder'] . $this->album . '/' . $filename));
10371125 $ this ->currentItem ->originalURL = rawurlencode ($ this ->CustomEncode ($ this ->config ['contentFolder ' ] . $ this ->album . '/ ' . $ filename ));
@@ -1087,4 +1175,4 @@ protected function PrepareData($filename, $kind)
10871175 }
10881176
10891177}
1090- ?>
1178+ ?>
0 commit comments