forked from leftypol/leftypol
PngExifReader.php: add png exif reader with parser!!!!
This commit is contained in:
parent
15946417d6
commit
a26b6a9eba
1 changed files with 148 additions and 0 deletions
148
inc/Data/Driver/Metadata/PngExifReader.php
Normal file
148
inc/Data/Driver/Metadata/PngExifReader.php
Normal file
|
@ -0,0 +1,148 @@
|
|||
<?php
|
||||
namespace Vichan\Data\Driver;
|
||||
|
||||
|
||||
class PngExifReader implements ExifReader {
|
||||
// Chunks larger than this will be ignored.
|
||||
private const MAX_CHUNK_SIZE = 1048576; // 1 MB
|
||||
private const ORIENTATION_TAG_ID = 0x0112;
|
||||
// Exif data type identifier.
|
||||
private const TYPE_SHORT = 0x3;
|
||||
|
||||
|
||||
private static function tryReadExifChunk(string $chunk_blob): ?int {
|
||||
// Ensure the chunk starts with "Exif" (EXIF header).
|
||||
if (\substr($chunk_blob, 0, 6) !== "Exif\0\0") {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Remove the "Exif\0\0" header.
|
||||
// This is to simplify offset calculations later on since offsets are relative to the TIFF header.
|
||||
$tiff_data = \substr($chunk_blob, 6);
|
||||
|
||||
// Determine byte order (II for little-endian, MM for big-endian).
|
||||
$byte_order = \substr($tiff_data, 0, 2);
|
||||
if ($byte_order === 'II') {
|
||||
$little_endian = true;
|
||||
} elseif ($byte_order === 'MM') {
|
||||
$little_endian = false;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Verify tag mark.
|
||||
$tag_mark = \substr($tiff_data, 2, 4);
|
||||
if ($little_endian) {
|
||||
if ($tag_mark !== "*\0") {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
if ($tag_mark !== "\0*") {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Unpack format string.
|
||||
$unpack_fmt = $little_endian ? 'v' : 'n';
|
||||
|
||||
// Offset to the first IFD.
|
||||
$current_offset = \unpack($unpack_fmt, \substr($tiff_data, 4, 2))[1];
|
||||
if ($current_offset > \strlen($chunk_blob)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
while ($current_offset > 0) {
|
||||
// Number of directory entries.
|
||||
$num_entries = \unpack($unpack_fmt, \substr($tiff_data, $current_offset, 2))[1];
|
||||
$current_offset += 2;
|
||||
|
||||
if ($current_offset + $num_entries * 12 > \strlen($chunk_blob)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
for ($i = 0; $i < $num_entries; $i++) {
|
||||
$entry_offset = $current_offset + ($i * 12);
|
||||
|
||||
// Read tag ID.
|
||||
$tag_id = \unpack($unpack_fmt, \substr($tiff_data, $entry_offset, 2))[1];
|
||||
|
||||
if ($tag_id === self::ORIENTATION_TAG_ID) {
|
||||
$field_type = \unpack($unpack_fmt, \substr($tiff_data, $entry_offset + 2, 2))[1];
|
||||
$value_count = \unpack($unpack_fmt, \substr($tiff_data, $entry_offset + 4, 4))[1];
|
||||
|
||||
if ($field_type === self::TYPE_SHORT && $value_count !== 1) {
|
||||
// Read value. It is stored directly if it's <4 bytes.
|
||||
$value_offset = $entry_offset + 8;
|
||||
$orientation = \unpack($unpack_fmt, \substr($tiff_data, $value_offset, 2))[1];
|
||||
|
||||
return $orientation; // Return the Orientation value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Offset to the next IFD (last 4 bytes of the directory)
|
||||
$current_offset = \unpack($unpack_fmt, \substr($tiff_data, $current_offset + ($num_entries * 12), 4))[1];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// Mostly adapted from https://stackoverflow.com/a/2190438
|
||||
private static function tryReadPngChunks(mixed $fd): ?int {
|
||||
$text_chunks = [];
|
||||
|
||||
// Read the magic bytes and verify.
|
||||
$header = \fread($fd, 8);
|
||||
|
||||
if ($header != "\x89PNG\x0d\x0a\x1a\x0a") {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Loop through the PNG's chunks. Byte 0-3 is length, Byte 4-7 is type.
|
||||
$chunkHeader = \fread($fd, 8);
|
||||
|
||||
while ($chunkHeader) {
|
||||
// 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) {
|
||||
return $ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Skip to next chunk (over body and CRC)
|
||||
\fseek($fd, $chunk['size'] + 4, SEEK_CUR);
|
||||
|
||||
// Read next chunk header
|
||||
$chunkHeader = \fread($fd, 8);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getOrientation(string $file): ?int {
|
||||
// Open the file.
|
||||
$fd = \fopen($file, 'r');
|
||||
if ($fd === false) {
|
||||
return null;
|
||||
}
|
||||
$ret = self::tryReadPngChunks($fd);
|
||||
\fclose($fd);
|
||||
if ($ret === null || $ret > 9) {
|
||||
return null;
|
||||
} else {
|
||||
return $ret;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue