1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elphin\IcoFileLoader; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* An instance of icon holds the extracted data from a .ico file |
7
|
|
|
*/ |
8
|
|
|
class Icon implements \ArrayAccess, \Countable, \Iterator |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var IconImage[] |
12
|
|
|
*/ |
13
|
|
|
private $images = []; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var int iterator position |
17
|
|
|
*/ |
18
|
|
|
private $position = 0; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Returns best icon image with dimensions matching w,h |
22
|
|
|
* @param $w |
23
|
|
|
* @param $h |
24
|
|
|
* @return IconImage|null |
25
|
|
|
*/ |
26
|
1 |
|
public function findBestForSize($w, $h) |
27
|
|
|
{ |
28
|
1 |
|
$bestBitCount = 0; |
29
|
1 |
|
$best = null; |
30
|
1 |
|
foreach ($this->images as $image) { |
31
|
1 |
|
if ($image->width == $w && $image->height == $h && ($image->bitCount > $bestBitCount)) { |
32
|
|
|
$bestBitCount = $image->bitCount; |
33
|
|
|
$best = $image; |
34
|
|
|
} |
35
|
1 |
|
} |
36
|
1 |
|
return $best; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Finds the highest quality image in the icon |
41
|
|
|
* @return IconImage |
42
|
|
|
*/ |
43
|
1 |
|
public function findBest() |
44
|
|
|
{ |
45
|
1 |
|
$bestBitCount = 0; |
46
|
1 |
|
$bestWidth = 0; |
47
|
1 |
|
$best = null; |
48
|
1 |
|
foreach ($this->images as $image) { |
49
|
1 |
|
if (($image->width > $bestWidth) || |
50
|
|
|
(($image->width == $bestWidth) && ($image->bitCount > $bestBitCount)) |
51
|
1 |
|
) { |
52
|
1 |
|
$bestWidth = $image->width; |
53
|
1 |
|
$bestBitCount = $image->bitCount; |
54
|
1 |
|
$best = $image; |
55
|
1 |
|
} |
56
|
1 |
|
} |
57
|
1 |
|
return $best; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Count number of images in the icon |
62
|
|
|
* As this class implements Countable you can simply use count($icon) if you desire |
63
|
|
|
* @return int |
64
|
|
|
*/ |
65
|
19 |
|
public function count() |
66
|
|
|
{ |
67
|
19 |
|
return count($this->images); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set an icon |
72
|
|
|
* This is an implementation of ArrayAccess allowing you to do $icon[$x]=$image |
73
|
|
|
* @param integer $offset |
74
|
|
|
* @param IconImage $value |
75
|
|
|
*/ |
76
|
20 |
|
public function offsetSet($offset, $value) |
77
|
|
|
{ |
78
|
20 |
|
if (!$value instanceof IconImage) { |
79
|
1 |
|
throw new \InvalidArgumentException('Can only add IconImage instances to an Icon'); |
80
|
|
|
} |
81
|
19 |
|
if (is_null($offset)) { |
82
|
19 |
|
$this->images[] = $value; |
83
|
19 |
|
} else { |
84
|
1 |
|
$this->images[$offset] = $value; |
85
|
|
|
} |
86
|
19 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Check if image with particular index exists |
90
|
|
|
* This is an implementation of ArrayAccess allowing you to do isset($icon[$x]) |
91
|
|
|
* @param integer $offset |
92
|
|
|
* @return boolean |
93
|
|
|
*/ |
94
|
1 |
|
public function offsetExists($offset) |
95
|
|
|
{ |
96
|
1 |
|
return isset($this->images[$offset]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Remove image from icon |
101
|
|
|
* This is an implementation of ArrayAccess allowing you to do unset($icon[$x]) |
102
|
|
|
* @param integer $offset |
103
|
|
|
* @return boolean |
104
|
|
|
*/ |
105
|
1 |
|
public function offsetUnset($offset) |
106
|
|
|
{ |
107
|
1 |
|
unset($this->images[$offset]); |
108
|
1 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get image from icon |
112
|
|
|
* This is an implementation of ArrayAccess allowing you to do $image = $icon[$x] |
113
|
|
|
* @param integer $offset |
114
|
|
|
* @return IconImage |
115
|
|
|
*/ |
116
|
18 |
|
public function offsetGet($offset) |
117
|
|
|
{ |
118
|
18 |
|
return isset($this->images[$offset]) ? $this->images[$offset] : null; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Implements \Iterator allowing foreach($icon as $image){} |
123
|
|
|
*/ |
124
|
1 |
|
public function rewind() |
125
|
|
|
{ |
126
|
1 |
|
$this->position = 0; |
127
|
1 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Implements \Iterator allowing foreach($icon as $image){} |
131
|
|
|
*/ |
132
|
1 |
|
public function current() |
133
|
|
|
{ |
134
|
1 |
|
return $this->images[$this->position]; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Implements \Iterator allowing foreach($icon as $image){} |
139
|
|
|
*/ |
140
|
|
|
public function key() |
141
|
|
|
{ |
142
|
|
|
return $this->position; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Implements \Iterator allowing foreach($icon as $image){} |
147
|
|
|
*/ |
148
|
1 |
|
public function next() |
149
|
|
|
{ |
150
|
1 |
|
++$this->position; |
151
|
1 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Implements \Iterator allowing foreach($icon as $image){} |
155
|
|
|
*/ |
156
|
1 |
|
public function valid() |
157
|
|
|
{ |
158
|
1 |
|
return isset($this->images[$this->position]); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|