b846bcb2da0402c982c4b2b7b20ecbe6a9547b7c
[yaffs-website] / vendor / lsolesen / pel / examples / edit-description.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, 2007 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 /* a printf() variant that appends a newline to the output. */
28 function println($args)
29 {
30     $args = func_get_args();
31     $fmt = array_shift($args);
32     vprintf($fmt . "\n", $args);
33 }
34
35 /* Make PEL speak the users language, if it is available. */
36 setlocale(LC_ALL, '');
37
38 /*
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
42  * class too.
43  */
44 require_once '../autoload.php';
45
46 use lsolesen\pel\PelDataWindow;
47 use lsolesen\pel\PelJpeg;
48 use lsolesen\pel\PelTiff;
49
50 /*
51  * Store the name of the script in $prog and remove this first part of
52  * the command line.
53  */
54 $prog = array_shift($argv);
55 $error = false;
56
57 /*
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.
60  */
61 if (isset($argv[0]) && $argv[0] == '-d') {
62     Pel::setDebug(true);
63     array_shift($argv);
64 }
65
66 /* The mandatory input filename. */
67 if (isset($argv[0])) {
68     $input = array_shift($argv);
69 } else {
70     $error = true;
71 }
72
73 /* The mandatory output filename. */
74 if (isset($argv[0])) {
75     $output = array_shift($argv);
76 } else {
77     $error = true;
78 }
79
80 /*
81  * Usage information is printed if an error was found in the command
82  * line arguments.
83  */
84 if ($error) {
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.');
92     exit(1);
93 }
94
95 /* Any remaining arguments are considered the new description. */
96 $description = implode(' ', $argv);
97
98 /*
99  * We typically need lots of RAM to parse TIFF images since they tend
100  * to be big and uncompressed.
101  */
102 ini_set('memory_limit', '32M');
103
104 /*
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.
109  */
110 println('Reading file "%s".', $input);
111 $data = new PelDataWindow(file_get_contents($input));
112
113 /*
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.
116  */
117 if (PelJpeg::isValid($data)) {
118     /*
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.
123      */
124     $jpeg = $file = new PelJpeg();
125
126     /*
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
130      * required.
131      */
132     $jpeg->load($data);
133
134     /*
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.
138      */
139     $exif = $jpeg->getExif();
140
141     if ($exif == null) {
142         /*
143          * Ups, there is no APP1 section in the JPEG file. This is where
144          * the Exif data should be.
145          */
146         println('No APP1 section found, added new.');
147
148         /*
149          * In this case we simply create a new APP1 section (a PelExif
150          * object) and adds it to the PelJpeg object.
151          */
152         $exif = new PelExif();
153         $jpeg->setExif($exif);
154
155         /* We then create an empty TIFF structure in the APP1 section. */
156         $tiff = new PelTiff();
157         $exif->setTiff($tiff);
158     } else {
159         /*
160          * Surprice, surprice: Exif data is really just TIFF data! So we
161          * extract the PelTiff object for later use.
162          */
163         println('Found existing APP1 section.');
164         $tiff = $exif->getTiff();
165     }
166 } elseif (PelTiff::isValid($data)) {
167     /*
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).
171      */
172     $tiff = $file = new PelTiff();
173     /* Now load the data. */
174     $tiff->load($data);
175 } else {
176     /*
177      * The data was not recognized as either JPEG or TIFF data.
178      * Complain loudly, dump the first 16 bytes, and exit.
179      */
180     println('Unrecognized image format! The first 16 bytes follow:');
181     PelConvert::bytesToDump($data->getBytes(0, 16));
182     exit(1);
183 }
184
185 /*
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.
192  */
193 $ifd0 = $tiff->getIfd();
194
195 if ($ifd0 == null) {
196     /*
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.
201      */
202     println('No IFD found, adding new.');
203     $ifd0 = new PelIfd(PelIfd::IFD0);
204     $tiff->setIfd($ifd0);
205 }
206
207 /*
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.
211  */
212 $desc = $ifd0->getEntry(PelTag::IMAGE_DESCRIPTION);
213
214 /* We need to check if the image already had a description stored. */
215 if ($desc == null) {
216     /* The was no description in the image. */
217     println('Added new IMAGE_DESCRIPTION entry with "%s".', $description);
218
219     /*
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.
223      */
224     $desc = new PelEntryAscii(PelTag::IMAGE_DESCRIPTION, $description);
225
226     /*
227      * This will insert the newly created entry with the description
228      * into the IFD.
229      */
230     $ifd0->addEntry($desc);
231 } else {
232     /* An old description was found in the image. */
233     println('Updating IMAGE_DESCRIPTION entry from "%s" to "%s".', $desc->getValue(), $description);
234
235     /* The description is simply updated with the new description. */
236     $desc->setValue($description);
237 }
238
239 /*
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.
245  */
246 println('Writing file "%s".', $output);
247 $file->saveFile($output);