1
|
|
|
<?php |
2
|
|
|
namespace Xls; |
3
|
|
|
|
4
|
|
|
class Bitmap |
5
|
|
|
{ |
6
|
|
|
const HEADER_SIZE = 0x36; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @var string |
10
|
|
|
*/ |
11
|
|
|
protected $filePath; |
12
|
|
|
|
13
|
|
|
/** Holds raw file data |
14
|
|
|
* @var mixed |
15
|
|
|
*/ |
16
|
|
|
protected $data; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var mixed |
20
|
|
|
*/ |
21
|
|
|
protected $header; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $filePath |
25
|
|
|
*/ |
26
|
|
|
public function __construct($filePath) |
27
|
|
|
{ |
28
|
|
|
$this->filePath = $filePath; |
29
|
|
|
|
30
|
|
|
$this->read(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return string |
35
|
|
|
* @throws \Exception |
36
|
|
|
*/ |
37
|
|
|
protected function read() |
38
|
|
|
{ |
39
|
|
|
$this->data = @file_get_contents($this->filePath, FILE_BINARY); |
40
|
|
|
if ($this->data === false) { |
41
|
|
|
throw new \Exception("Couldn't import $this->filePath"); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->header = substr($this->data, 0, self::HEADER_SIZE); |
45
|
|
|
|
46
|
|
|
$this->validate(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @throws \Exception |
51
|
|
|
*/ |
52
|
|
|
protected function validate() |
53
|
|
|
{ |
54
|
|
|
if (strlen($this->data) <= self::HEADER_SIZE) { |
55
|
|
|
throw new \Exception("$this->filePath doesn't contain enough data"); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($this->getIdent() != "BM") { |
59
|
|
|
throw new \Exception("$this->filePath doesn't appear to be a valid bitmap image"); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($this->getColorDepth() != 24) { |
63
|
|
|
throw new \Exception("$this->filePath isn't a 24bit true color bitmap"); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($this->getPlanesCount() != 1) { |
67
|
|
|
throw new \Exception("$this->filePath: only 1 plane supported in bitmap image"); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($this->getCompression() != 0) { |
71
|
|
|
throw new \Exception("$this->filePath: compression not supported in bitmap image"); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return mixed |
77
|
|
|
*/ |
78
|
|
|
protected function getIdent() |
79
|
|
|
{ |
80
|
|
|
$identity = unpack("A2ident", $this->header); |
81
|
|
|
|
82
|
|
|
return $identity['ident']; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return mixed |
87
|
|
|
*/ |
88
|
|
|
public function getWidth() |
89
|
|
|
{ |
90
|
|
|
$result = unpack("Vwidth", substr($this->header, 18, 4)); |
91
|
|
|
|
92
|
|
|
return $result['width']; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return mixed |
97
|
|
|
*/ |
98
|
|
|
public function getHeight() |
99
|
|
|
{ |
100
|
|
|
$result = unpack("Vheight", substr($this->header, 22, 4)); |
101
|
|
|
|
102
|
|
|
return $result['height']; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return mixed |
107
|
|
|
*/ |
108
|
|
|
public function getPlanesCount() |
109
|
|
|
{ |
110
|
|
|
$result = unpack("v1", substr($this->header, 26, 2)); |
111
|
|
|
|
112
|
|
|
return $result[1]; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return mixed |
117
|
|
|
*/ |
118
|
|
|
public function getColorDepth() |
119
|
|
|
{ |
120
|
|
|
$result = unpack("v1", substr($this->header, 28, 2)); |
121
|
|
|
|
122
|
|
|
return $result[1]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return mixed |
127
|
|
|
*/ |
128
|
|
|
public function getCompression() |
129
|
|
|
{ |
130
|
|
|
$result = unpack("V1", substr($this->header, 30, 4)); |
131
|
|
|
|
132
|
|
|
return $result[1]; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public function getDataWithoutHeader() |
139
|
|
|
{ |
140
|
|
|
return substr($this->data, self::HEADER_SIZE); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|