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, 2007 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
27 /* a printf() variant that appends a newline to the output. */
28 function println($args)
30 $args = func_get_args();
31 $fmt = array_shift($args);
32 vprintf($fmt . "\n", $args);
35 /* Make PEL speak the users language, if it is available. */
36 setlocale(LC_ALL, '');
39 * Load the required files. One would normally just require the
40 * PelJpeg.php file for dealing with JPEG images, but because this
41 * example can handle both JPEG and TIFF it loads the PelDataWindow
44 require_once '../autoload.php';
46 use lsolesen\pel\PelDataWindow;
47 use lsolesen\pel\PelJpeg;
48 use lsolesen\pel\PelTiff;
51 * Store the name of the script in $prog and remove this first part of
54 $prog = array_shift($argv);
58 * The next argument could be -d to signal debug mode where lots of
59 * extra information is printed out when the image is parsed.
61 if (isset($argv[0]) && $argv[0] == '-d') {
66 /* The mandatory input filename. */
67 if (isset($argv[0])) {
68 $input = array_shift($argv);
73 /* The mandatory output filename. */
74 if (isset($argv[0])) {
75 $output = array_shift($argv);
81 * Usage information is printed if an error was found in the command
85 println('Usage: %s [-d] <input> <output> [desc]', $prog);
86 println('Optional arguments:');
87 println(' -d turn debug output on.');
88 println(' desc the new description.');
89 println('Mandatory arguments:');
90 println(' input the input file, a JPEG or TIFF image.');
91 println(' output the output file for the changed image.');
95 /* Any remaining arguments are considered the new description. */
96 $description = implode(' ', $argv);
99 * We typically need lots of RAM to parse TIFF images since they tend
100 * to be big and uncompressed.
102 ini_set('memory_limit', '32M');
105 * The input file is now read into a PelDataWindow object. At this
106 * point we do not know if the file stores JPEG or TIFF data, so
107 * instead of using one of the loadFile methods on PelJpeg or PelTiff
108 * we store the data in a PelDataWindow.
110 println('Reading file "%s".', $input);
111 $data = new PelDataWindow(file_get_contents($input));
114 * The static isValid methods in PelJpeg and PelTiff will tell us in
115 * an efficient maner which kind of data we are dealing with.
117 if (PelJpeg::isValid($data)) {
119 * The data was recognized as JPEG data, so we create a new empty
120 * PelJpeg object which will hold it. When we want to save the
121 * image again, we need to know which object to same (using the
122 * getBytes method), so we store $jpeg as $file too.
124 $jpeg = $file = new PelJpeg();
127 * We then load the data from the PelDataWindow into our PelJpeg
128 * object. No copying of data will be done, the PelJpeg object will
129 * simply remember that it is to ask the PelDataWindow for data when
135 * The PelJpeg object contains a number of sections, one of which
136 * might be our Exif data. The getExif() method is a convenient way
137 * of getting the right section with a minimum of fuzz.
139 $exif = $jpeg->getExif();
143 * Ups, there is no APP1 section in the JPEG file. This is where
144 * the Exif data should be.
146 println('No APP1 section found, added new.');
149 * In this case we simply create a new APP1 section (a PelExif
150 * object) and adds it to the PelJpeg object.
152 $exif = new PelExif();
153 $jpeg->setExif($exif);
155 /* We then create an empty TIFF structure in the APP1 section. */
156 $tiff = new PelTiff();
157 $exif->setTiff($tiff);
160 * Surprice, surprice: Exif data is really just TIFF data! So we
161 * extract the PelTiff object for later use.
163 println('Found existing APP1 section.');
164 $tiff = $exif->getTiff();
166 } elseif (PelTiff::isValid($data)) {
168 * The data was recognized as TIFF data. We prepare a PelTiff
169 * object to hold it, and record in $file that the PelTiff object is
170 * the top-most object (the one on which we will call getBytes).
172 $tiff = $file = new PelTiff();
173 /* Now load the data. */
177 * The data was not recognized as either JPEG or TIFF data.
178 * Complain loudly, dump the first 16 bytes, and exit.
180 println('Unrecognized image format! The first 16 bytes follow:');
181 PelConvert::bytesToDump($data->getBytes(0, 16));
186 * TIFF data has a tree structure much like a file system. There is a
187 * root IFD (Image File Directory) which contains a number of entries
188 * and maybe a link to the next IFD. The IFDs are chained together
189 * like this, but some of them can also contain what is known as
190 * sub-IFDs. For our purpose we only need the first IFD, for this is
191 * where the image description should be stored.
193 $ifd0 = $tiff->getIfd();
197 * No IFD in the TIFF data? This probably means that the image
198 * didn't have any Exif information to start with, and so an empty
199 * PelTiff object was inserted by the code above. But this is no
200 * problem, we just create and inserts an empty PelIfd object.
202 println('No IFD found, adding new.');
203 $ifd0 = new PelIfd(PelIfd::IFD0);
204 $tiff->setIfd($ifd0);
208 * Each entry in an IFD is identified with a tag. This will load the
209 * ImageDescription entry if it is present. If the IFD does not
210 * contain such an entry, null will be returned.
212 $desc = $ifd0->getEntry(PelTag::IMAGE_DESCRIPTION);
214 /* We need to check if the image already had a description stored. */
216 /* The was no description in the image. */
217 println('Added new IMAGE_DESCRIPTION entry with "%s".', $description);
220 * In this case we simply create a new PelEntryAscii object to hold
221 * the description. The constructor for PelEntryAscii needs to know
222 * the tag and contents of the new entry.
224 $desc = new PelEntryAscii(PelTag::IMAGE_DESCRIPTION, $description);
227 * This will insert the newly created entry with the description
230 $ifd0->addEntry($desc);
232 /* An old description was found in the image. */
233 println('Updating IMAGE_DESCRIPTION entry from "%s" to "%s".', $desc->getValue(), $description);
235 /* The description is simply updated with the new description. */
236 $desc->setValue($description);
240 * At this point the image on disk has not been changed, it is only
241 * the object structure in memory which represent the image which has
242 * been altered. This structure can be converted into a string of
243 * bytes with the getBytes method, and saving this in the output file
244 * completes the script.
246 println('Writing file "%s".', $output);
247 $file->saveFile($output);