ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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.

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

References $contents, and $header.

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 71 of file baker.lib.php.

71 {
72
73 $chunkData = $key . "\0" . $value;
74 $crc = pack("N", crc32($chunkType . $chunkData));
75 $len = pack("N", strlen($chunkData));
76
77 $newChunk = $len . $chunkType . $chunkData . $crc;
78 $result = substr($this->_contents, 0, $this->_size - 12)
79 . $newChunk
80 . substr($this->_contents, $this->_size - 12, 12);
81 return $result;
82 }
$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 49 of file baker.lib.php.

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

References $data.

◆ 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 134 of file baker.lib.php.

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
142 // Loop through the chunks. Byte 0-3 is length, Byte 4-7 is type
143 $chunkHeader = substr($png,$ipos,8);
144 $ipos = $ipos + 8;
145 while ($chunkHeader) {
146 // Extract length and type from binary data
147 $chunk = @unpack('Nsize/a4type', $chunkHeader);
148 $skip = false;
149 if ($chunk['type'] == 'tEXt') {
150 $data = substr($png,$ipos,$chunk['size']);
151 $sections = explode("\0", $data);
152 if ($sections[0] == $key) {
153 return $sections;
154 }
155 }
156 // Extract the data and the CRC
157 $data = substr($png,$ipos,$chunk['size']+4);
158 $ipos = $ipos + $chunk['size'] + 4;
159
160 // Read next chunk header
161 $chunkHeader = substr($png,$ipos,8);
162 $ipos = $ipos + 8;
163 }
164 }

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 93 of file baker.lib.php.

93 {
94 // Read the magic bytes and verify
95 $retval = substr($png,0,8);
96 $ipos = 8;
97 if ($retval != "\x89PNG\x0d\x0a\x1a\x0a")
98 throw new Exception('Is not a valid PNG image');
99 // Loop through the chunks. Byte 0-3 is length, Byte 4-7 is type
100 $chunkHeader = substr($png,$ipos,8);
101 $ipos = $ipos + 8;
102 while ($chunkHeader) {
103 // Extract length and type from binary data
104 $chunk = @unpack('Nsize/a4type', $chunkHeader);
105 $skip = false;
106 if ( $chunk['type'] == $chunkType ) {
107 $data = substr($png,$ipos,$chunk['size']);
108 $sections = explode("\0", $data);
109 print_r($sections);
110 if ( $sections[0] == $key ) $skip = true;
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 ) $retval = $retval . $chunkHeader . $data;
117 // Read next chunk header
118 $chunkHeader = substr($png,$ipos,8);
119 $ipos = $ipos + 8;
120 }
121 return $retval;
122 }

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: