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 |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var IconImage[] |
12
|
|
|
*/ |
13
|
|
|
private $images = []; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Returns best icon image with dimensions matching w,h |
17
|
|
|
* @param $w |
18
|
|
|
* @param $h |
19
|
|
|
* @return IconImage|null |
20
|
|
|
*/ |
21
|
1 |
|
public function findBestForSize($w, $h) |
22
|
|
|
{ |
23
|
1 |
|
$bestBitCount = 0; |
24
|
1 |
|
$best = null; |
25
|
1 |
|
foreach ($this->images as $image) { |
26
|
1 |
|
if ($image->width == $w && $image->height == $h && ($image->bitCount > $bestBitCount)) { |
27
|
|
|
$bestBitCount = $image->bitCount; |
28
|
|
|
$best = $image; |
29
|
|
|
} |
30
|
1 |
|
} |
31
|
1 |
|
return $best; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Finds the highest quality image in the icon |
36
|
|
|
* @return IconImage|null |
37
|
|
|
*/ |
38
|
1 |
|
public function findBest() |
39
|
|
|
{ |
40
|
1 |
|
$bestBitCount = 0; |
41
|
1 |
|
$bestWidth=0; |
42
|
1 |
|
$best = null; |
43
|
1 |
|
foreach ($this->images as $image) { |
44
|
1 |
|
if (($image->width > $bestWidth) || |
45
|
|
|
(($image->width == $bestWidth) && ($image->bitCount > $bestBitCount)) |
46
|
1 |
|
) { |
47
|
1 |
|
$bestWidth = $image->width; |
48
|
1 |
|
$bestBitCount = $image->bitCount; |
49
|
1 |
|
$best = $image; |
50
|
1 |
|
} |
51
|
1 |
|
} |
52
|
1 |
|
return $best; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Count number of images in the icon |
57
|
|
|
* As this class implements Countable you can simply use count($icon) if you desire |
58
|
|
|
* @return int |
59
|
|
|
*/ |
60
|
15 |
|
public function count() |
61
|
|
|
{ |
62
|
15 |
|
return count($this->images); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Set an icon |
67
|
|
|
* This is an implementation of ArrayAccess allowing you to do $icon[$x]=$image |
68
|
|
|
* @param integer $offset |
69
|
|
|
* @param IconImage $value |
70
|
|
|
*/ |
71
|
15 |
|
public function offsetSet($offset, $value) |
72
|
|
|
{ |
73
|
15 |
|
if (!$value instanceof IconImage) { |
74
|
|
|
throw new \InvalidArgumentException('Can only add IconImage instances to an Icon'); |
75
|
|
|
} |
76
|
15 |
|
if (is_null($offset)) { |
77
|
15 |
|
$this->images[] = $value; |
78
|
15 |
|
} else { |
79
|
|
|
$this->images[$offset] = $value; |
80
|
|
|
} |
81
|
15 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Check if image with particular index exists |
85
|
|
|
* This is an implementation of ArrayAccess allowing you to do isset($icon[$x]) |
86
|
|
|
* @param integer $offset |
87
|
|
|
* @return boolean |
88
|
|
|
*/ |
89
|
|
|
public function offsetExists($offset) |
90
|
|
|
{ |
91
|
|
|
return isset($this->images[$offset]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Remove image from icon |
96
|
|
|
* This is an implementation of ArrayAccess allowing you to do unset($icon[$x]) |
97
|
|
|
* @param integer $offset |
98
|
|
|
* @return boolean |
99
|
|
|
*/ |
100
|
|
|
public function offsetUnset($offset) |
101
|
|
|
{ |
102
|
|
|
unset($this->images[$offset]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get image from icon |
107
|
|
|
* This is an implementation of ArrayAccess allowing you to do $image = $icon[$x] |
108
|
|
|
* @param integer $offset |
109
|
|
|
* @return IconImage |
110
|
|
|
*/ |
111
|
15 |
|
public function offsetGet($offset) |
112
|
|
|
{ |
113
|
15 |
|
return isset($this->images[$offset]) ? $this->images[$offset] : null; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|