baker.lib.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. // 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) $skip = true;
  108. }
  109. // Extract the data and the CRC
  110. $data = substr($png, $ipos, $chunk['size'] + 4);
  111. $ipos = $ipos + $chunk['size'] + 4;
  112. // Add in the header, data, and CRC
  113. if (!$skip) $retval = $retval.$chunkHeader.$data;
  114. // Read next chunk header
  115. $chunkHeader = substr($png, $ipos, 8);
  116. $ipos = $ipos + 8;
  117. }
  118. return $retval;
  119. }
  120. /**
  121. * Extracts the baked PNG info by the Key
  122. *
  123. * @param string $png the png image
  124. * @param string $key Keyword that needs to be searched.
  125. *
  126. * @return mixed - If there is an error - boolean false is returned
  127. * If there is PNG information that matches the key an array is returned
  128. *
  129. */
  130. public function extractBadgeInfo($png, $key = 'openbadges')
  131. {
  132. // Read the magic bytes and verify
  133. $retval = substr($png, 0, 8);
  134. $ipos = 8;
  135. if ($retval != "\x89PNG\x0d\x0a\x1a\x0a") {
  136. return false;
  137. }
  138. // Loop through the chunks. Byte 0-3 is length, Byte 4-7 is type
  139. $chunkHeader = substr($png, $ipos, 8);
  140. $ipos = $ipos + 8;
  141. while ($chunkHeader) {
  142. // Extract length and type from binary data
  143. $chunk = @unpack('Nsize/a4type', $chunkHeader);
  144. $skip = false;
  145. if ($chunk['type'] == 'tEXt') {
  146. $data = substr($png, $ipos, $chunk['size']);
  147. $sections = explode("\0", $data);
  148. if ($sections[0] == $key) {
  149. return $sections;
  150. }
  151. }
  152. // Extract the data and the CRC
  153. $data = substr($png, $ipos, $chunk['size'] + 4);
  154. $ipos = $ipos + $chunk['size'] + 4;
  155. // Read next chunk header
  156. $chunkHeader = substr($png, $ipos, 8);
  157. $ipos = $ipos + 8;
  158. }
  159. }
  160. }