Skip to content

Commit

Permalink
Add method to detect PNG color types
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Aug 3, 2024
1 parent 460b205 commit 2ee997d
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tests/Traits/CanInspectPngFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,29 @@ private function isInterlacedPng(string $imagedata): bool

return ord($contents[28]) != 0;
}

/**
* Try to detect PNG color type from given binary data
*
* @param string $data
* @return string
*/
private function pngColorType(string $data): string
{
if (substr($data, 1, 3) !== 'PNG') {
return 'unkown';
}

$pos = strpos($data, 'IHDR');
$type = substr($data, $pos + 13, 1);

return match (unpack('C', $type)[1]) {
0 => 'grayscale',
2 => 'truecolor',
3 => 'indexed',
4 => 'grayscale-alpha',
6 => 'truecolor-alpha',
default => 'unknown',
};
}
}

0 comments on commit 2ee997d

Please sign in to comment.