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