-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.php
executable file
·39 lines (38 loc) · 1.03 KB
/
compiler.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
<?php
if (count($argv) !== 3) {
echo 'Usage: php compiler.php "/path/to/pngs" "file_name.js"'.PHP_EOL;
exit;
}
$origdir = __DIR__;
$dir = $argv[1];
$outputname = $argv[2];
chdir($dir);
$files = scandir($dir);
natsort($files);
$output = '[';
$n = 0;
foreach ($files as $file) {
if ($file != '.' && $file!='..') {
if (isPNG($file)) {
echo "Converting file ".$file.PHP_EOL;
$output .= "'".base64_encode_image($file, 'png')."',";
$n++;
}
}
}
$output = substr($output, 0, -1);
$output .= ']';
chdir($origdir);
file_put_contents($outputname, $output);
echo "File created correctly $outputname containing $n images".PHP_EOL;
function isPNG($file) {
$imageinfo = @getimagesize($file);
return $imageinfo['mime'] =='image/png';
}
function base64_encode_image ($filename=string,$filetype=string) {
if ($filename) {
$imgbinary = fread(fopen($filename, "r"), filesize($filename));
return 'data:image/' . $filetype . ';base64,' . base64_encode($imgbinary);
}
}
?>