-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPNGParser.php
More file actions
97 lines (79 loc) · 1.92 KB
/
PNGParser.php
File metadata and controls
97 lines (79 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
namespace SliveredMindOps\PNGParser;
require_once('Utils/Stream.php');
require_once('Utils/FileStream.php');
//require_once('Utils/ArrayStream.php');
require_once('Utils/Logger.php');
require_once('PNGChunk.php');
require_once('Constants.php');
if(DEBUG){
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
}
class PNGImage
{
/* Data */
public $magic;
public $chunks = [];
private $stream;
private function __construct($stream)
{
$this->stream = $stream;
$this->parse();
}
//parse png image
public static function fromFile(string $path){
$fileStream = new Utils\Streams\FileStream($path);
if(!$fileStream->canRead()){
return NULL;
}
return new PNGImage($fileStream);
}
/*@TODO
public static function fromData(array $data){
}*/
//parse file
private function parse(){
try{
$this->magic = $this->stream->read(8);
if($this->magic !== MAGIC){
Utils\Log\Logger::get()->out('Unable to verify magic');
}
while ($this->stream->getPosition() < $this->stream->getLength()) {
array_push($this->chunks, new PNGChunk($this->stream));
}
}
catch(Exception $e){
Utils\Log\Logger::get()->out('Failed to parse data. Exception:' . $e);
}
}
//create byte array from magic and chunks
public function build(){
$data = [];
$data = array_merge($data, $this->magic);
for ($i=0; $i < count($this->chunks); $i++) {
$data = array_merge($data, $this->chunks[$i]->getData());
}
return $data;
}
//write magic and chunks to file
public function write(string $path){
try{
$build_data = $this->build();
$handle = fopen($path,'wb');
$tmp_data = "";
foreach($build_data as $data) {
$tmp_data .= pack("C", $data);
}
fwrite($handle, $tmp_data);
fclose($handle);
}
catch(Exception $e){
Utils\Log\Logger::get()->out('Failed to parse data. Exception:' . $e);
}
}
//dispose object
public function dispose(){
$this->stream->dispose();
}
}