baker.lib.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * Php library to Bake the PNG Images
  4. *
  5. */
  6. class PNGImageBaker
  7. {
  8. private $_contents;
  9. private $_size;
  10. private $_chunks;
  11. /**
  12. * Prepares file for handling metadata.
  13. * Verifies that this file is a valid PNG file.
  14. * Unpacks file chunks and reads them into an array.
  15. *
  16. * @param string $contents File content as a string
  17. */
  18. public function __construct($contents) {
  19. $this->_contents = $contents;
  20. $png_signature = pack("C8", 137, 80, 78, 71, 13, 10, 26, 10);
  21. // Read 8 bytes of PNG header and verify.
  22. $header = substr($this->_contents, 0, 8);
  23. if ($header != $png_signature) {
  24. echo 'This is not a valid PNG image';
  25. }
  26. $this->_size = strlen($this->_contents);
  27. $this->_chunks = array();
  28. // Skip 8 bytes of IHDR image header.
  29. $position = 8;
  30. do {
  31. $chunk = @unpack('Nsize/a4type', substr($this->_contents, $position, 8));
  32. $this->_chunks[$chunk['type']][] = substr($this->_contents, $position + 8, $chunk['size']);
  33. // Skip 12 bytes chunk overhead.
  34. $position += $chunk['size'] + 12;
  35. } while ($position < $this->_size);
  36. }
  37. /**
  38. * Checks if a key already exists in the chunk of said type.
  39. * We need to avoid writing same keyword into file chunks.
  40. *
  41. * @param string $type Chunk type, like iTXt, tEXt, etc.
  42. * @param string $check Keyword that needs to be checked.
  43. *
  44. * @return boolean (true|false) True if file is safe to write this keyword, false otherwise.
  45. */
  46. public function checkChunks($type, $check) {
  47. if (array_key_exists($type, $this->_chunks)) {
  48. foreach (array_keys($this->_chunks[$type]) as $typekey) {
  49. list($key, $data) = explode("\0", $this->_chunks[$type][$typekey]);
  50. if (strcmp($key, $check) == 0) {
  51. echo 'Key "'.$check.'" already exists in "'.$type.'" chunk.';
  52. return false;
  53. }
  54. }
  55. }
  56. return true;
  57. }
  58. /**
  59. * Add a chunk by type with given key and text
  60. *
  61. * @param string $chunkType Chunk type, like iTXt, tEXt, etc.
  62. * @param string $key Keyword that needs to be added.
  63. * @param string $value Currently an assertion URL that is added to an image metadata.
  64. *
  65. * @return string $result File content with a new chunk as a string.
  66. */
  67. public function addChunk($chunkType, $key, $value) {
  68. $chunkData = $key."\0".$value;
  69. $crc = pack("N", crc32($chunkType.$chunkData));
  70. $len = pack("N", strlen($chunkData));
  71. $newChunk = $len.$chunkType.$chunkData.$crc;
  72. $result = substr($this->_contents, 0, $this->_size - 12)
  73. . $newChunk
  74. . substr($this->_contents, $this->_size - 12, 12);
  75. return $result;
  76. }
  77. /**
  78. * removes a chunk by type with given key and text
  79. *
  80. * @param string $chunkType Chunk type, like iTXt, tEXt, etc.
  81. * @param string $key Keyword that needs to be deleted.
  82. * @param string $png the png image.
  83. *
  84. * @return string $result New File content.
  85. */
  86. public function removeChunks($chunkType, $key, $png) {
  87. // Read the magic bytes and verify
  88. $retval = substr($png, 0, 8);
  89. $ipos = 8;
  90. if ($retval != "\x89PNG\x0d\x0a\x1a\x0a")
  91. throw new Exception('Is not a valid PNG image');
  92. // Loop through the chunks. Byte 0-3 is length, Byte 4-7 is type
  93. $chunkHeader = substr($png, $ipos, 8);
  94. $ipos = $ipos + 8;
  95. while ($chunkHeader) {
  96. // Extract length and type from binary data
  97. $chunk = @unpack('Nsize/a4type', $chunkHeader);
  98. $skip = false;
  99. if ($chunk['type'] == $chunkType) {
  100. $data = substr($png, $ipos, $chunk['size']);
  101. $sections = explode("\0", $data);
  102. print_r($sections);
  103. if ($sections[0] == $key) $skip = true;
  104. }
  105. // Extract the data and the CRC
  106. $data = substr($png, $ipos, $chunk['size'] + 4);
  107. $ipos = $ipos + $chunk['size'] + 4;
  108. // Add in the header, data, and CRC
  109. if (!$skip) $retval = $retval.$chunkHeader.$data;
  110. // Read next chunk header
  111. $chunkHeader = substr($png, $ipos, 8);
  112. $ipos = $ipos + 8;
  113. }
  114. return $retval;
  115. }
  116. /**
  117. * Extracts the baked PNG info by the Key
  118. *
  119. * @param string $png the png image
  120. * @param string $key Keyword that needs to be searched.
  121. *
  122. * @return mixed - If there is an error - boolean false is returned
  123. * If there is PNG information that matches the key an array is returned
  124. *
  125. */
  126. public function extractBadgeInfo($png, $key = 'openbadges') {
  127. // Read the magic bytes and verify
  128. $retval = substr($png, 0, 8);
  129. $ipos = 8;
  130. if ($retval != "\x89PNG\x0d\x0a\x1a\x0a") {
  131. return false;
  132. }
  133. // Loop through the chunks. Byte 0-3 is length, Byte 4-7 is type
  134. $chunkHeader = substr($png, $ipos, 8);
  135. $ipos = $ipos + 8;
  136. while ($chunkHeader) {
  137. // Extract length and type from binary data
  138. $chunk = @unpack('Nsize/a4type', $chunkHeader);
  139. $skip = false;
  140. if ($chunk['type'] == 'tEXt') {
  141. $data = substr($png, $ipos, $chunk['size']);
  142. $sections = explode("\0", $data);
  143. if ($sections[0] == $key) {
  144. return $sections;
  145. }
  146. }
  147. // Extract the data and the CRC
  148. $data = substr($png, $ipos, $chunk['size'] + 4);
  149. $ipos = $ipos + $chunk['size'] + 4;
  150. // Read next chunk header
  151. $chunkHeader = substr($png, $ipos, 8);
  152. $ipos = $ipos + 8;
  153. }
  154. }
  155. }