|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
if(!extension_loaded('gd')) { die('GD ext is required to run this!'); } |
|
4
|
|
|
|
|
5
|
|
|
chdir(__DIR__ . '/../'); //Just to make things easier, change dir to project root. |
|
6
|
|
|
const ASSET_FOLDER = 'public/assets'; |
|
7
|
|
|
const ICON_FOLDER = ASSET_FOLDER.'/img/site_icons'; |
|
8
|
|
|
|
|
9
|
|
|
/** @noinspection AutoloadingIssuesInspection */ |
|
10
|
|
|
class Spritesheet { |
|
11
|
|
|
private $fileList; |
|
12
|
|
|
private $less; |
|
13
|
|
|
|
|
14
|
|
|
public function __construct() { |
|
15
|
|
|
$this->fileList = $this->getFileList(); |
|
16
|
|
|
$this->less = ''; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function generate() : void { |
|
20
|
|
|
$this->generateSpritesheet(); |
|
21
|
|
|
$this->modifySiteLESS(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
private function generateSpritesheet() : void { |
|
25
|
|
|
$width = (count($this->fileList) * (16 + /* padding */ 2)) - 2; |
|
26
|
|
|
|
|
27
|
|
|
$sheetImage = imagecreatetruecolor($width, 16); |
|
28
|
|
|
imagealphablending($sheetImage, FALSE); |
|
29
|
|
|
imagesavealpha($sheetImage, TRUE); |
|
30
|
|
|
|
|
31
|
|
|
imagefill($sheetImage,0,0,0x7fff0000); |
|
32
|
|
|
|
|
33
|
|
|
$x = 0; |
|
34
|
|
|
foreach ($this->fileList as $filename) { |
|
35
|
|
|
$siteImage = imagecreatefrompng(ICON_FOLDER. "/{$filename}"); |
|
36
|
|
|
imagealphablending($siteImage, TRUE); |
|
37
|
|
|
|
|
38
|
|
|
$dst_x = ((16 + 2) * $x); |
|
39
|
|
|
imagecopyresampled($sheetImage, $siteImage, $dst_x, 0, 0, 0, 16, 16, 16, 16); |
|
40
|
|
|
|
|
41
|
|
|
$this->generateLESS($filename, $dst_x); |
|
42
|
|
|
$x++; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
imagepng($sheetImage, ASSET_FOLDER . '/img/sites.png'); |
|
46
|
|
|
say('Updated spritesheet!'); |
|
47
|
|
|
} |
|
48
|
|
|
private function generateLESS(string $filename, int $dst_x) : void { |
|
49
|
|
|
$parts = pathinfo($filename); |
|
50
|
|
|
|
|
51
|
|
|
$this->less .= "\n". |
|
52
|
|
|
" &.sprite-{$parts['filename']} {\n". |
|
53
|
|
|
" .stitches-sprite(-{$dst_x}px);\n". |
|
54
|
|
|
" }\n"; |
|
55
|
|
|
} |
|
56
|
|
|
private function modifySiteLESS() : void { |
|
57
|
|
|
$newSiteLESS = trim($this->less); |
|
58
|
|
|
|
|
59
|
|
|
$icons_file = ASSET_FOLDER.'/less/modules/icons.less'; |
|
60
|
|
|
$oldLESS = file_get_contents($icons_file); |
|
61
|
|
|
if(preg_match('/\.sprite-site.*\@cache-version: (\d+);/s', $oldLESS, $cvMatches)) { |
|
62
|
|
|
$cacheVersion = ((int) $cvMatches[1]) + 1; |
|
63
|
|
|
|
|
64
|
|
|
$newLESS = preg_replace('/\.sprite-site.*/s', '',$oldLESS); |
|
65
|
|
|
$newLESS .= ''. |
|
66
|
|
|
".sprite-site {\n". |
|
67
|
|
|
" .sprite();\n". |
|
68
|
|
|
" @cache-version: {$cacheVersion};\n". |
|
69
|
|
|
" background: url('../../img/sites.@{cache-version}.png') no-repeat;\n\n". |
|
70
|
|
|
" {$newSiteLESS}\n". |
|
71
|
|
|
"}\n"; |
|
72
|
|
|
|
|
73
|
|
|
file_put_contents($icons_file, $newLESS); |
|
74
|
|
|
say('Updated LESS!'); |
|
75
|
|
|
} else { |
|
76
|
|
|
die("Can't find cache-version?"); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
private function getFileList() : array { |
|
81
|
|
|
return array_diff(scandir(ICON_FOLDER, SCANDIR_SORT_NONE), array('..', '.')); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** @noinspection PhpFunctionNamingConventionInspection */ |
|
86
|
|
|
function say(string $text = '') { print "{$text}\n"; } |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
$Spritesheet = new Spritesheet(); |
|
89
|
|
|
$Spritesheet->generate(); |
|
90
|
|
|
|
This check looks for functions that have already been defined in other files.
Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the
@ignoreannotation.See also the PhpDoc documentation for @ignore.