fec1af7761cf19904abd4c89afd0001e67ddaef6
[yaffs-website] / vendor / lsolesen / pel / examples / rename.php
1 #!/usr/bin/php
2 <?php
3
4 /**
5  * PEL: PHP Exif Library.
6  * A library with support for reading and
7  * writing all Exif headers in JPEG and TIFF images using PHP.
8  *
9  * Copyright (C) 2004, 2005, 2006 Martin Geisler.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program in the file COPYING; if not, write to the
23  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
24  * Boston, MA 02110-1301 USA
25  */
26
27 /*
28  * An example of how PEL can be used. The script will read the Exif
29  * timestamps in the files given, and rename the files based on that.
30  * Should there be several files with the same timestamp --- the
31  * resolution of the Exif timestamp is one second, so a burst of
32  * images will typically have identical timestamps --- then the next
33  * available time will be used.
34  */
35
36 /* a printf() variant that appends a newline to the output. */
37 function println($args)
38 {
39     $args = func_get_args();
40     $fmt = array_shift($args);
41     vprintf($fmt . "\n", $args);
42 }
43
44 /* Make PEL speak the users language, if it is available. */
45 setlocale(LC_ALL, '');
46
47 /* Load the required class definitions. */
48 require_once '../autoload.php';
49
50 use lsolesen\pel\PelDataWindow;
51 use lsolesen\pel\PelJpeg;
52 use lsolesen\pel\PelTiff;
53
54 $prog = array_shift($argv);
55 $error = false;
56
57 /* Accept the optional -d command line argument. */
58 if (isset($argv[0]) && $argv[0] == '-d') {
59     Pel::setDebug(true);
60     array_shift($argv);
61 }
62
63 /*
64  * Print usage information if there are no more command line
65  * arguments.
66  */
67 if (empty($argv)) {
68     println('Usage: %s [-d] <file> ...', $prog);
69     println('Optional arguments:');
70     println('  -d        turn debug output on.');
71     println('Mandatory arguments:');
72     println('  file ...  one or more file names.');
73     println();
74     println('The files will be renamed based on their Exif timestamp.');
75     exit(1);
76 }
77
78 /*
79  * We typically need lots of RAM to parse TIFF images since they tend
80  * to be big and uncompressed.
81  */
82 ini_set('memory_limit', '32M');
83
84 foreach ($argv as $file) {
85     println('Reading file "%s".', $file);
86     $data = new PelDataWindow(file_get_contents($file));
87
88     if (PelJpeg::isValid($data)) {
89         $jpeg = new PelJpeg();
90         $jpeg->load($data);
91         $app1 = $jpeg->getExif();
92         $tiff = $app1->getTiff();
93     } elseif (PelTiff::isValid($data)) {
94         $tiff = new PelTiff($data);
95     } else {
96         println('Unrecognized image format! Skipping.');
97         continue;
98     }
99
100     $ifd0 = $tiff->getIfd();
101     $entry = $ifd0->getEntry(PelTag::DATE_TIME);
102
103     if ($entry == null) {
104         println('Skipping %s because no DATE_TIME tag was found.', $file);
105         continue;
106     }
107
108     $time = $entry->getValue();
109
110     do {
111         $new = gmdate('Y:m:d-H:i:s', $time) . strchr($file, '.');
112         println('Trying file name %s', $new);
113         $time ++;
114     } while (file_exists($new));
115
116     rename($file, $new);
117 }