1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-17 03:13:59 +02:00

Add method to detect PNG color types

This commit is contained in:
Oliver Vogel
2024-08-03 11:04:30 +02:00
parent 460b205687
commit 2ee997d98f

View File

@@ -24,4 +24,29 @@ trait CanInspectPngFormat
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',
};
}
}