123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- <?php
- class PNGImageBaker
- {
- private $_contents;
- private $_size;
- private $_chunks;
-
- public function __construct($contents)
- {
- $this->_contents = $contents;
- $png_signature = pack("C8", 137, 80, 78, 71, 13, 10, 26, 10);
-
- $header = substr($this->_contents, 0, 8);
- if ($header != $png_signature) {
- echo 'This is not a valid PNG image';
- }
- $this->_size = strlen($this->_contents);
- $this->_chunks = [];
-
- $position = 8;
- do {
- $chunk = @unpack('Nsize/a4type', substr($this->_contents, $position, 8));
- $this->_chunks[$chunk['type']][] = substr($this->_contents, $position + 8, $chunk['size']);
-
- $position += $chunk['size'] + 12;
- } while ($position < $this->_size);
- }
-
- public function checkChunks($type, $check)
- {
- if (array_key_exists($type, $this->_chunks)) {
- foreach (array_keys($this->_chunks[$type]) as $typekey) {
- list($key, $data) = explode("\0", $this->_chunks[$type][$typekey]);
- if (strcmp($key, $check) == 0) {
- echo 'Key "'.$check.'" already exists in "'.$type.'" chunk.';
- return false;
- }
- }
- }
- return true;
- }
-
- public function addChunk($chunkType, $key, $value)
- {
- $chunkData = $key."\0".$value;
- $crc = pack("N", crc32($chunkType.$chunkData));
- $len = pack("N", strlen($chunkData));
- $newChunk = $len.$chunkType.$chunkData.$crc;
- $result = substr($this->_contents, 0, $this->_size - 12)
- .$newChunk
- .substr($this->_contents, $this->_size - 12, 12);
- return $result;
- }
-
- public function removeChunks($chunkType, $key, $png)
- {
-
- $retval = substr($png, 0, 8);
- $ipos = 8;
- if ($retval != "\x89PNG\x0d\x0a\x1a\x0a") {
- throw new Exception('Is not a valid PNG image');
- }
-
- $chunkHeader = substr($png, $ipos, 8);
- $ipos = $ipos + 8;
- while ($chunkHeader) {
-
- $chunk = @unpack('Nsize/a4type', $chunkHeader);
- $skip = false;
- if ($chunk['type'] == $chunkType) {
- $data = substr($png, $ipos, $chunk['size']);
- $sections = explode("\0", $data);
- print_r($sections);
- if ($sections[0] == $key) {
- $skip = true;
- }
- }
-
- $data = substr($png, $ipos, $chunk['size'] + 4);
- $ipos = $ipos + $chunk['size'] + 4;
-
- if (!$skip) {
- $retval = $retval.$chunkHeader.$data;
- }
-
- $chunkHeader = substr($png, $ipos, 8);
- $ipos = $ipos + 8;
- }
- return $retval;
- }
-
- public function extractBadgeInfo($png, $key = 'openbadges')
- {
-
- $retval = substr($png, 0, 8);
- $ipos = 8;
- if ($retval != "\x89PNG\x0d\x0a\x1a\x0a") {
- return false;
- }
-
- $chunkHeader = substr($png, $ipos, 8);
- $ipos = $ipos + 8;
- while ($chunkHeader) {
-
- $chunk = @unpack('Nsize/a4type', $chunkHeader);
- $skip = false;
- if ($chunk['type'] == 'tEXt') {
- $data = substr($png, $ipos, $chunk['size']);
- $sections = explode("\0", $data);
- if ($sections[0] == $key) {
- return $sections;
- }
- }
-
- $data = substr($png, $ipos, $chunk['size'] + 4);
- $ipos = $ipos + $chunk['size'] + 4;
-
- $chunkHeader = substr($png, $ipos, 8);
- $ipos = $ipos + 8;
- }
- }
- }
|