Mercurial > hg > mgallery
annotate mgtool.php @ 185:cb7019690a73
Separate medium image and thumbnail directories / handling.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 14 Mar 2018 15:21:42 +0200 |
parents | 087df916b6b4 |
children | a5ce6144fbf5 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/php |
2 <?php | |
3 // | |
4 // Yet Another Image Gallery | |
69
7777887bc515
Update header/copyright blurbs.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
5 // -- Commandline tool for creating / updating gallery |
7777887bc515
Update header/copyright blurbs.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
6 // Programmed and designed by Matti 'ccr' Hamalainen <ccr@tnsp.org> |
161 | 7 // (C) Copyright 2015-2018 Tecnic Software productions (TNSP) |
0 | 8 // |
9 require_once "mgallery.inc.php"; | |
10 | |
11 // | |
12 // Array for specifying what will be copied and converted | |
13 // from the image file's EXIF information tag(s). | |
14 // | |
45 | 15 $galExifConversions = |
16 [ | |
124
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
17 [ MG_STR, "caption" , "ImageDescription" ], |
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
18 [ MG_STR, "copyright" , "Copyright" ], |
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
19 [ MG_STR, "model" , "Model" ], |
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
20 [ MG_INT, "width" , ["COMPUTED", "Width"] ], |
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
21 [ MG_INT, "height" , ["COMPUTED", "Height"] ], |
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
22 [ MG_DVA, "fnumber" , "FNumber" ], |
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
23 [ MG_DVA, "exposure" , "ExposureTime" ], |
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
24 [ MG_INT, "iso" , "ISOSpeedRatings" ], |
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
25 [ MG_STR, "lensmodel" , "UndefinedTag:0xA434" ], |
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
26 [ MG_DVA, "focallength" , "FocalLength" ], |
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
27 [ MG_DATE, "datetime" , "DateTimeOriginal" ], |
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
28 [ MG_DATE, "datetime" , "DateTimeDigitized" ], |
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
29 [ MG_INT, "filesize" , "FileSize" ], |
45 | 30 ]; |
0 | 31 |
32 define("GCMD_UPDATE" , 1); | |
33 define("GCMD_RESCAN" , 2); | |
34 define("GCMD_CLEAN" , 3); | |
35 | |
36 define("GCLEAN_CACHES" , 0x01); | |
37 define("GCLEAN_THUMBNAILS", 0x02); | |
38 define("GCLEAN_ALL" , 0x0f); | |
39 | |
40 define("GUPD_MED_IMAGE" , 0x01); | |
41 define("GUPD_TN_IMAGE" , 0x02); | |
42 define("GUPD_IMAGES" , 0x0f); | |
43 define("GUPD_EXIF_INFO" , 0x10); | |
44 define("GUPD_CAPTION" , 0x20); | |
45 | |
46 | |
47 // | |
48 // Convert and scale image file function, for generating | |
49 // the intermediate size images and thumbnails. Uses the | |
50 // PHP ImageMagick bindings. | |
51 // | |
52 function mgConvertImage($inFilename, $outFilename, $outDim, $outFormat, $outQuality, $thumb) | |
53 { | |
54 // Create conversion entity | |
53
4c0a08b0ef65
Handle ImageMagick exceptions for loading input images.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
55 try |
4c0a08b0ef65
Handle ImageMagick exceptions for loading input images.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
56 { |
4c0a08b0ef65
Handle ImageMagick exceptions for loading input images.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
57 $img = new Imagick($inFilename); |
4c0a08b0ef65
Handle ImageMagick exceptions for loading input images.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
58 } |
4c0a08b0ef65
Handle ImageMagick exceptions for loading input images.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
59 catch (Exception $e) |
4c0a08b0ef65
Handle ImageMagick exceptions for loading input images.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
60 { |
4c0a08b0ef65
Handle ImageMagick exceptions for loading input images.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
61 return mgError("ImageMagick exception for file '".$inFilename."':\n".$e->getMessage()."\n"); |
4c0a08b0ef65
Handle ImageMagick exceptions for loading input images.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
62 } |
4c0a08b0ef65
Handle ImageMagick exceptions for loading input images.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
63 |
0 | 64 if ($img === FALSE) |
65 return mgError("ImageMagick could not digest the file '".$inFilename."'.\n"); | |
66 | |
51
549541f3e9c0
Possibly improve medium and thumbnail image generation by controlling Imagick components better.
Matti Hamalainen <ccr@tnsp.org>
parents:
46
diff
changeset
|
67 $profiles = $img->getImageProfiles("icc", true); |
549541f3e9c0
Possibly improve medium and thumbnail image generation by controlling Imagick components better.
Matti Hamalainen <ccr@tnsp.org>
parents:
46
diff
changeset
|
68 $img->setImageDepth(16); |
549541f3e9c0
Possibly improve medium and thumbnail image generation by controlling Imagick components better.
Matti Hamalainen <ccr@tnsp.org>
parents:
46
diff
changeset
|
69 $img->transformImageColorspace(Imagick::COLORSPACE_SRGB); |
549541f3e9c0
Possibly improve medium and thumbnail image generation by controlling Imagick components better.
Matti Hamalainen <ccr@tnsp.org>
parents:
46
diff
changeset
|
70 $img->setImageColorspace(Imagick::COLORSPACE_SRGB); |
549541f3e9c0
Possibly improve medium and thumbnail image generation by controlling Imagick components better.
Matti Hamalainen <ccr@tnsp.org>
parents:
46
diff
changeset
|
71 |
0 | 72 if ($outDim !== FALSE) |
73 { | |
74 // Get dimensions, setup background | |
75 $dim = $img->getImageGeometry(); | |
76 //$img->setImageBackgroundColor(imagick::COLOR_BLACK); | |
77 $img->setGravity(imagick::GRAVITY_CENTER); | |
78 | |
79 | |
80 if ($dim["width"] < $dim["height"]) | |
81 { | |
82 $stmp = $outDim[0]; | |
83 $outDim[0] = $outDim[1]; | |
84 $outDim[1] = $stmp; | |
85 } | |
86 | |
87 if ($dim["width"] != $outDim[0] || $dim["height"] != $outDim[1]) | |
88 { | |
89 $outDim[1] = ($dim["height"] * $outDim[0]) / $dim["width"]; | |
90 } | |
91 | |
92 // Act based on image size vs. desired size and $thumb mode | |
93 if ($thumb || $dim["width"] > $outDim[0] || $dim["height"] > $outDim[1]) | |
94 { | |
95 // Image is larger | |
1
a07447c02b13
Use Lanczos filter for scaling instead of quadratic. Better quality and
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
96 $img->resizeImage($outDim[0], $outDim[1], Imagick::FILTER_LANCZOS, 1); |
0 | 97 $img->setImageExtent($outDim[0], $outDim[1]); |
51
549541f3e9c0
Possibly improve medium and thumbnail image generation by controlling Imagick components better.
Matti Hamalainen <ccr@tnsp.org>
parents:
46
diff
changeset
|
98 // $img->normalizeImage(); |
0 | 99 $img->unsharpMaskImage(0, 0.5, 1, 0.05); |
100 } | |
101 if ($dim["width"] < $outDim[0] || $dim["height"] < $outDim[1]) | |
102 { | |
103 // Image is smaller than requested dimension(s)? | |
1
a07447c02b13
Use Lanczos filter for scaling instead of quadratic. Better quality and
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
104 $img->resizeImage($outDim[0], $outDim[1], Imagick::FILTER_LANCZOS, 1); |
0 | 105 $img->setImageExtent($outDim[0], $outDim[1]); |
51
549541f3e9c0
Possibly improve medium and thumbnail image generation by controlling Imagick components better.
Matti Hamalainen <ccr@tnsp.org>
parents:
46
diff
changeset
|
106 // $img->normalizeImage(); |
0 | 107 $img->unsharpMaskImage(0, 0.5, 1, 0.05); |
108 } | |
109 } | |
110 | |
51
549541f3e9c0
Possibly improve medium and thumbnail image generation by controlling Imagick components better.
Matti Hamalainen <ccr@tnsp.org>
parents:
46
diff
changeset
|
111 |
549541f3e9c0
Possibly improve medium and thumbnail image generation by controlling Imagick components better.
Matti Hamalainen <ccr@tnsp.org>
parents:
46
diff
changeset
|
112 $img->setImageDepth(8); |
0 | 113 $img->setFormat($outFormat); |
55
b1fa4fd593d1
Fix thumbnail/medium image JPEG quality handling to actually work. :S
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
114 $img->setImageCompression(Imagick::COMPRESSION_JPEG); |
b1fa4fd593d1
Fix thumbnail/medium image JPEG quality handling to actually work. :S
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
115 $img->setImageCompressionQuality($outQuality); |
80 | 116 |
51
549541f3e9c0
Possibly improve medium and thumbnail image generation by controlling Imagick components better.
Matti Hamalainen <ccr@tnsp.org>
parents:
46
diff
changeset
|
117 $img->stripImage(); |
549541f3e9c0
Possibly improve medium and thumbnail image generation by controlling Imagick components better.
Matti Hamalainen <ccr@tnsp.org>
parents:
46
diff
changeset
|
118 if (!empty($profiles)) |
549541f3e9c0
Possibly improve medium and thumbnail image generation by controlling Imagick components better.
Matti Hamalainen <ccr@tnsp.org>
parents:
46
diff
changeset
|
119 $img->profileImage("icc", $profiles["icc"]); |
0 | 120 |
121 $img->writeImage($outFilename); | |
122 $img->removeImage(); | |
123 return TRUE; | |
124 } | |
125 | |
126 | |
127 // | |
128 // Converts one value (mainly from EXIF tag information) | |
52 | 129 // by doing explicit type casting and special conversions. |
0 | 130 // |
131 function mgConvertExifData($val, $vtype) | |
132 { | |
133 switch ($vtype) | |
134 { | |
46
e58292065b01
Let arrays through mgConvertExifData() when the type is MG_STR without
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
135 case MG_STR: return is_array($val) ? $val : (string) $val; |
0 | 136 case MG_INT: return intval($val); |
137 case MG_BOOL: return intval($val); | |
138 case MG_DVA: | |
139 if (sscanf($val, "%d/%d", $v1, $v2) == 2) | |
140 { | |
141 if ($v1 < $v2) | |
142 return $val; | |
143 else | |
144 return sprintf("%1.1f", $v1 / $v2); | |
145 } | |
146 else | |
147 return $val; | |
148 | |
124
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
149 case MG_DATE: |
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
150 return date_timestamp_get(date_create_from_format("Y:m:d H:i:s", $val)); |
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
151 |
0 | 152 default: |
153 return $val; | |
154 } | |
155 } | |
156 | |
157 | |
158 // | |
159 // Conditionally copies one "field" from an associated array/hash to another. | |
160 // If destination is already SET, nothing will be done. If source does | |
161 // not exist (e.g. one or more of the keys do not exist in source array), | |
162 // a default value will be used, if provided. | |
163 // Source may have multi-depth keys, destination has one key. | |
164 // | |
165 function mgCopyEntryData(&$dst, $src, $vtype, $dkey, $skeys, $default = NULL) | |
166 { | |
167 // Is destination already set? | |
168 if (isset($dst[$dkey])) | |
169 return FALSE; | |
170 | |
171 // If input key is not array, change it into one | |
172 if (!is_array($skeys)) | |
173 $skeys = array($skeys); | |
174 | |
175 // Traverse input array by using consequent keys | |
176 $tmp = &$src; | |
177 foreach ($skeys as $skey) | |
178 { | |
179 if (!array_key_exists($skey, $tmp)) | |
180 { | |
181 // Key didn't exist, try for default | |
182 if ($default !== NULL) | |
183 $dst[$dkey] = $default; | |
184 return FALSE; | |
185 } | |
186 else | |
187 $tmp = &$tmp[$skey]; | |
188 } | |
189 | |
190 // Optionally convert the input value | |
191 $dst[$dkey] = mgConvertExifData($tmp, $vtype); | |
192 return TRUE; | |
193 } | |
194 | |
195 | |
196 // | |
197 // Attempt to get gallery album data from various sources. | |
198 // | |
199 function mgGetAlbumData($galBasePath, $galPath) | |
200 { | |
201 // Check path permissions | |
60
ef9f17280372
Use modern PHP array definition in some places.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
202 $galData = []; |
0 | 203 if (is_readable($galPath)) |
204 { | |
205 // First, try to read gallery/album info file | |
206 $filename = mgGetPath($galPath, "info_file"); | |
207 if ($filename !== FALSE && file_exists($filename)) | |
208 { | |
209 mgDebug("Reading INFOFILE: ".$filename."\n"); | |
210 if (($galData = parse_ini_file($filename, FALSE)) === FALSE) | |
60
ef9f17280372
Use modern PHP array definition in some places.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
211 $galData = []; |
0 | 212 } |
213 | |
214 // Read header file, if any, and we don't have "header" field set yet | |
215 $filename = mgGetPath($galPath, "header_file"); | |
216 if ($filename !== FALSE && file_exists($filename) && | |
217 !isset($galData["header"])) | |
218 { | |
219 mgDebug("Reading HEADERFILE: ".$filename."\n"); | |
220 $galData["header"] = file_get_contents($filename); | |
221 } | |
222 | |
223 // Check for alternate key/values for album title/caption | |
224 if (isset($galData["title"]) && !isset($galData["caption"])) | |
225 { | |
226 $galData["caption"] = $galData["title"]; | |
227 unset($galData["title"]); | |
228 } | |
229 } | |
230 else | |
231 $galData["hide"] = TRUE; | |
232 | |
233 // If caption is not set, use last path component for | |
234 // a fallback value in case we don't discover proper title | |
235 // from other sources we can't check here yet. | |
236 $path = explode("/", $galPath); | |
237 $galData["fallback_caption"] = ucfirst(str_replace("_", " ", end($path))); | |
238 | |
239 // Last, store the current gallery path | |
240 $len = strlen($galBasePath); | |
241 if ($len < strlen($galPath) && substr($galPath, 0, $len) == $galBasePath) | |
242 $galData["path"] = substr($galPath, $len); | |
243 else | |
244 $galData["path"] = ""; | |
245 | |
246 return $galData; | |
247 } | |
248 | |
249 | |
165 | 250 // |
251 // Parse a simple image captions file, with format of one entry per line. | |
252 // Lines starting with "# " are comments (note the whitespace), empty lines ignored. | |
253 // First continuous non-whitespace text is file/dirname, followed | |
254 // by caption text, which is separate with whitespace from the filename. | |
255 // Filenames starting with # will be made hidden entries. | |
256 // Filenames starging with % will hide contents. | |
257 // | |
0 | 258 function mgReadCaptionsFile($galBasePath, $galPath) |
259 { | |
60
ef9f17280372
Use modern PHP array definition in some places.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
260 $captions = []; |
0 | 261 $filename = mgGetPath($galPath, "captions_file"); |
262 if ($filename === FALSE || ($fp = @fopen($filename, "rb")) === FALSE) | |
263 return $captions; | |
264 | |
265 mgDebug("Reading CAPTIONS: ".$filename."\n"); | |
266 | |
267 // Read and parse data | |
268 while (!feof($fp)) | |
269 { | |
270 $str = trim(fgets($fp)); | |
271 // Ignore comments and empty lines | |
272 if ($str != "#" && $str != "") | |
273 { | |
11
0b097770061d
Improve content hiding functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
274 if (preg_match("/^([#%]?)\s*(\S+?)\s+(.+)$/", $str, $m)) |
45 | 275 $captions[$m[2]] = ["caption" => $m[3], "hide" => ($m[1] == "#"), "hide_contents" => ($m[1] == "%"), "used" => FALSE]; |
0 | 276 else |
11
0b097770061d
Improve content hiding functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
277 if (preg_match("/^([#%]?)\s*(\S+?)$/", $str, $m)) |
45 | 278 $captions[$m[2]] = ["hide" => ($m[1] == "#"), "hide_contents" => ($m[1] == "%"), "used" => FALSE]; |
0 | 279 } |
280 } | |
281 | |
282 fclose($fp); | |
283 return $captions; | |
284 } | |
285 | |
286 | |
165 | 287 // |
288 // Create directory with specified permissions | |
289 // | |
0 | 290 function mgMakeDir($path, $perm) |
291 { | |
292 if (!file_exists($path)) | |
293 { | |
294 if (mkdir($path, $perm, TRUE) === false) | |
295 return mgError("Could not create directory '".$path."'\n"); | |
296 } | |
297 return TRUE; | |
298 } | |
299 | |
300 | |
165 | 301 // |
302 // Print a simple yes/no prompt with given message | |
303 // and default value. | |
304 // | |
0 | 305 function mgYesNoPrompt($msg, $default = FALSE) |
306 { | |
307 echo $msg." [".($default ? "Y/n" : "y/N")."]? "; | |
308 $sprompt = strtolower(trim(fgets(STDIN))); | |
309 | |
166
d8387b48b08d
Improve yes/no prompt check.
Matti Hamalainen <ccr@tnsp.org>
parents:
165
diff
changeset
|
310 if ($sprompt == "") |
d8387b48b08d
Improve yes/no prompt check.
Matti Hamalainen <ccr@tnsp.org>
parents:
165
diff
changeset
|
311 return $default; |
0 | 312 else |
165 | 313 return $sprompt[0] == 'y'; |
0 | 314 } |
315 | |
316 | |
165 | 317 // |
182 | 318 // Delete given directory OR file, recursively |
165 | 319 // |
183 | 320 function mgDelete($path, $showFiles) |
0 | 321 { |
56
7384504d7b52
Do not prompt for deletion in cleanup if there are no files/directories to delete.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
322 global $flagDoDelete, $countDoDelete; |
0 | 323 if (is_dir($path)) |
324 { | |
325 if (($dirHandle = @opendir($path)) === FALSE) | |
326 return mgError("Could not read directory '".$path."'.\n"); | |
327 | |
328 while (($dirFile = @readdir($dirHandle)) !== FALSE) | |
329 { | |
330 if ($dirFile != "." && $dirFile != "..") | |
183 | 331 mgDelete($path."/".$dirFile, $showFiles); |
0 | 332 } |
183 | 333 |
0 | 334 closedir($dirHandle); |
335 | |
336 echo " - ".$path." [DIR]\n"; | |
62
3f60a5f5b4b3
Actually fix checking for files to be deleted to work.
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
337 $countDoDelete++; |
0 | 338 if ($flagDoDelete) |
339 rmdir($path); | |
340 } | |
341 else | |
54
624c50e1b52d
Fix 'mgtool clean' to not attempt deleting files that do not exist.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
342 if (file_exists($path)) |
0 | 343 { |
183 | 344 if ($showFiles) |
0 | 345 echo " - ".$path."\n"; |
56
7384504d7b52
Do not prompt for deletion in cleanup if there are no files/directories to delete.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
346 |
62
3f60a5f5b4b3
Actually fix checking for files to be deleted to work.
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
347 $countDoDelete++; |
0 | 348 if ($flagDoDelete) |
349 unlink($path); | |
350 } | |
351 } | |
352 | |
353 | |
165 | 354 // |
355 // Check if we have received the quit signal | |
356 // and if yes, quit cleanly now. | |
357 // | |
0 | 358 function mgCheckQuit($now = FALSE) |
359 { | |
360 global $flagQuit; | |
361 | |
362 // Dispatch pending signals | |
363 pcntl_signal_dispatch(); | |
364 | |
365 // Check result | |
366 if ($now && $flagQuit) | |
367 mgFatal("Quitting.\n"); | |
368 | |
369 return $flagQuit; | |
370 } | |
371 | |
372 | |
373 function mgNeedUpdate($entry, $field, $cvalue) | |
374 { | |
375 if (!array_key_exists($field, $entry)) | |
376 return TRUE; | |
80 | 377 |
0 | 378 return ($entry[$field] < $cvalue); |
379 } | |
380 | |
381 | |
30
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
382 function mgSortFunc($a, $b) |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
383 { |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
384 if (isset($a["datetime"]) && isset($b["datetime"])) |
124
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
385 return $b["datetime"] - $a["datetime"]; |
30
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
386 else |
108
417cdd9f8864
Use image file base name for sorting if timestamps are not available.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
387 if (isset($a["base"]) && isset($b["base"])) |
417cdd9f8864
Use image file base name for sorting if timestamps are not available.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
388 return strcmp($b["base"], $a["base"]); |
417cdd9f8864
Use image file base name for sorting if timestamps are not available.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
389 else |
30
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
390 return 0; |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
391 } |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
392 |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
393 |
16
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
394 function mgWriteGalleryCache($cacheFilename, &$gallery, &$entries, &$parentEntry) |
15
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
395 { |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
396 // Store gallery cache for this directory |
60
ef9f17280372
Use modern PHP array definition in some places.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
397 $images = []; |
ef9f17280372
Use modern PHP array definition in some places.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
398 $albums = []; |
61
ee82f7c8d44c
Do not include hidden entries in the entry list.
Matti Hamalainen <ccr@tnsp.org>
parents:
60
diff
changeset
|
399 $output = []; |
60
ef9f17280372
Use modern PHP array definition in some places.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
400 |
64
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
401 // If we are hiding contents of an album, generate no data |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
402 if (!$parentEntry["hide_contents"]) |
15
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
403 { |
64
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
404 foreach ($entries as $ename => &$edata) |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
405 { |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
406 if ($edata["hide"]) |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
407 continue; |
15
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
408 |
64
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
409 unset($edata["hide"]); |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
410 if ($edata["type"] == 0) |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
411 $images[$ename] = &$edata; |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
412 else |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
413 $albums[$ename] = &$edata; |
61
ee82f7c8d44c
Do not include hidden entries in the entry list.
Matti Hamalainen <ccr@tnsp.org>
parents:
60
diff
changeset
|
414 |
64
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
415 $output[$ename] = &$edata; |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
416 } |
30
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
417 |
64
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
418 uasort($images, "mgSortFunc"); |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
419 krsort($albums); |
15
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
420 |
64
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
421 // Choose gallery album image |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
422 if (count($images) > 0) |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
423 { |
79
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
424 if (isset($gallery["albumpic"]) && |
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
425 isset($images[$gallery["albumpic"]])) |
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
426 { |
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
427 $parentEntry["image"] = $gallery["albumpic"]; |
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
428 } |
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
429 else |
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
430 { |
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
431 end($images); |
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
432 $parentEntry["image"] = key($images); |
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
433 } |
64
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
434 } |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
435 else |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
436 { |
79
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
437 foreach ($albums as $aid => &$adata) |
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
438 if (isset($adata["image"])) |
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
439 { |
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
440 $parentEntry["image"] = &$adata; |
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
441 break; |
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
442 } |
64
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
443 } |
35
985596db0f01
Implement recursive depth album covers.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
444 } |
16
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
445 |
15
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
446 $str = |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
447 "<?\n". |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
448 "\$galData = ".var_export($gallery, TRUE).";\n". |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
449 "\$galAlbumsIndex = ".var_export(array_keys($albums), TRUE).";\n". |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
450 "\$galImagesIndex = ".var_export(array_keys($images), TRUE).";\n". |
61
ee82f7c8d44c
Do not include hidden entries in the entry list.
Matti Hamalainen <ccr@tnsp.org>
parents:
60
diff
changeset
|
451 "\$galEntries = ".var_export($output, TRUE).";\n". |
15
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
452 "?>"; |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
453 |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
454 if (@file_put_contents($cacheFilename, $str, LOCK_EX) === FALSE) |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
455 return mgError("Error writing '".$cacheFilename."'\n"); |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
456 |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
457 return TRUE; |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
458 } |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
459 |
16
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
460 |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
461 function mgHandleDirectory($mode, $basepath, $path, &$parentData, &$parentEntry, $writeMode, $startAt) |
0 | 462 { |
185
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
463 global $galExifConversions, $galTNPath, $galMedPath, $galCleanFlags; |
0 | 464 |
465 // Get cache file path | |
466 if (($cacheFilename = mgGetPath($path, "cache_file")) === FALSE) | |
467 return mgError("Cache filename / path not set.\n"); | |
468 | |
469 mgCheckQuit(TRUE); | |
470 | |
471 // Read directory contents | |
60
ef9f17280372
Use modern PHP array definition in some places.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
472 $entries = []; |
0 | 473 if (($dirHandle = @opendir($path)) === FALSE) |
474 return mgError("Could not read directory '".$path."'.\n"); | |
475 | |
476 while (($dirFile = @readdir($dirHandle)) !== FALSE) | |
477 { | |
478 $realFile = $path."/".$dirFile; | |
479 if (is_dir($realFile)) | |
480 { | |
185
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
481 if ($dirFile[0] != "." && $dirFile != $galTNPath && $dirFile != $galMedPath) |
63
1c32ece9acde
Some residual array() => [] conversions.
Matti Hamalainen <ccr@tnsp.org>
parents:
62
diff
changeset
|
482 $entries[$dirFile] = ["type" => 1, "base" => $dirFile, "ext" => "", "mtime" => filemtime($realFile)]; |
0 | 483 } |
484 else | |
6
405a02586fc2
Fix handling of image files that have spaces in filename (regexp used \S,
Matti Hamalainen <ccr@tnsp.org>
parents:
1
diff
changeset
|
485 if (preg_match("/^([^\/]+)(".mgGetSetting("format_exts").")$/i", $dirFile, $dirMatch)) |
63
1c32ece9acde
Some residual array() => [] conversions.
Matti Hamalainen <ccr@tnsp.org>
parents:
62
diff
changeset
|
486 $entries[$dirFile] = ["type" => 0, "base" => $dirMatch[1], "ext" => $dirMatch[2], "mtime" => filemtime($realFile), "hide" => false]; |
0 | 487 } |
488 closedir($dirHandle); | |
489 | |
490 mgCheckQuit(); | |
491 | |
492 // Cleanup mode | |
493 if ($mode == GCMD_CLEAN) | |
494 { | |
60
ef9f17280372
Use modern PHP array definition in some places.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
495 $gallery = []; |
0 | 496 |
497 if ($writeMode) | |
498 { | |
499 if ($galCleanFlags & GCLEAN_CACHES) | |
183 | 500 mgDelete($cacheFilename, TRUE); |
0 | 501 |
502 if ($galCleanFlags & GCLEAN_THUMBNAILS) | |
183 | 503 mgDelete($path."/".$galTNPath, FALSE); |
185
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
504 |
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
505 if ($galCleanFlags & GCLEAN_THUMBNAILS) |
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
506 mgDelete($path."/".$galMedPath, FALSE); |
0 | 507 } |
508 } | |
509 else | |
510 // Update modes | |
511 if ($mode == GCMD_UPDATE || $mode == GCMD_RESCAN) | |
512 { | |
513 // Load current cache file, if it exists | |
60
ef9f17280372
Use modern PHP array definition in some places.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
514 $galEntries = []; |
0 | 515 $cacheTime = -1; |
516 if ($mode == GCMD_UPDATE && file_exists($cacheFilename)) | |
517 { | |
518 $cacheTime = filemtime($cacheFilename); | |
519 @include $cacheFilename; | |
520 } | |
521 | |
522 // Read caption data | |
523 $captions = mgReadCaptionsFile($basepath, $path); | |
524 $gallery = mgGetAlbumData($basepath, $path); | |
525 if ($parentData !== NULL && $parentEntry !== NULL) | |
526 { | |
16
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
527 $gallery["parent"] = &$parentData; |
0 | 528 mgCopyEntryData($gallery, $parentEntry, MG_STR, "caption", "caption"); |
529 } | |
530 | |
531 // Start actual processing | |
532 $nentries = count($entries); | |
533 $nentry = 0; | |
534 echo $path." .. "; | |
535 foreach ($entries as $ename => &$edata) | |
536 { | |
537 printf("\r%s (%1.1f%%) ..", $path, ($nentry * 100.0) / $nentries); | |
538 | |
539 $nentry++; | |
540 $efilename = $path."/".$ename; | |
541 | |
542 if (array_key_exists($ename, $galEntries)) | |
543 $galEntry = &$galEntries[$ename]; | |
544 else | |
63
1c32ece9acde
Some residual array() => [] conversions.
Matti Hamalainen <ccr@tnsp.org>
parents:
62
diff
changeset
|
545 $galEntry = []; |
0 | 546 |
57
29bb365e3ee2
Be more responsive when processing images, check for signals after each
Matti Hamalainen <ccr@tnsp.org>
parents:
56
diff
changeset
|
547 mgCheckQuit(TRUE); |
0 | 548 |
549 // Update with captions file data, if any | |
550 if (array_key_exists($ename, $captions)) | |
551 { | |
552 foreach ($captions[$ename] as $ckey => $cval) | |
553 $edata[$ckey] = $cval; | |
554 } | |
555 | |
556 // Handle entry based on type | |
557 if ($edata["type"] == 0) | |
558 { | |
559 $updFlags = 0; | |
560 $tnPath = $path."/".$galTNPath; | |
185
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
561 $medPath = $path."/".$galMedPath; |
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
562 $medFilename = $medPath."/".$ename; |
0 | 563 $tnFilename = $tnPath."/".$ename; |
564 $capFilename = $path."/".$edata["base"].".txt"; | |
565 | |
566 // Check what we need to update .. | |
567 if (!file_exists($medFilename) || filemtime($medFilename) < $edata["mtime"]) | |
568 $updFlags |= GUPD_MED_IMAGE; | |
569 | |
570 if (!file_exists($tnFilename) || filemtime($tnFilename) < $edata["mtime"]) | |
571 $updFlags |= GUPD_TN_IMAGE; | |
572 | |
573 if (mgNeedUpdate($galEntry, "mtime", $edata["mtime"])) | |
574 $updFlags |= GUPD_EXIF_INFO; | |
575 | |
576 if (file_exists($capFilename) && | |
577 mgNeedUpdate($galEntry, "mtime", filemtime($capFilename))) | |
578 $updFlags |= GUPD_CAPTION; | |
579 | |
580 // Check for EXIF info | |
581 if (($updFlags & GUPD_EXIF_INFO) && | |
582 ($exif = @exif_read_data($efilename)) !== FALSE) | |
583 { | |
584 echo "%"; | |
585 foreach ($galExifConversions as $conv) | |
586 mgCopyEntryData($edata, $exif, $conv[0], $conv[1], $conv[2]); | |
587 } | |
588 else | |
589 { | |
590 // Copy old data that is not yet in new | |
591 echo "*"; | |
592 foreach ($galEntry as $okey => $odata) | |
593 { | |
594 if (!array_key_exists($okey, $edata)) | |
595 $edata[$okey] = $odata; | |
596 } | |
597 } | |
598 | |
599 // Generate thumbnails, etc. | |
600 if ($updFlags & GUPD_IMAGES) | |
601 { | |
602 mgMakeDir($tnPath, 0755); | |
185
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
603 mgMakeDir($medPath, 0755); |
0 | 604 |
605 if ($updFlags & GUPD_MED_IMAGE) | |
606 { | |
607 echo "1"; | |
608 mgConvertImage($efilename, $medFilename, | |
63
1c32ece9acde
Some residual array() => [] conversions.
Matti Hamalainen <ccr@tnsp.org>
parents:
62
diff
changeset
|
609 [mgGetSetting("med_width"), mgGetSetting("med_height")], |
0 | 610 "JPEG", mgGetSetting("med_quality"), TRUE); |
611 } | |
612 | |
613 if ($updFlags & GUPD_TN_IMAGE) | |
614 { | |
615 echo "2"; | |
616 mgConvertImage($efilename, $tnFilename, | |
63
1c32ece9acde
Some residual array() => [] conversions.
Matti Hamalainen <ccr@tnsp.org>
parents:
62
diff
changeset
|
617 [mgGetSetting("tn_width"), mgGetSetting("tn_height")], |
0 | 618 "JPEG", mgGetSetting("tn_quality"), TRUE); |
619 } | |
620 } | |
621 | |
622 // Check for .txt caption file | |
623 if ($updFlags & GUPD_CAPTION) | |
624 { | |
625 echo "?"; | |
626 if (($tmpData = @file_get_contents($capFilename)) !== FALSE) | |
627 $edata["caption"] = $tmpData; | |
628 } | |
629 } | |
630 else | |
631 if ($edata["type"] == 1) | |
632 { | |
10 | 633 // Set some of the album data here |
0 | 634 $tmp = mgGetAlbumData($basepath, $efilename); |
635 mgCopyEntryData($edata, $tmp, MG_STR, "caption", "caption"); | |
636 mgCopyEntryData($edata, $tmp, MG_STR, "caption", "fallback_caption"); | |
637 mgCopyEntryData($edata, $tmp, MG_STR, "caption", "title"); | |
638 mgCopyEntryData($edata, $tmp, MG_BOOL, "hide", "hide", FALSE); | |
11
0b097770061d
Improve content hiding functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
639 mgCopyEntryData($edata, $tmp, MG_BOOL, "hide_contents", "hide_contents", FALSE); |
0 | 640 } |
641 } | |
642 | |
643 echo "\r".$path." ..... DONE\n"; | |
644 | |
645 mgCheckQuit(TRUE); | |
646 } | |
647 else | |
648 mgFatal("Invalid work mode '".$mode."'.\n"); | |
649 | |
650 mgCheckQuit(TRUE); | |
651 | |
652 // Recurse to subdirectories | |
9
232086da1670
Fix generating of sub-album/dirs names without gallery.info to specify
Matti Hamalainen <ccr@tnsp.org>
parents:
6
diff
changeset
|
653 foreach ($entries as $ename => &$edata) |
0 | 654 if ($edata["type"] == 1) |
655 { | |
656 $epath = $path."/".$ename."/"; | |
657 $newWriteMode = ($writeMode === FALSE && $epath == $startAt) || $writeMode; | |
658 | |
659 if (!mgHandleDirectory($mode, $basepath, $epath, $gallery, $edata, $newWriteMode, $startAt)) | |
660 return FALSE; | |
661 } | |
662 | |
16
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
663 // Finish update modes |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
664 if ($mode == GCMD_UPDATE || $mode == GCMD_RESCAN) |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
665 { |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
666 // Store gallery cache for this directory |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
667 if ($writeMode && !mgWriteGalleryCache($cacheFilename, $gallery, $entries, $parentEntry)) |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
668 return FALSE; |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
669 } |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
670 |
0 | 671 mgCheckQuit(TRUE); |
672 | |
673 return TRUE; | |
674 } | |
675 | |
676 | |
677 function mgSigHandler($signo) | |
678 { | |
679 global $flagQuit; | |
680 switch ($signo) | |
681 { | |
682 case SIGTERM: | |
683 mgFatal("Received SIGTERM.\n"); | |
684 break; | |
685 | |
686 case SIGQUIT: | |
687 case SIGINT: | |
688 $flagQuit = TRUE; | |
689 break; | |
690 | |
691 } | |
692 } | |
693 | |
694 | |
185
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
695 function mgCheckMPath($path, $id) |
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
696 { |
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
697 if ($path."/" != mgGetSetting($id)) |
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
698 mgError("Invalid ".$id." '".mgGetSetting($id)."', using '".$path."'.\n"); |
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
699 |
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
700 if (strpos($path, "/") !== FALSE || $path == "") |
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
701 mgFatal("Invalid ".$id." '".$path."'.\n"); |
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
702 } |
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
703 |
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
704 |
0 | 705 function mgProcessGalleries($cmd, $path) |
706 { | |
185
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
707 global $galTNPath, $galMedPath; |
0 | 708 |
709 // Check validity of some settings | |
710 $galPath = mgGetSetting("base_path"); | |
711 $galTNPath = mgCleanPath(TRUE, mgGetSetting("tn_path")); | |
185
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
712 $galMedPath = mgCleanPath(TRUE, mgGetSetting("med_path")); |
0 | 713 |
185
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
714 mgCheckMPath($galTNPath, "tn_path"); |
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
715 mgCheckMPath($galMedPath, "med_path"); |
0 | 716 |
717 $parentData = $parentEntry = NULL; | |
718 $writeMode = TRUE; | |
719 $startAt = NULL; | |
720 | |
721 // Check for path argument | |
722 if ($path !== FALSE) | |
723 { | |
724 // Check the given path, needs to be "under" the gallery path | |
725 $cmp = mgCleanPath(TRUE, mgRealPath($galPath))."/"; | |
726 $tmp = mgCleanPath(TRUE, mgRealPath($path))."/"; | |
727 if (substr($tmp, 0, strlen($cmp)) != $cmp) | |
728 mgFatal("Path '".$path."' ('".$tmp."') does not reside under '".$galPath."' ('".$cmp."')!\n"); | |
729 | |
730 // Check if we need to bootstrap | |
731 if ($cmp != $tmp) | |
732 { | |
733 $bpath = mgCleanPath(TRUE, mgRealPath($tmp."../")); | |
734 | |
735 if ($cmd != GCMD_CLEAN) | |
736 { | |
737 $cacheFile = mgGetPath($bpath, "cache_file"); | |
738 if (!file_exists($cacheFile)) | |
739 mgFatal("Can't start working from '".$path."', parent '".$cacheFile."' does not exist!\n"); | |
740 | |
741 @include($cacheFile); | |
742 if (!isset($galEntries) || !isset($galData)) | |
743 mgFatal("Cache file '".$cacheFile."' is broken or stale.\n"); | |
744 } | |
745 | |
746 $writeMode = FALSE; | |
747 $startAt = $tmp; | |
748 $path = $bpath; | |
749 | |
750 echo "Starting: '".$startAt."' inside '".$path."'.\n"; | |
751 } | |
752 else | |
753 $path = $tmp; | |
754 } | |
755 else | |
756 $path = $galPath; | |
757 | |
758 // Start working | |
759 echo "Gallery path: '".$galPath."', starting at '".$path."' ...\n"; | |
760 mgHandleDirectory($cmd, $galPath, $path, $parentData, $parentEntry, $writeMode, $startAt); | |
761 } | |
762 | |
763 | |
764 function mgShowCopyright() | |
765 { | |
766 global $mgProgVersion, $mgProgCopyright, | |
767 $mgProgInfo, $mgProgEmail; | |
768 | |
769 echo | |
184 | 770 "MGTool ".$mgProgVersion." - MGallery management tool\n". |
771 $mgProgInfo." ".$mgProgEmail."\n". | |
772 "(C) Copyright ".$mgProgCopyright."\n"; | |
0 | 773 } |
774 | |
775 | |
776 function mgShowHelp() | |
777 { | |
778 global $argv; | |
779 echo | |
780 "Usage: ".basename($argv[0])." <command> [arguments]\n". | |
781 "\n". | |
782 " --help - Show this help.\n". | |
783 " --version - Show version information.\n". | |
784 "\n". | |
785 " update [path]\n". | |
786 " Update directories under <path> or all gallery dirs.\n". | |
787 " Conditionally scans dirs for new or changed images and\n". | |
788 " updates cache files as needed.\n". | |
789 "\n". | |
790 " rescan [path]\n". | |
791 " Like 'update', but forces all cache files to be regenerated\n". | |
792 " and EXIF/caption/etc information to be re-scanned even\n". | |
793 " if the file timestamps do not justify re-scanning.\n". | |
794 "\n". | |
795 " clean <all|caches|thumbnails> [path]\n". | |
796 " Delete all generated files or selectively cache files or\n". | |
797 " everything inside thumbnail directories.\n". | |
798 "\n". | |
159
b010c763a1fc
Document mgtool 'dump' command.
Matti Hamalainen <ccr@tnsp.org>
parents:
130
diff
changeset
|
799 " config|dump\n". |
b010c763a1fc
Document mgtool 'dump' command.
Matti Hamalainen <ccr@tnsp.org>
parents:
130
diff
changeset
|
800 " Display configuration values (with extra information) or\n". |
b010c763a1fc
Document mgtool 'dump' command.
Matti Hamalainen <ccr@tnsp.org>
parents:
130
diff
changeset
|
801 " \"dump\" current configuration as is.\n". |
0 | 802 "\n"; |
803 } | |
804 | |
805 | |
806 // | |
807 // Main code starts | |
808 // | |
809 if (php_sapi_name() != "cli" || !empty($_SERVER["REMOTE_ADDR"])) | |
810 { | |
811 header("Status: 404 Not Found"); | |
812 die(); | |
813 } | |
814 | |
815 pcntl_signal(SIGTERM, "mgSigHandler"); | |
816 pcntl_signal(SIGHUP, "mgSigHandler"); | |
817 pcntl_signal(SIGQUIT, "mgSigHandler"); | |
818 pcntl_signal(SIGINT, "mgSigHandler"); | |
819 | |
130
f825d644cf7a
Implement new config setting "timezone" for specifying the timezone used for date/timestamp interpretation.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
820 if (mgReadSettings() === FALSE) |
f825d644cf7a
Implement new config setting "timezone" for specifying the timezone used for date/timestamp interpretation.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
821 die("MGallery is not configured, failed to find a configuration file.\n"); |
f825d644cf7a
Implement new config setting "timezone" for specifying the timezone used for date/timestamp interpretation.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
822 |
f825d644cf7a
Implement new config setting "timezone" for specifying the timezone used for date/timestamp interpretation.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
823 if (($pageTimeZone = mgGetSetting("timezone")) !== NULL) |
f825d644cf7a
Implement new config setting "timezone" for specifying the timezone used for date/timestamp interpretation.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
824 date_default_timezone_set($pageTimeZone); |
0 | 825 |
826 $cmd = mgCArgLC(1); | |
827 switch ($cmd) | |
828 { | |
829 case "--version": | |
830 case "version": | |
831 case "ver": | |
832 mgShowCopyright(); | |
833 break; | |
834 | |
835 case FALSE: | |
836 // No arguments | |
837 mgError("Nothing to do. Showing help:\n"); | |
838 | |
839 case "--help": | |
840 case "help": | |
841 if ($cmd !== FALSE) | |
842 mgShowCopyright(); | |
843 mgShowHelp(); | |
844 break; | |
845 | |
846 case "update": case "up": case "upd": case "upda": | |
847 mgProcessGalleries(GCMD_UPDATE, mgCArg(2)); | |
848 break; | |
849 | |
850 case "rescan": case "re": case "res": | |
851 mgProcessGalleries(GCMD_RESCAN, mgCArg(2)); | |
852 break; | |
853 | |
854 case "clean": case "cl": case "cle": | |
855 $cmode = mgCArgLC(2, 2); | |
856 switch ($cmode) | |
857 { | |
858 case "al": $galCleanFlags = GCLEAN_ALL; break; | |
859 case "ca": $galCleanFlags = GCLEAN_CACHES; break; | |
860 case "th": $galCleanFlags = GCLEAN_THUMBNAILS; break; | |
861 case FALSE: | |
862 mgFatal("Cleaning requires a mode argument.\n"); | |
863 | |
864 default: | |
865 mgFatal("Invalid clean mode '".mgCArg(2)."'.\n"); | |
866 } | |
56
7384504d7b52
Do not prompt for deletion in cleanup if there are no files/directories to delete.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
867 $countDoDelete = 0; |
0 | 868 $flagDoDelete = FALSE; |
869 mgProcessGalleries(GCMD_CLEAN, mgCArg(3)); | |
870 echo "--\n"; | |
56
7384504d7b52
Do not prompt for deletion in cleanup if there are no files/directories to delete.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
871 if ($countDoDelete == 0) |
7384504d7b52
Do not prompt for deletion in cleanup if there are no files/directories to delete.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
872 { |
7384504d7b52
Do not prompt for deletion in cleanup if there are no files/directories to delete.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
873 echo "Nothing to clean!\n"; |
7384504d7b52
Do not prompt for deletion in cleanup if there are no files/directories to delete.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
874 } |
7384504d7b52
Do not prompt for deletion in cleanup if there are no files/directories to delete.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
875 else |
0 | 876 if (mgYesNoPrompt("Really delete the above files and directories?")) |
877 { | |
25
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
878 echo "Deleting ...\n"; |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
879 $flagDoDelete = TRUE; |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
880 mgProcessGalleries(GCMD_CLEAN, mgCArg(3)); |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
881 } |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
882 else |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
883 { |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
884 echo "Okay, canceling operation.\n"; |
0 | 885 } |
886 break; | |
887 | |
888 case "config": | |
889 case "dump": | |
890 | |
891 foreach ($mgDefaults as $key => $dval) | |
892 { | |
893 $sval = mgGetSetting($key); | |
26 | 894 if ($cmd == "dump") |
895 { | |
114
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
896 printf("%-20s = %s\n", $key, mgGetDValStr($dval, $sval)); |
26 | 897 } |
898 else | |
899 { | |
900 printf("%-20s = %s%s\n", | |
901 $key, | |
114
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
902 mgGetDValStr($dval, $sval), |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
903 ($dval[1] !== NULL && $sval !== $dval[1]) ? " (default: ".mgGetDValStr($dval, $dval[1]).")" : ""); |
26 | 904 } |
0 | 905 } |
906 break; | |
907 | |
908 default: | |
909 mgError("Unknown option/command '".$cmd."'.\n"); | |
910 break; | |
911 } | |
912 | |
913 ?> |