4 * PEL: PHP Exif Library. A library with support for reading and
5 * writing all Exif headers in JPEG and TIFF images using PHP.
7 * Copyright (C) 2004, 2006, 2007 Martin Geisler.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program in the file COPYING; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301 USA
26 use lsolesen\pel\PelEntryByte;
27 use lsolesen\pel\PelIfd;
28 use lsolesen\pel\PelTiff;
29 use lsolesen\pel\PelExif;
30 use lsolesen\pel\PelJpeg;
31 use lsolesen\pel\PelEntrySByte;
32 use lsolesen\pel\PelEntryShort;
33 use lsolesen\pel\PelEntrySShort;
34 use lsolesen\pel\PelEntryLong;
35 use lsolesen\pel\PelEntrySLong;
36 use lsolesen\pel\PelEntryAscii;
38 abstract class WriteEntryTestCase extends \PHPUnit_Framework_TestCase
40 protected $entries = array();
42 public function testWriteRead()
44 Pel::setStrictParsing(true);
46 $ifd = new PelIfd(PelIfd::IFD0);
47 $this->assertTrue($ifd->isLastIfd());
49 foreach ($this->entries as $entry) {
50 $ifd->addEntry($entry);
53 $tiff = new PelTiff();
54 $this->assertNull($tiff->getIfd());
56 $this->assertNotNull($tiff->getIfd());
58 $exif = new PelExif();
59 $this->assertNull($exif->getTiff());
60 $exif->setTiff($tiff);
61 $this->assertNotNull($exif->getTiff());
63 $jpeg = new PelJpeg(dirname(__FILE__) . '/no-exif.jpg');
64 $this->assertNull($jpeg->getExif());
65 $jpeg->setExif($exif);
66 $this->assertNotNull($jpeg->getExif());
68 $jpeg->saveFile('test-output.jpg');
69 $this->assertTrue(file_exists('test-output.jpg'));
70 $this->assertTrue(filesize('test-output.jpg') > 0);
72 /* Now read the file and see if the entries are still there. */
73 $jpeg = new PelJpeg('test-output.jpg');
75 $exif = $jpeg->getExif();
76 $this->assertInstanceOf('lsolesen\pel\PelExif', $exif);
78 $tiff = $exif->getTiff();
79 $this->assertInstanceOf('lsolesen\pel\PelTiff', $tiff);
81 $ifd = $tiff->getIfd();
82 $this->assertInstanceOf('lsolesen\pel\PelIfd', $ifd);
84 $this->assertEquals($ifd->getType(), PelIfd::IFD0);
85 $this->assertTrue($ifd->isLastIfd());
87 foreach ($this->entries as $entry) {
88 $this->assertEquals($ifd->getEntry($entry->getTag())
89 ->getValue(), $entry->getValue());
92 unlink('test-output.jpg');
96 class WriteByteTestCase extends WriteEntryTestCase
98 public function __construct()
100 $this->entries[] = new PelEntryByte(0xF001, 0);
101 $this->entries[] = new PelEntryByte(0xF002, 1);
102 $this->entries[] = new PelEntryByte(0xF003, 2);
103 $this->entries[] = new PelEntryByte(0xF004, 253);
104 $this->entries[] = new PelEntryByte(0xF005, 254);
105 $this->entries[] = new PelEntryByte(0xF006, 255);
107 $this->entries[] = new PelEntryByte(0xF007, 0, 1, 2, 253, 254, 255);
108 $this->entries[] = new PelEntryByte(0xF008);
110 parent::__construct('PEL Byte Read/Write Tests');
114 class WriteSByteTestCase extends WriteEntryTestCase
116 public function __construct()
118 $this->entries[] = new PelEntrySByte(0xF101, - 128);
119 $this->entries[] = new PelEntrySByte(0xF102, - 127);
120 $this->entries[] = new PelEntrySByte(0xF103, - 1);
121 $this->entries[] = new PelEntrySByte(0xF104, 0);
122 $this->entries[] = new PelEntrySByte(0xF105, 1);
123 $this->entries[] = new PelEntrySByte(0xF106, 126);
124 $this->entries[] = new PelEntrySByte(0xF107, 127);
126 $this->entries[] = new PelEntrySByte(0xF108, - 128, - 1, 0, 1, 127);
127 $this->entries[] = new PelEntrySByte(0xF109);
129 parent::__construct('PEL SByte Read/Write Tests');
133 class WriteShortTestCase extends WriteEntryTestCase
135 public function __construct()
137 $this->entries[] = new PelEntryShort(0xF201, 0);
138 $this->entries[] = new PelEntryShort(0xF202, 1);
139 $this->entries[] = new PelEntryShort(0xF203, 2);
140 $this->entries[] = new PelEntryShort(0xF204, 65533);
141 $this->entries[] = new PelEntryShort(0xF205, 65534);
142 $this->entries[] = new PelEntryShort(0xF206, 65535);
144 $this->entries[] = new PelEntryShort(0xF208, 0, 1, 65534, 65535);
145 $this->entries[] = new PelEntryShort(0xF209);
147 parent::__construct('PEL Short Read/Write Tests');
151 class WriteSShortTestCase extends WriteEntryTestCase
153 public function __construct()
155 $this->entries[] = new PelEntrySShort(0xF301, - 32768);
156 $this->entries[] = new PelEntrySShort(0xF302, - 32767);
157 $this->entries[] = new PelEntrySShort(0xF303, - 1);
158 $this->entries[] = new PelEntrySShort(0xF304, 0);
159 $this->entries[] = new PelEntrySShort(0xF305, 1);
160 $this->entries[] = new PelEntrySShort(0xF306, 32766);
161 $this->entries[] = new PelEntrySShort(0xF307, 32767);
163 $this->entries[] = new PelEntrySShort(0xF308, - 32768, - 1, 0, 1, 32767);
164 $this->entries[] = new PelEntrySShort(0xF309);
166 parent::__construct('PEL SShort Read/Write Tests');
170 class WriteLongTestCase extends WriteEntryTestCase
172 public function __construct()
174 $this->entries[] = new PelEntryLong(0xF401, 0);
175 $this->entries[] = new PelEntryLong(0xF402, 1);
176 $this->entries[] = new PelEntryLong(0xF403, 2);
177 $this->entries[] = new PelEntryLong(0xF404, 4294967293);
178 $this->entries[] = new PelEntryLong(0xF405, 4294967294);
179 $this->entries[] = new PelEntryLong(0xF406, 4294967295);
181 $this->entries[] = new PelEntryLong(0xF408, 0, 1, 4294967295);
182 $this->entries[] = new PelEntryLong(0xF409);
184 parent::__construct('PEL Long Read/Write Tests');
188 class WriteSLongTestCase extends WriteEntryTestCase
190 public function __construct()
192 $this->entries[] = new PelEntrySLong(0xF501, - 2147483648);
193 $this->entries[] = new PelEntrySLong(0xF502, - 2147483647);
194 $this->entries[] = new PelEntrySLong(0xF503, - 1);
195 $this->entries[] = new PelEntrySLong(0xF504, 0);
196 $this->entries[] = new PelEntrySLong(0xF505, 1);
197 $this->entries[] = new PelEntrySLong(0xF506, 2147483646);
198 $this->entries[] = new PelEntrySLong(0xF507, 2147483647);
200 $this->entries[] = new PelEntrySLong(0xF508, - 2147483648, 0, 2147483647);
201 $this->entries[] = new PelEntrySLong(0xF509);
203 parent::__construct('PEL SLong Read/Write Tests');
207 class WriteAsciiTestCase extends WriteEntryTestCase
209 public function __construct()
211 $this->entries[] = new PelEntryAscii(0xF601);
212 $this->entries[] = new PelEntryAscii(0xF602, '');
213 $this->entries[] = new PelEntryAscii(0xF603, 'Hello World!');
214 $this->entries[] = new PelEntryAscii(0xF604, "\x00\x01\x02...\xFD\xFE\xFF");
216 parent::__construct('PEL Ascii Read/Write Tests');