baker.lib.php 5.9 KB

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