1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** Created by Gorlum 08.01.2024 19:21 */ |
4
|
|
|
|
5
|
|
|
/** @noinspection PhpUnnecessaryCurlyVarSyntaxInspection */ |
6
|
|
|
|
7
|
|
|
namespace Tools; |
8
|
|
|
|
9
|
|
|
use Exception; |
10
|
|
|
use ImageFile; |
11
|
|
|
|
12
|
|
|
define('INSIDE', true); |
13
|
|
|
|
14
|
|
|
require_once __DIR__ . '/../../classes/debug.php'; |
15
|
|
|
require_once __DIR__ . '/Sprite.php'; |
16
|
|
|
|
17
|
|
|
class Spritify { |
18
|
|
|
/** |
19
|
|
|
* @param string|string[] $dirInArray Input directory |
20
|
|
|
* @param string $dirOut Output directory |
21
|
|
|
* @param string $outName Name to use CSS/PNG file names and global CSS class |
22
|
|
|
* @param string $cssPrefix Prefix to CSS qualifier. Default '#' |
23
|
|
|
* @param string $cssSuffix Suffix to CSS qualifier. Default '' |
24
|
|
|
* @param int $scaleToPx Pixel size to scale largest side of sprite to for scale(). Default 0 - no scaling |
25
|
|
|
* @param string $relativeUrl Url relative to root where PNG sprite will reside. Default: '/design/images/' |
26
|
|
|
* @param string $layout How images should be arranged in sprite. Default: Sprite::LAYOUT_SQUARE |
27
|
|
|
* |
28
|
|
|
* @return void |
29
|
|
|
* |
30
|
|
|
* @throws Exception |
31
|
|
|
* @see Sprite::LAYOUT_SQUARE and others Sprite::LAYOUT_XXX constants |
32
|
|
|
* |
33
|
|
|
*/ |
34
|
|
|
public static function go($dirInArray, $dirOut, $outName, $cssPrefix = '#', $cssSuffix = '', $scaleToPx = 0, $relativeUrl = '/design/images/', $layout = Sprite::LAYOUT_SQUARE) { |
35
|
|
|
if (!is_array($dirInArray)) { |
36
|
|
|
$dirInArray = [$dirInArray]; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$images = []; |
40
|
|
|
foreach ($dirInArray as $dirIn) { |
41
|
|
|
print "Loading files from folder/filter `$dirIn` \n"; |
42
|
|
|
|
43
|
|
|
// $images = self::propagateImagesScanDir($dirIn); |
44
|
|
|
$images = array_merge($images, self::propagateImagesGlob($dirIn)); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
print "Processing images to `$outName`\n"; |
48
|
|
|
if (empty($images)) { |
49
|
|
|
print "No images found to process\n\n"; |
50
|
|
|
|
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$sprite = new Sprite($images, $layout, $scaleToPx); |
55
|
|
|
$sprite->generate(); |
56
|
|
|
$sprite->saveOutput($dirOut, $outName, [$cssPrefix, $cssSuffix, $outName, $relativeUrl,]); |
57
|
|
|
|
58
|
|
|
print "Folder/filter processed\n\n"; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Propagates array with image files from glob pattern (as in OS - with * and ?) |
63
|
|
|
* |
64
|
|
|
* @param string $dirIn |
65
|
|
|
* |
66
|
|
|
* @return ImageFile[]|false |
67
|
|
|
*/ |
68
|
|
|
protected static function propagateImagesGlob($dirIn) { |
69
|
|
|
if (($fullPathList = glob($dirIn, GLOB_BRACE)) === false) { |
70
|
|
|
print "Can't find directory `$dirIn`\n"; |
71
|
|
|
|
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** @var ImageFile[] $images */ |
76
|
|
|
$images = []; |
77
|
|
|
foreach ($fullPathList as $fullPath) { |
78
|
|
|
$fullPath = str_replace('\\', '/', realpath($fullPath)); |
79
|
|
|
$images = self::tryReadFile($fullPath, $images); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $images; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Propagates array with image files from folder |
87
|
|
|
* |
88
|
|
|
* @param string $dirIn |
89
|
|
|
* |
90
|
|
|
* @return ImageFile[]|false |
91
|
|
|
*/ |
92
|
|
|
protected static function propagateImagesScanDir($dirIn) { |
93
|
|
|
$dirRealPath = str_replace('\\', '/', realpath($dirIn ?: './')); |
94
|
|
|
if (!file_exists($dirRealPath) || !is_dir($dirRealPath)) { |
95
|
|
|
print "Can't find directory `$dirIn`\n"; |
96
|
|
|
|
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** @var ImageFile[] $images */ |
101
|
|
|
$images = []; |
102
|
|
|
|
103
|
|
|
foreach (scandir($dirRealPath) as $fileNameOnly) { |
104
|
|
|
if (in_array($fileNameOnly, ['.', '..',]) || is_dir($fullPath = "$dirRealPath/$fileNameOnly")) { |
105
|
|
|
continue; |
106
|
|
|
} |
107
|
|
|
$images = self::tryReadFile($fullPath, $images); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $images; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param $fullPath |
115
|
|
|
* @param ImageFile[] $images |
116
|
|
|
* |
117
|
|
|
* @return ImageFile[] |
118
|
|
|
*/ |
119
|
|
|
protected static function tryReadFile($fullPath, $images) { |
120
|
|
|
if ($image = ImageFile::read($fullPath)) { |
121
|
|
|
$images[] = $image; |
122
|
|
|
print "`$fullPath` read OK\n"; |
123
|
|
|
} else { |
124
|
|
|
print "ERROR: Can't read file `$fullPath`\n"; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $images; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
|