ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
baker.lib.php
Go to the documentation of this file.
1<?php
2
8{
9 private $_contents;
10 private $_size;
11 private $_chunks;
12
20 public function __construct($contents) {
21 $this->_contents = $contents;
22 $png_signature = pack("C8", 137, 80, 78, 71, 13, 10, 26, 10);
23 // Read 8 bytes of PNG header and verify.
24 $header = substr($this->_contents, 0, 8);
25 if ($header != $png_signature) {
26 echo 'This is not a valid PNG image';
27 }
28 $this->_size = strlen($this->_contents);
29 $this->_chunks = array();
30 // Skip 8 bytes of IHDR image header.
31 $position = 8;
32 do {
33 $chunk = @unpack('Nsize/a4type', substr($this->_contents, $position, 8));
34 $this->_chunks[$chunk['type']][] = substr($this->_contents, $position + 8, $chunk['size']);
35 // Skip 12 bytes chunk overhead.
36 $position += $chunk['size'] + 12;
37 } while ($position < $this->_size);
38 }
39
49 public function checkChunks($type, $check) {
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.';
55 return false;
56 }
57 }
58 }
59 return true;
60 }
61
71 public function addChunk($chunkType, $key, $value) {
72
73 $chunkData = $key . "\0" . $value;
74 $crc = pack("N", crc32($chunkType . $chunkData));
75 $len = pack("N", strlen($chunkData));
76
77 $newChunk = $len . $chunkType . $chunkData . $crc;
78 $result = substr($this->_contents, 0, $this->_size - 12)
79 . $newChunk
80 . substr($this->_contents, $this->_size - 12, 12);
81 return $result;
82 }
83
93 public function removeChunks($chunkType, $key, $png) {
94 // Read the magic bytes and verify
95 $retval = substr($png,0,8);
96 $ipos = 8;
97 if ($retval != "\x89PNG\x0d\x0a\x1a\x0a")
98 throw new Exception('Is not a valid PNG image');
99 // Loop through the chunks. Byte 0-3 is length, Byte 4-7 is type
100 $chunkHeader = substr($png,$ipos,8);
101 $ipos = $ipos + 8;
102 while ($chunkHeader) {
103 // Extract length and type from binary data
104 $chunk = @unpack('Nsize/a4type', $chunkHeader);
105 $skip = false;
106 if ( $chunk['type'] == $chunkType ) {
107 $data = substr($png,$ipos,$chunk['size']);
108 $sections = explode("\0", $data);
109 print_r($sections);
110 if ( $sections[0] == $key ) $skip = true;
111 }
112 // Extract the data and the CRC
113 $data = substr($png,$ipos,$chunk['size']+4);
114 $ipos = $ipos + $chunk['size'] + 4;
115 // Add in the header, data, and CRC
116 if ( ! $skip ) $retval = $retval . $chunkHeader . $data;
117 // Read next chunk header
118 $chunkHeader = substr($png,$ipos,8);
119 $ipos = $ipos + 8;
120 }
121 return $retval;
122 }
123
134 public function extractBadgeInfo($png, $key='openbadges') {
135 // Read the magic bytes and verify
136 $retval = substr($png,0,8);
137 $ipos = 8;
138 if ($retval != "\x89PNG\x0d\x0a\x1a\x0a") {
139 return false;
140 }
141
142 // Loop through the chunks. Byte 0-3 is length, Byte 4-7 is type
143 $chunkHeader = substr($png,$ipos,8);
144 $ipos = $ipos + 8;
145 while ($chunkHeader) {
146 // Extract length and type from binary data
147 $chunk = @unpack('Nsize/a4type', $chunkHeader);
148 $skip = false;
149 if ($chunk['type'] == 'tEXt') {
150 $data = substr($png,$ipos,$chunk['size']);
151 $sections = explode("\0", $data);
152 if ($sections[0] == $key) {
153 return $sections;
154 }
155 }
156 // Extract the data and the CRC
157 $data = substr($png,$ipos,$chunk['size']+4);
158 $ipos = $ipos + $chunk['size'] + 4;
159
160 // Read next chunk header
161 $chunkHeader = substr($png,$ipos,8);
162 $ipos = $ipos + 8;
163 }
164 }
165}
$result
An exception for terminatinating execution or to throw for unit testing.
Php library to Bake the PNG Images.
Definition: baker.lib.php:8
extractBadgeInfo($png, $key='openbadges')
Extracts the baked PNG info by the Key.
Definition: baker.lib.php:134
addChunk($chunkType, $key, $value)
Add a chunk by type with given key and text.
Definition: baker.lib.php:71
checkChunks($type, $check)
Checks if a key already exists in the chunk of said type.
Definition: baker.lib.php:49
__construct($contents)
Prepares file for handling metadata.
Definition: baker.lib.php:20
removeChunks($chunkType, $key, $png)
removes a chunk by type with given key and text
Definition: baker.lib.php:93
$header