diff --git a/inc/Data/Driver/Metadata/PngExifReader.php b/inc/Data/Driver/Metadata/PngExifReader.php index 9791dfb9..f56471cb 100644 --- a/inc/Data/Driver/Metadata/PngExifReader.php +++ b/inc/Data/Driver/Metadata/PngExifReader.php @@ -1,5 +1,5 @@ \strlen($chunk_blob)) { - return null; + return self::ERR_BAD_OFFSET_TO_IFD; } while ($current_offset > 0) { @@ -57,7 +69,7 @@ class PngExifReader implements ExifReader { $current_offset += 2; if ($current_offset + $num_entries * 12 > \strlen($chunk_blob)) { - return null; + return self::ERR_BAD_OFFSET_TO_DIR_ENTRY; } for ($i = 0; $i < $num_entries; $i++) { @@ -84,18 +96,16 @@ class PngExifReader implements ExifReader { $current_offset = \unpack($unpack_fmt, \substr($tiff_data, $current_offset + ($num_entries * 12), 4))[1]; } - return null; + return self::ERR_BAD_ORIENTATION_NOT_FOUND; } // Mostly adapted from https://stackoverflow.com/a/2190438 - private static function tryReadPngChunks(mixed $fd): ?int { - $text_chunks = []; - + private static function tryReadPngChunks(mixed $fd): int { // Read the magic bytes and verify. $header = \fread($fd, 8); if ($header != "\x89PNG\x0d\x0a\x1a\x0a") { - return null; + return self::ERR_NOT_A_PNG; } // Loop through the PNG's chunks. Byte 0-3 is length, Byte 4-7 is type. @@ -105,17 +115,12 @@ class PngExifReader implements ExifReader { // Extract length and type from binary data. $chunk = @\unpack('Nsize/a4type', $chunkHeader); - // Store position into internal array. - if (!isset($text_chunks[$chunk['type']])) { - $text_chunks[$chunk['type']] = []; - } - if ($chunk['type'] === 'tEXt') { if ($chunk['size'] > 0 && $chunk['size'] < self::MAX_CHUNK_SIZE) { $size = $chunk['size']; $chunk_blob = \fread($fd, $size); $ret = self::tryReadExifChunk($chunk_blob); - if ($ret !== null) { + if ($ret < 1) { return $ret; } } @@ -128,18 +133,18 @@ class PngExifReader implements ExifReader { $chunkHeader = \fread($fd, 8); } - return null; + return self::ERR_EOF; } public function getOrientation(string $file): ?int { // Open the file. $fd = \fopen($file, 'r'); if ($fd === false) { - return null; + return self::ERR_COULD_NOT_OPEN; } - $ret = self::tryReadPngChunks($fd); + return self::tryReadPngChunks($fd); \fclose($fd); - if ($ret === null || $ret > 9) { + if ($ret < 1 || $ret > 9) { return null; } else { return $ret;