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