22 $png_signature = pack(
"C8", 137, 80, 78, 71, 13, 10, 26, 10);
24 $header = substr($this->_contents, 0, 8);
25 if (
$header != $png_signature) {
26 echo 'This is not a valid PNG image';
28 $this->_size = strlen($this->_contents);
29 $this->_chunks =
array();
33 $chunk = @unpack(
'Nsize/a4type', substr($this->_contents, $position, 8));
34 $this->_chunks[$chunk[
'type']][] = substr($this->_contents, $position + 8, $chunk[
'size']);
36 $position += $chunk[
'size'] + 12;
37 }
while ($position < $this->_size);
50 if (array_key_exists($type, $this->_chunks)) {
51 foreach (array_keys($this->_chunks[$type]) as $typekey) {
52 list($key,
$data) = explode(
"\0", $this->_chunks[$type][$typekey]);
53 if (strcmp($key, $check) == 0) {
54 echo 'Key "' . $check .
'" already exists in "' . $type .
'" chunk.';
71 public function addChunk($chunkType, $key, $value) {
73 $chunkData = $key .
"\0" . $value;
74 $crc = pack(
"N", crc32($chunkType . $chunkData));
75 $len = pack(
"N", strlen($chunkData));
77 $newChunk = $len . $chunkType . $chunkData . $crc;
78 $result = substr($this->_contents, 0, $this->_size - 12)
80 . substr($this->_contents, $this->_size - 12, 12);
95 $retval = substr($png,0,8);
97 if ($retval !=
"\x89PNG\x0d\x0a\x1a\x0a")
98 throw new Exception(
'Is not a valid PNG image');
100 $chunkHeader = substr($png,$ipos,8);
102 while ($chunkHeader) {
104 $chunk = @unpack(
'Nsize/a4type', $chunkHeader);
106 if ( $chunk[
'type'] == $chunkType ) {
107 $data = substr($png,$ipos,$chunk[
'size']);
108 $sections = explode(
"\0",
$data);
110 if ( $sections[0] == $key ) $skip =
true;
113 $data = substr($png,$ipos,$chunk[
'size']+4);
114 $ipos = $ipos + $chunk[
'size'] + 4;
116 if ( ! $skip ) $retval = $retval . $chunkHeader .
$data;
118 $chunkHeader = substr($png,$ipos,8);
136 $retval = substr($png,0,8);
138 if ($retval !=
"\x89PNG\x0d\x0a\x1a\x0a") {
143 $chunkHeader = substr($png,$ipos,8);
145 while ($chunkHeader) {
147 $chunk = @unpack(
'Nsize/a4type', $chunkHeader);
149 if ($chunk[
'type'] ==
'tEXt') {
150 $data = substr($png,$ipos,$chunk[
'size']);
151 $sections = explode(
"\0",
$data);
152 if ($sections[0] == $key) {
157 $data = substr($png,$ipos,$chunk[
'size']+4);
158 $ipos = $ipos + $chunk[
'size'] + 4;
161 $chunkHeader = substr($png,$ipos,8);
Php library to Bake the PNG Images.
__construct($contents)
Prepares file for handling metadata.
addChunk($chunkType, $key, $value)
Add a chunk by type with given key and text.
Create styles array
The data for the language used.
removeChunks($chunkType, $key, $png)
removes a chunk by type with given key and text
extractBadgeInfo($png, $key='openbadges')
Extracts the baked PNG info by the Key.
checkChunks($type, $check)
Checks if a key already exists in the chunk of said type.