From 1a457a0860f9157af3011265fc6288abec63dc74 Mon Sep 17 00:00:00 2001 From: mondrake Date: Wed, 8 Jan 2025 21:51:17 +0100 Subject: [PATCH] PelEntryTime is not bijective (#32) --- src/PelEntryTime.php | 26 ++++++++++++++++---------- test/ReadWriteTest.php | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/PelEntryTime.php b/src/PelEntryTime.php index d33b60d2..4ae79c13 100644 --- a/src/PelEntryTime.php +++ b/src/PelEntryTime.php @@ -154,12 +154,15 @@ public function getValue(int $type = self::UNIX_TIMESTAMP): string public function setValue(mixed $timestamp, int $type = self::UNIX_TIMESTAMP): void { if ($type === self::UNIX_TIMESTAMP) { - if (is_int($timestamp) || is_float($timestamp)) { - $this->day_count = (int) $this->convertUnixToJd($timestamp); - $this->seconds = $timestamp % 86400; - } else { - throw new PelInvalidArgumentException('Expected integer value for $type, got %s', gettype($timestamp)); + if (is_string($timestamp)) { + if (is_numeric($timestamp)) { + $timestamp = (int) $timestamp; + } else { + throw new PelInvalidArgumentException('Expected numeric value for $type, got "%s"', $timestamp); + } } + $this->day_count = (int) $this->convertUnixToJd($timestamp); + $this->seconds = $timestamp % 86400; } elseif ($type === self::EXIF_STRING) { /* * Clean the timestamp: some timestamps are broken other @@ -179,12 +182,15 @@ public function setValue(mixed $timestamp, int $type = self::UNIX_TIMESTAMP): vo $this->day_count = $this->convertGregorianToJd((int) $d[0], (int) $d[1], (int) $d[2]); $this->seconds = (int) $d[3] * 3600 + (int) $d[4] * 60 + (int) $d[5]; } elseif ($type === self::JULIAN_DAY_COUNT) { - if (is_int($timestamp) || is_float($timestamp)) { - $this->day_count = (int) floor($timestamp); - $this->seconds = (int) (86400 * ($timestamp - floor($timestamp))); - } else { - throw new PelInvalidArgumentException('Expected integer value for $type, got %s', gettype($timestamp)); + if (is_string($timestamp)) { + if (is_numeric($timestamp)) { + $timestamp = (int) $timestamp; + } else { + throw new PelInvalidArgumentException('Expected numeric value for $type, got "%s"', $timestamp); + } } + $this->day_count = (int) floor($timestamp); + $this->seconds = (int) (86400 * ($timestamp - floor($timestamp))); } else { throw new PelInvalidArgumentException('Expected UNIX_TIMESTAMP (%d), EXIF_STRING (%d), or JULIAN_DAY_COUNT (%d) for $type, got %d.', self::UNIX_TIMESTAMP, self::EXIF_STRING, self::JULIAN_DAY_COUNT, $type); } diff --git a/test/ReadWriteTest.php b/test/ReadWriteTest.php index 10ae2c81..5bde6b8e 100644 --- a/test/ReadWriteTest.php +++ b/test/ReadWriteTest.php @@ -191,6 +191,38 @@ public static function writeEntryProvider(): \Iterator ]; } + /** + * Tests loading and writing back a JPEG image file. + */ + public function testJpegLoadSave(): void + { + $file_uri = __DIR__ . '/imagetests/canon-eos-650d.jpg'; + $jpeg = new PelJpeg($file_uri); + $exif = $jpeg->getExif(); + $this->assertInstanceOf(PelExif::class, $exif); + $tiff = $exif->getTiff(); + $this->assertInstanceOf(PelTiff::class, $tiff); + $ifd0 = $tiff->getIfd(); + $this->assertInstanceOf(PelIfd::class, $ifd0); + + $entry = $ifd0->getEntry(271); // Make + $this->assertInstanceOf(PelEntryAscii::class, $entry); + $this->assertSame('Canon', $entry->getValue()); + $entry->setValue('Foo-Bar'); + + $out_uri = __DIR__ . '/imagetests/output.canon-eos-650d.jpg'; + $jpeg->saveFile($out_uri); + + unset($jpeg); + + $data_reload = exif_read_data($out_uri); + $this->assertIsArray($data_reload); + $this->assertArrayHasKey('Make', $data_reload); + $this->assertEquals('Foo-Bar', $data_reload['Make']); + + unlink($out_uri); + } + /** * Tests loading and writing back a TIFF image file. */