ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
PNGImageBaker Class Reference

Php library to Bake the PNG Images. More...

+ Collaboration diagram for PNGImageBaker:

Public Member Functions

 __construct ($contents)
 Prepares file for handling metadata. More...
 
 checkChunks ($type, $check)
 Checks if a key already exists in the chunk of said type. More...
 
 addChunk ($chunkType, $key, $value)
 Add a chunk by type with given key and text. More...
 
 removeChunks ($chunkType, $key, $png)
 removes a chunk by type with given key and text More...
 
 extractBadgeInfo ($png, $key='openbadges')
 Extracts the baked PNG info by the Key. More...
 

Private Attributes

 $_contents
 
 $_size
 
 $_chunks
 

Detailed Description

Php library to Bake the PNG Images.

Definition at line 7 of file baker.lib.php.

Constructor & Destructor Documentation

◆ __construct()

PNGImageBaker::__construct (   $contents)

Prepares file for handling metadata.

Verifies that this file is a valid PNG file. Unpacks file chunks and reads them into an array.

Parameters
string$contentsFile content as a string

Definition at line 20 of file baker.lib.php.

21 {
22 $this->_contents = $contents;
23 $png_signature = pack("C8", 137, 80, 78, 71, 13, 10, 26, 10);
24 // Read 8 bytes of PNG header and verify.
25 $header = substr($this->_contents, 0, 8);
26 if ($header != $png_signature) {
27 echo 'This is not a valid PNG image';
28 }
29 $this->_size = strlen($this->_contents);
30 $this->_chunks = array();
31 // Skip 8 bytes of IHDR image header.
32 $position = 8;
33 do {
34 $chunk = @unpack('Nsize/a4type', substr($this->_contents, $position, 8));
35 $this->_chunks[$chunk['type']][] = substr($this->_contents, $position + 8, $chunk['size']);
36 // Skip 12 bytes chunk overhead.
37 $position += $chunk['size'] + 12;
38 } while ($position < $this->_size);
39 }

Member Function Documentation

◆ addChunk()

PNGImageBaker::addChunk (   $chunkType,
  $key,
  $value 
)

Add a chunk by type with given key and text.

Parameters
string$chunkTypeChunk type, like iTXt, tEXt, etc.
string$keyKeyword that needs to be added.
string$valueCurrently an assertion URL that is added to an image metadata.
Returns
string $result File content with a new chunk as a string.

Definition at line 73 of file baker.lib.php.

74 {
75 $chunkData = $key . "\0" . $value;
76 $crc = pack("N", crc32($chunkType . $chunkData));
77 $len = pack("N", strlen($chunkData));
78
79 $newChunk = $len . $chunkType . $chunkData . $crc;
80 $result = substr($this->_contents, 0, $this->_size - 12)
81 . $newChunk
82 . substr($this->_contents, $this->_size - 12, 12);
83 return $result;
84 }
$result

References $result.

◆ checkChunks()

PNGImageBaker::checkChunks (   $type,
  $check 
)

Checks if a key already exists in the chunk of said type.

We need to avoid writing same keyword into file chunks.

Parameters
string$typeChunk type, like iTXt, tEXt, etc.
string$checkKeyword that needs to be checked.
Returns
boolean (true|false) True if file is safe to write this keyword, false otherwise.

Definition at line 50 of file baker.lib.php.

51 {
52 if (array_key_exists($type, $this->_chunks)) {
53 foreach (array_keys($this->_chunks[$type]) as $typekey) {
54 list($key, $data) = explode("\0", $this->_chunks[$type][$typekey]);
55 if (strcmp($key, $check) == 0) {
56 echo 'Key "' . $check . '" already exists in "' . $type . '" chunk.';
57 return false;
58 }
59 }
60 }
61 return true;
62 }
$type
$data
Definition: storeScorm.php:23

References $data, and $type.

◆ extractBadgeInfo()

PNGImageBaker::extractBadgeInfo (   $png,
  $key = 'openbadges' 
)

Extracts the baked PNG info by the Key.

Parameters
string$pngthe png image
string$keyKeyword that needs to be searched.
Returns
mixed - If there is an error - boolean false is returned If there is PNG information that matches the key an array is returned

Definition at line 142 of file baker.lib.php.

143 {
144 // Read the magic bytes and verify
145 $retval = substr($png, 0, 8);
146 $ipos = 8;
147 if ($retval != "\x89PNG\x0d\x0a\x1a\x0a") {
148 return false;
149 }
150
151 // Loop through the chunks. Byte 0-3 is length, Byte 4-7 is type
152 $chunkHeader = substr($png, $ipos, 8);
153 $ipos = $ipos + 8;
154 while ($chunkHeader) {
155 // Extract length and type from binary data
156 $chunk = @unpack('Nsize/a4type', $chunkHeader);
157 $skip = false;
158 if ($chunk['type'] == 'tEXt') {
159 $data = substr($png, $ipos, $chunk['size']);
160 $sections = explode("\0", $data);
161 if ($sections[0] == $key) {
162 return $sections;
163 }
164 }
165 // Extract the data and the CRC
166 $data = substr($png, $ipos, $chunk['size'] + 4);
167 $ipos = $ipos + $chunk['size'] + 4;
168
169 // Read next chunk header
170 $chunkHeader = substr($png, $ipos, 8);
171 $ipos = $ipos + 8;
172 }
173 }

References $data.

◆ removeChunks()

PNGImageBaker::removeChunks (   $chunkType,
  $key,
  $png 
)

removes a chunk by type with given key and text

Parameters
string$chunkTypeChunk type, like iTXt, tEXt, etc.
string$keyKeyword that needs to be deleted.
string$pngthe png image.
Returns
string $result New File content.

Definition at line 95 of file baker.lib.php.

96 {
97 // Read the magic bytes and verify
98 $retval = substr($png, 0, 8);
99 $ipos = 8;
100 if ($retval != "\x89PNG\x0d\x0a\x1a\x0a") {
101 throw new Exception('Is not a valid PNG image');
102 }
103 // Loop through the chunks. Byte 0-3 is length, Byte 4-7 is type
104 $chunkHeader = substr($png, $ipos, 8);
105 $ipos = $ipos + 8;
106 while ($chunkHeader) {
107 // Extract length and type from binary data
108 $chunk = @unpack('Nsize/a4type', $chunkHeader);
109 $skip = false;
110 if ($chunk['type'] == $chunkType) {
111 $data = substr($png, $ipos, $chunk['size']);
112 $sections = explode("\0", $data);
113 print_r($sections);
114 if ($sections[0] == $key) {
115 $skip = true;
116 }
117 }
118 // Extract the data and the CRC
119 $data = substr($png, $ipos, $chunk['size'] + 4);
120 $ipos = $ipos + $chunk['size'] + 4;
121 // Add in the header, data, and CRC
122 if (!$skip) {
123 $retval = $retval . $chunkHeader . $data;
124 }
125 // Read next chunk header
126 $chunkHeader = substr($png, $ipos, 8);
127 $ipos = $ipos + 8;
128 }
129 return $retval;
130 }

References $data.

Field Documentation

◆ $_chunks

PNGImageBaker::$_chunks
private

Definition at line 11 of file baker.lib.php.

◆ $_contents

PNGImageBaker::$_contents
private

Definition at line 9 of file baker.lib.php.

◆ $_size

PNGImageBaker::$_size
private

Definition at line 10 of file baker.lib.php.


The documentation for this class was generated from the following file: