Skip to content

Commit

Permalink
PelEntryTime is not bijective (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
mondrake authored Jan 8, 2025
1 parent 71bc738 commit 1a457a0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/PelEntryTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
Expand Down
32 changes: 32 additions & 0 deletions test/ReadWriteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down

0 comments on commit 1a457a0

Please sign in to comment.