1 | <?php |
||
9 | class IcoFileService |
||
10 | { |
||
11 | /** |
||
12 | * @var ParserInterface |
||
13 | */ |
||
14 | protected $parser; |
||
15 | |||
16 | /** |
||
17 | * @var RendererInterface |
||
18 | */ |
||
19 | protected $renderer; |
||
20 | |||
21 | /** |
||
22 | * IcoFileService constructor |
||
23 | * |
||
24 | * You can inject alternative implementations of the renderer or parser, but for most |
||
25 | * typical uses, you can accept the defaults |
||
26 | * |
||
27 | * @param RendererInterface|null $renderer |
||
28 | * @param ParserInterface|null $parser |
||
29 | */ |
||
30 | 6 | public function __construct(RendererInterface $renderer = null, ParserInterface $parser = null) |
|
35 | |||
36 | /** |
||
37 | * This is a useful one-stop function for obtaining the best possible icon of a particular size from an .ico file |
||
38 | * |
||
39 | * As icons are often hand-crafted to look good at particular sizes, this will try to use the best quality image |
||
40 | * in the icon at the required size. If it can't be found, then it will resize the largest icon it can find. |
||
41 | * |
||
42 | * This will either return a valid image, or will throw an \InvalidArgumentException in the event of the file |
||
43 | * being unreadable. |
||
44 | * |
||
45 | * @param string $dataOrFile either a filename to a .ico file, or binary data from an .ico file in a string |
||
46 | * @param integer $w desired width. The class tries to locate the best quality image at this size, but |
||
47 | * if not found, the largest available icon will be used and resized to fit |
||
48 | * @param integer $h desired height - as icons are usually square, this should be same as $w |
||
49 | * @param array $opts array of renderer options. The built in renderer supports an optional 'background' |
||
50 | * elemen in this array. Normally, the result will use alpha transparency, but you can |
||
51 | * pass a hex colour to choose the colour of the transparent area instead, e.g. |
||
52 | * ['background=>'#ffffff'] for a white background |
||
53 | * @return mixed the built in renderer will return a gd image resource, which you could save with |
||
54 | * the gd function imagepng(), for example. If you swap in an alternative renderer, |
||
55 | * the result is whatever that renderer returns. |
||
56 | * @throws \InvalidArgumentException if file is not found or is invalid |
||
57 | */ |
||
58 | 1 | public function extractIcon($dataOrFile, $w, $h, array $opts = null) |
|
59 | { |
||
60 | 1 | $icon = $this->from($dataOrFile); |
|
61 | 1 | $image = $icon->findBestForSize($w, $h); |
|
62 | 1 | if (!$image) { |
|
63 | //nothing at our required size, so we'll find the highest quality icon |
||
64 | 1 | $image = $icon->findBest(); |
|
65 | 1 | } |
|
66 | 1 | return $this->renderImage($image, $w, $h, $opts); |
|
|
|||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Renders an IconImage at a desired width and height |
||
71 | * |
||
72 | * @param IconImage $image image obtained from an Icon object |
||
73 | * @param integer $w desired width - if null, width of IconImage is used |
||
74 | * @param integer $h desired height - if null, height of IconImage is used |
||
75 | * @param array $opts array of renderer options. The built in renderer supports an optional 'background' |
||
76 | * elemen in this array. Normally, the result will use alpha transparency, but you can |
||
77 | * pass a hex colour to choose the colour of the transparent area instead, e.g. |
||
78 | * ['background=>'#ffffff'] for a white background |
||
79 | * @return mixed the built in renderer will return a gd image resource, which you could save with |
||
80 | * the gd function imagepng(), for example. If you swap in an alternative renderer, |
||
81 | * the result is whatever that renderer returns. |
||
82 | * @throws \InvalidArgumentException if IconImage or options are invalid |
||
83 | */ |
||
84 | 1 | public function renderImage(IconImage $image, $w = null, $h = null, array $opts = null) |
|
91 | |||
92 | /** |
||
93 | * Parses a .ico file from a pathname or binary data string and return an Icon object |
||
94 | * |
||
95 | * This is a useful lower level member which can be used to inspect an icon before |
||
96 | * rendering a particular image within it with renderImage |
||
97 | * |
||
98 | * @param string $dataOrFile either filename or binary data |
||
99 | * @return Icon |
||
100 | * @throws \InvalidArgumentException if file is not found or invalid |
||
101 | */ |
||
102 | 4 | public function from($dataOrFile) |
|
109 | |||
110 | /** |
||
111 | * Loads icon from file |
||
112 | * @param string $file filename or URL (if fopen wrappers installed) |
||
113 | * @return Icon |
||
114 | * @throws \InvalidArgumentException if file is not found or invalid |
||
115 | */ |
||
116 | 4 | public function fromFile($file) |
|
124 | |||
125 | /** |
||
126 | * Loads icon from string |
||
127 | * @param string $data binary data string containing a .ico file |
||
128 | * @return Icon |
||
129 | * @throws \InvalidArgumentException if file is not found or invalid |
||
130 | */ |
||
131 | 2 | public function fromString($data) |
|
135 | } |
||
136 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: