5 * PEL: PHP Exif Library. A library with support for reading and
6 * writing all Exif headers in JPEG and TIFF images using PHP.
8 * Copyright (C) 2005, 2006 Martin Geisler.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program in the file COPYING; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
27 * This meta-program will generate a PHP script with unit tests for
28 * testing the image supplied on the command line. It works by
29 * loading the image, and traversing it, outputting test code at each
30 * step which will verify that a future parse of the image gives the
33 if (count($argv) != 2) {
34 exit("Usage: $argv[0] <image>\n");
37 $basename = substr($argv[1], 0, - strlen(strrchr($argv[1], '.')));
38 $image_filename = $argv[1];
39 $thumb_filename = $basename . '-thumb.jpg';
40 $test_filename = $basename . '.php';
41 $test_name = str_replace('-', '_', $basename);
45 function println($args)
48 $args = func_get_args();
49 $str = array_shift($args);
50 vprintf(str_repeat(' ', $indent) . $str . "\n", $args);
55 return str_replace(array(
64 function entryToTest($name, PelEntry $entry)
66 println('$this->assertInstanceOf(\'%s\', %s);', $name, get_class($entry));
68 println('$this->assertEquals(%s->getValue(), %s);', $name, var_export($entry->getValue(), true));
70 println('$this->assertEquals(%s->getText(), \'%s\');', $name, quote($entry->getText()));
73 function ifdToTest($name, $number, PelIfd $ifd)
76 println('/* Start of IDF %s%d. */', $name, $number);
78 $entries = $ifd->getEntries();
79 println('$this->assertEquals(count(%s%d->getEntries()), %d);', $name, $number, count($entries));
81 foreach ($entries as $tag => $entry) {
83 println('$entry = %s%d->getEntry(%d); // %s', $name, $number, $tag, PelTag::getName($ifd->getType(), $tag));
84 entryToTest('$entry', $entry);
88 println('/* Sub IFDs of %s%d. */', $name, $number);
90 $sub_ifds = $ifd->getSubIfds();
91 println('$this->assertEquals(count(%s%d->getSubIfds()), %d);', $name, $number, count($sub_ifds));
94 $sub_name = $name . $number . '_';
95 foreach ($sub_ifds as $type => $sub_ifd) {
96 println('%s%d = %s%d->getSubIfd(%d); // IFD %s', $sub_name, $n, $name, $number, $type, $sub_ifd->getName());
97 println('$this->assertInstanceOf(\'PelIfd\', %s%d);', $sub_name, $n);
98 ifdToTest($sub_name, $n, $sub_ifd);
104 if (strlen($ifd->getThumbnailData()) > 0) {
105 println('$thumb_data = file_get_contents(dirname(__FILE__) .');
106 println(' \'/%s\');', $GLOBALS['thumb_filename']);
107 println('$this->assertEquals(%s%d->getThumbnailData(), $thumb_data);', $name, $number);
109 println('$this->assertEquals(%s%d->getThumbnailData(), \'\');', $name, $number);
113 println('/* Next IFD. */');
115 $next = $ifd->getNextIfd();
116 println('%s%d = %s%d->getNextIfd();', $name, $number + 1, $name, $number);
118 if ($next instanceof PelIfd) {
119 println('$this->assertInstanceOf(\'PelIfd\', %s%d);', $name, $number + 1);
120 println('/* End of IFD %s%d. */', $name, $number);
122 ifdToTest($name, $number + 1, $next);
124 println('$this->assertNull(%s%d);', $name, $number + 1);
125 println('/* End of IFD %s%d. */', $name, $number);
129 function tiffToTest($name, PelTiff $tiff)
132 println('/* The first IFD. */');
133 println('$ifd0 = %s->getIfd();', $name);
134 $ifd = $tiff->getIfd();
135 if ($ifd instanceof PelIfd) {
136 println('$this->assertInstanceOf(\'PelIfd\', $ifd0);');
137 ifdToTest('$ifd', 0, $ifd);
139 println('$this->assertNull($ifd0);');
143 function jpegContentToTest($name, PelJpegContent $content)
145 if ($content instanceof PelExif) {
146 println('$this->assertInstanceOf(\'PelExif\', %s);', $name);
147 $tiff = $content->getTiff();
149 println('$tiff = %s->getTiff();', $name);
150 if ($tiff instanceof PelTiff) {
151 println('$this->assertInstanceOf(\'PelTiff\', $tiff);');
152 tiffToTest('$tiff', $tiff);
157 function jpegToTest($name, PelJpeg $jpeg)
159 $exif = $jpeg->getExif();
160 println('$exif = %s->getExif();', $name);
162 println('$this->assertNull($exif);');
164 jpegContentToTest('$exif', $exif);
169 * convert a binary string to a sequence of hexadecimals
171 function binstrencode($field)
173 $field = bin2hex($field);
174 $field = chunk_split($field, 2, "\\x");
175 return str_replace('\x00', '\0', "\\x" . substr($field, 0, - 2));
179 * All output is buffered so that we can dump it in $test_filename at
186 /* PEL: PHP Exif Library. A library with support for reading and
187 * writing all Exif headers in JPEG and TIFF images using PHP.
189 * Copyright (C) 2005, 2006 Martin Geisler.
191 * This program is free software; you can redistribute it and/or modify
192 * it under the terms of the GNU General Public License as published by
193 * the Free Software Foundation; either version 2 of the License, or
194 * (at your option) any later version.
196 * This program is distributed in the hope that it will be useful,
197 * but WITHOUT ANY WARRANTY; without even the implied warranty of
198 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
199 * GNU General Public License for more details.
201 * You should have received a copy of the GNU General Public License
202 * along with this program in the file COPYING; if not, write to the
203 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
204 * Boston, MA 02110-1301 USA
208 /* Autogenerated by the make-image-test.php script */
210 use lsolesen\pel\PelJpeg;
212 class %s extends \PHPUnit_Framework_TestCase
217 Pel::clearExceptions();
218 Pel::setStrictParsing(false);
219 $jpeg = new PelJpeg(dirname(__FILE__) . \'/%s\');
220 ', $test_name, $image_filename, $image_filename);
222 require_once(dirname(__FILE__) . '/../../src/PelJpeg.php');
223 $jpeg = new PelJpeg($image_filename);
226 jpegToTest('$jpeg', $jpeg);
230 $exceptions = Pel::getExceptions();
232 if (count($exceptions) == 0) {
233 println('$this->assertTrue(count(Pel::getExceptions()) == 0);');
235 println('$exceptions = Pel::getExceptions();');
236 for ($i = 0; $i < count($exceptions); $i ++) {
237 println('$this->assertInstanceOf(\'%s\', $exceptions[%d]);', $i, get_class($exceptions[$i]));
239 println('$this->assertEquals($exceptions[%d]->getMessage(),', $i);
240 println(' \'%s\');', quote($exceptions[$i]->getMessage()));
249 /* The test case is finished -- now dump the output as a PHP file. */
250 file_put_contents($test_filename, ob_get_clean());