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.
9 * Copyright (C) 2004, 2005, 2006 Martin Geisler.
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.
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.
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
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.
36 /* a printf() variant that appends a newline to the output. */
37 function println($args)
39 $args = func_get_args();
40 $fmt = array_shift($args);
41 vprintf($fmt . "\n", $args);
44 /* Make PEL speak the users language, if it is available. */
45 setlocale(LC_ALL, '');
47 /* Load the required class definitions. */
48 require_once '../autoload.php';
50 use lsolesen\pel\PelDataWindow;
51 use lsolesen\pel\PelJpeg;
52 use lsolesen\pel\PelTiff;
54 $prog = array_shift($argv);
57 /* Accept the optional -d command line argument. */
58 if (isset($argv[0]) && $argv[0] == '-d') {
64 * Print usage information if there are no more command line
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.');
74 println('The files will be renamed based on their Exif timestamp.');
79 * We typically need lots of RAM to parse TIFF images since they tend
80 * to be big and uncompressed.
82 ini_set('memory_limit', '32M');
84 foreach ($argv as $file) {
85 println('Reading file "%s".', $file);
86 $data = new PelDataWindow(file_get_contents($file));
88 if (PelJpeg::isValid($data)) {
89 $jpeg = new PelJpeg();
91 $app1 = $jpeg->getExif();
92 $tiff = $app1->getTiff();
93 } elseif (PelTiff::isValid($data)) {
94 $tiff = new PelTiff($data);
96 println('Unrecognized image format! Skipping.');
100 $ifd0 = $tiff->getIfd();
101 $entry = $ifd0->getEntry(PelTag::DATE_TIME);
103 if ($entry == null) {
104 println('Skipping %s because no DATE_TIME tag was found.', $file);
108 $time = $entry->getValue();
111 $new = gmdate('Y:m:d-H:i:s', $time) . strchr($file, '.');
112 println('Trying file name %s', $new);
114 } while (file_exists($new));