This repository has been archived by the owner on Jan 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Generator.php
397 lines (331 loc) · 8.75 KB
/
Generator.php
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<?php
/**
* (c) Joffrey Demetz <joffrey.demetz@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace JDZ\Favicon;
use JDZ\Favicon\Exception\ConfigException;
use JDZ\Favicon\Exception\GeneratorException;
/**
* Favicon generator
*
* @author Joffrey Demetz <joffrey.demetz@gmail.com>
*/
class Generator
{
/**
* Input file path
*
* @var string
*/
protected $filePath;
/**
* Output folder which contains ico and png files
*
*/
protected $destPath;
/**
* Android manifest app name
*
* @var string
*/
protected $appName;
/**
* Android manifest app short name
*
* @var string
*/
protected $appShortName;
/**
* Android manifest app language
*
* @var string
*/
protected $appLanguage;
/**
* Android manifest app start url
*
* @var string
*/
protected $appStartUrl;
/**
* Android manifest app theme color
*
* @var string
*/
protected $appThemeColor;
/**
* Android manifest app background color
*
* @var string
*/
protected $appBgColor;
/**
* Android manifest app display type
*
* @var string
*/
protected $appDisplay;
/**
* Info Output buffer
*
* Used to debug what happened during process
*
* @var array
*/
protected $infoBuffer;
/**
* Include the 64x64 image in the ICO or not
*
* @var bool
*/
protected $use64Icon = false;
/**
* Include the 48x48 image in the ICO or not
*
* @var bool
*/
protected $use48Icon = false;
/**
* Exclude old apple touch images
*
* @var type
*/
protected $noOldApple = true;
/**
* Exclude manifest.json and Android images
*
* @var type
*/
protected $noAndroid = false;
/**
* Exclude Windows and IE tile images
*
* @var type
*/
protected $noMs = true;
/**
* Constructor
*
* @param array $properties Key/Value pairs
*/
public function __construct(array $properties=[])
{
foreach($properties as $key => $value){
if ( property_exists($this, $key) ){
$this->{$key} = $value;
}
}
$this->filePath = $this->cleanPath($this->filePath);
$this->destPath = $this->cleanPath($this->destPath);
$this->infoBuffer = [];
}
/**
* Get the info buffer
*
* @return array The execution buffer infos
*/
public function getInfoBuffer()
{
return $this->infoBuffer;
}
/**
* Build favicons
*
* @throws GeneratorException
*/
public function execute()
{
try {
if ( !file_exists($this->destPath) ){
mkdir($this->destPath);
}
}
catch(\Exception $e){
throw new GeneratorException('Error creating the root folder: '.$this->destPath);
}
try {
if ( !file_exists($this->cleanPath($this->destPath.'favicon/')) ){
mkdir($this->cleanPath($this->destPath.'favicon/'));
}
}
catch(\Exception $e){
throw new GeneratorException('Error creating the favicon folder in '.$this->destPath);
}
if ( '' === $this->filePath || !file_exists($this->filePath) ){
throw new GeneratorException('Input file does not exist: '.$this->filePath);
}
try {
Ico::checkDependencies();
Png::checkDependencies();
} catch(\Exception $e){
throw new GeneratorException($e->getMessage());
}
$this->generateIco();
$this->generatePngs();
if ( $this->noAndroid === false ){
$this->generateManifestJson();
}
if ( $this->noMs === false ){
$this->generateBrowserConfigXml();
}
}
/**
* Generate ICO icon
*
* @throws GeneratorException
*/
protected function generateIco()
{
$ico = new Ico();
$ico->add($this->filePath, [16, 16]);
if ( $this->use48Icon === true ){
$ico->add($this->filePath, [48, 48]);
}
if ( $this->use64Icon === true ){
$ico->add($this->filePath, [64, 64]);
}
$destPath = $this->cleanPath($this->destPath.'favicon/favicon.ico');
$ico->save($destPath);
if ( !file_exists($destPath) ){
throw new GeneratorException('Failed writing favicon/favicon.ico');
}
$this->infoBuffer[] = $destPath;
$destPath2 = $this->cleanPath($this->destPath.'favicon.ico');
$this->infoBuffer[] = $destPath2;
try {
copy($destPath, $destPath2);
}
catch(\Exception $e){
throw new GeneratorException('Failed copying favicon/favicon.ico to favicon.ico ('.$e->getMessage().')');
}
}
/**
* Generate PNG files
*
* @throws GeneratorException
*/
protected function generatePngs()
{
try {
$sizes = Config::getSizes($this->noOldApple, $this->noAndroid, $this->noMs);
}
catch(ConfigException $e){
throw new GeneratorException('Config error: '.$e->getMessage());
}
foreach($sizes as $imageName => $size){
if ( !is_array($size) ){
$size = [ $size ];
}
if ( count($size) === 1 ){
$size = [ $size[0], $size[0] ];
}
list($width, $height) = $size;
$png = new Png($this->filePath);
$imagePath = $this->cleanPath($this->destPath.'/favicon/'.$imageName);
if ( substr($imageName, 0, 6) === 'mstile' && $width !== 144 ){
$png->tile($imagePath, $this->appThemeColor, $width, $height);
}
else {
$png->square($imagePath, $width);
}
if ( !file_exists($imagePath) ){
throw new GeneratorException('Failed writing '.$imageName);
}
$this->infoBuffer[] = $imagePath;
}
}
/**
* Generate The manifest.json file
*
*/
protected function generateManifestJson()
{
$manifest = [];
if ( $this->appName !== '' ){
$manifest['name'] = $this->appName;
}
if ( $this->appShortName !== '' ){
$manifest['short_name'] = $this->appShortName;
}
if ( $this->appLanguage !== '' ){
$manifest['lang'] = $this->appLanguage;
}
if ( $this->appStartUrl !== '' ){
$manifest['start_url'] = $this->appStartUrl;
}
$manifest['icons'] = [];
if ( $this->appThemeColor !== '' ){
$manifest['theme_color'] = $this->appThemeColor;
}
if ( $this->appBgColor !== '' ){
$manifest['background_color'] = $this->appBgColor;
}
if ( $this->appDisplay !== '' ){
$manifest['display'] = $this->appDisplay;
}
foreach(array(36, 48, 72, 96, 144, 192) as $size){
$manifest['icons'][] = array(
'src' => '/favicon/android-chrome-'.$size.'x'.$size.'.png',
'sizes' => $size.'x'.$size,
'type' => 'image/png',
'density' => round($size / 48.0, 2)
);
}
$json = json_encode($manifest, JSON_PRETTY_PRINT);
$jsonFilePath = $this->cleanPath($this->destPath.'favicon/manifest.json');
$this->infoBuffer[] = $jsonFilePath;
try {
$this->writeFile($jsonFilePath, $json);
}
catch(\Exception $e){
throw new GeneratorException('Failed writing manifest.json ('.$e->getMessage().')');
}
}
/**
* Generate The browserconfig.xml file
*
*/
protected function generateBrowserConfigXml()
{
$xml = '';
$xml .= '<?xml version="1.0" encoding="utf-8"?>'."\n";
$xml .= "".'<browserconfig>'."\n";
$xml .= "\t".'<msapplication>'."\n";
$xml .= "\t\t".'<tile>'."\n";
$xml .= "\t\t\t".'<square70x70logo src="/favicon/mstile-70x70.png" />'."\n";
$xml .= "\t\t\t".'<square144x144logo src="/favicon/mstile-144x144.png "/>'."\n";
$xml .= "\t\t\t".'<square150x150logo src="/favicon/mstile-150x150.png" />'."\n";
$xml .= "\t\t\t".'<square310x310logo src="/favicon/mstile-310x310.png" />'."\n";
$xml .= "\t\t\t".'<wide310x150logo src="/favicon/mstile-310x150.png" />'."\n";
if ( $this->appThemeColor !== '' ){
$xml .= "\t\t\t".'<TileColor>'.$this->appThemeColor.'</TileColor>'."\n";
}
$xml .= "\t\t".'</tile>'."\n";
$xml .= "\t".'</msapplication>'."\n";
$xml .= '</browserconfig>'."\n";
$xmlFilePath = $this->cleanPath($this->destPath.'favicon/browserconfig.xml');
$this->infoBuffer[] = $xmlFilePath;
try {
$this->writeFile($xmlFilePath, $xml);
}
catch(\Exception $e){
throw new GeneratorException('Failed writing browserconfig.xml ('.$e->getMessage().')');
}
}
private function cleanPath(string $path): string
{
$path = trim($path);
if ( '' !== $path ){
$path = preg_replace('#[/\\\\]+#', \DIRECTORY_SEPARATOR, $path);
}
return $path;
}
private function writeFile(string $path, string $content): bool
{
file_put_contents($path, $content);
return file_exists($path);
}
}