baker.lib.php 5.9 KB

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