-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpack.php
More file actions
executable file
·112 lines (97 loc) · 3.96 KB
/
Copy pathpack.php
File metadata and controls
executable file
·112 lines (97 loc) · 3.96 KB
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
#!/usr/bin/env php
<?php
use SpritePack\Image;
use SpritePack\Packer;
require __DIR__ . '/vendor/autoload.php';
function ag($array, $key, $default=null) {
return array_key_exists($key, $array) ? $array[$key] : $default;
}
class Program {
private static function imageCreateFromAny($filename) {
return imagecreatefromstring(file_get_contents($filename));
}
private static function imageCreateTrueColorTransparent($width, $height) {
$im = imagecreatetruecolor($width, $height);
imagesavealpha($im, true);
$transColor = imagecolorallocatealpha($im, 0, 0, 0, 127); // 127 indicates completely transparent
imagefill($im, 0, 0, $transColor);
return $im;
}
public static function main($cliArgs) {
$options = [];
$args = [];
$command = array_shift($cliArgs);
foreach($cliArgs as $arg) {
if($arg[0] === '-') {
$options[ltrim($arg,'-')] = true;
} else {
$args[] = $arg;
}
}
if(count($args) < 1) {
fwrite(STDERR, "usage: $command <input-dir> [output-file]\n");
exit(1);
}
$sourceDir = ag($args,0,'images');
$outFile = ag($args,1,'spritesheet.png');
/** @var Image[] $images */
$images = [];
$di = new DirectoryIterator($sourceDir);
foreach($di as $f) {
/** @var $f DirectoryIterator */
if(!$f->isFile()) continue;
$filePath = $f->getPathname();
list($w, $h) = getimagesize($filePath);
if(!$w || !$h) {
fwrite(STDERR,"could not get width/height for $filePath -- skipping\n");
continue;
}
$images[] = new Image($filePath, $w, $h);
}
usort($images, function ($a, $b) {
// return max($a->width, $a->height) < max($b->width, $b->height) ? 1 : -1;
if($a->width > $a->height) {
$aMax = $a->width;
$aMin = $a->height;
} else {
$aMin = $a->width;
$aMax = $a->height;
}
if($b->width > $b->height) {
$bMax = $b->width;
$bMin = $b->height;
} else {
$bMin = $b->width;
$bMax = $b->height;
}
if($aMax > $bMax) return -1;
if($aMax < $bMax) return 1;
if($aMin > $bMin) return -1;
if($aMin < $bMin) return 1;
return strnatcasecmp($a->filePath, $b->filePath);
});
$packer = new Packer();
$packer->fit($images);
$spritesheet = self::imageCreateTrueColorTransparent($packer->root->width, $packer->root->height);
if(ag($options,'test')) {
$black = imagecolorallocate($spritesheet, 0, 0, 0);
foreach($images as $i => $img) {
$r = mt_rand(0, 255);
$g = mt_rand(0, 255);
$b = mt_rand(0, 255);
imagefilledrectangle($spritesheet, $img->fit->x, $img->fit->y, $img->fit->x + $img->width - 1, $img->fit->y + $img->height - 1, imagecolorallocatealpha($spritesheet, $r, $g, $b, 64));
imagerectangle($spritesheet, $img->fit->x, $img->fit->y, $img->fit->x + $img->width - 1, $img->fit->y + $img->height - 1, imagecolorallocate($spritesheet, $r, $g, $b));
imagestring($spritesheet, 5, $img->fit->x + 2, $img->fit->y + 2, $i, $black);
}
} else {
foreach($images as $i => $img) {
imagecopy($spritesheet, self::imageCreateFromAny($img->filePath), $img->fit->x, $img->fit->y, 0, 0, $img->width, $img->height);
}
}
imagepng($spritesheet, $outFile);
echo "packed ".number_format(count($images))." images into $outFile\n";
}
}
if(php_sapi_name() === 'cli' && __FILE__ == realpath($argv[0])) {
Program::main($argv);
}