-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.php
executable file
·163 lines (163 loc) · 5.14 KB
/
scraper.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
<?php
if (PHP_SAPI !== 'cli') { die(); }
ini_set('memory_limit', '1024M');
require_once('config.php');
require_once('mysql.php');
require_once('font.php');
require_once('vendor/autoload.php');
function LogStr(string $message, int $status = 0) {
$logType = ($status === -1 ? '错误' : '信息');
$date = date('Y-m-d');
$time = date('H:i:s');
$logStr = "[{$date} {$time}][{$logType}] {$message}.\n";
echo $logStr;
}
if (!is_dir('font2')) {
LogStr('找不到 font2 目录', -1);
die();
}
$fontfiles = GetAllFontsFilename('font2');
foreach ($fontfiles as $fontfile) {
unset($font, $fontsInfo, $fontsInfoArr);
gc_collect_cycles();
$oldFontPath = $fontfile->getPathname();
if (!is_file($oldFontPath)) {
LogStr('跳过错误文件', -1);
continue;
}
$fileSize = filesize($oldFontPath);
if ($fileSize <= 0) {
LogStr('跳过空文件', -1);
continue;
}
$fontFileInfo = pathinfo($oldFontPath);
$fontExt = strtolower($fontFileInfo['extension']);
if (!in_array($fontExt, ['ttf', 'ttc', 'otf'])) {
//if ($fontExt !== 'otf') {
LogStr('跳过不支持文件', -1);
continue;
}
if ($fontExt === 'otf') {
if (!is_file('/usr/local/bin/ftcli')) {
LogStr('找不到 ftcli', -1);
die();
}
system("/usr/local/bin/ftcli converter otf2ttf -out " . escapeshellarg("{$fontFileInfo['dirname']}/") . " " . escapeshellarg($oldFontPath), $retcode);
if ($retcode > 0) {
LogStr("转换 otf 失败: {$oldFontPath}", -1);
continue;
}
LogStr("转换 otf 成功: {$oldFontPath}");
//unlink($oldFontPath);
$fontExt = 'ttf';
$oldFontPath = ($fontFileInfo['filename'] . ".ttf");
}
$fontFilename = preg_replace('/\d{10,}/', '', $fontFileInfo['filename']);
$fontsInfoArr = [];
try {
$fontsInfo = FontLib\Font::load($oldFontPath);
if ($fontsInfo instanceof FontLib\TrueType\Collection) {
while ($fontsInfo->valid()) {
$font = $fontsInfo->current();
$font->parse();
for ($i = 0; $i < 5; $i++) {
foreach (LanguageID as &$languageID) {
$fontname = $font->getFontName(3, $i, $languageID);
$fontfullname = $font->getFontFullName(3, $i, $languageID);
$fontpsname = $font->getFontPostscriptName(3, $i, $languageID);
if (empty($fontfullname)) {
if (empty($fontname)) {
if (empty($fontpsname)) {
continue;
}
$fontfullname = $fontpsname;
} else {
$fontfullname = $fontname;
}
} else if (empty($fontname)) {
$fontname = $fontfullname;
}
$fontsInfoArr[] = [$fontname, $fontfullname, $fontpsname, $font->getFontSubfamily(3, $i, $languageID)];
}
}
$fontsInfo->next();
}
} else {
$fontsInfo->parse();
for ($i = 0; $i < 5; $i++) {
foreach (LanguageID as &$languageID) {
$fontname = $fontsInfo->getFontName(3, $i, $languageID);
$fontfullname = $fontsInfo->getFontFullName(3, $i, $languageID);
$fontpsname = $fontsInfo->getFontPostscriptName(3, $i, $languageID);
if (empty($fontfullname)) {
if (empty($fontname)) {
if (empty($fontpsname)) {
continue;
}
$fontfullname = $fontpsname;
} else {
$fontfullname = $fontname;
}
} else if (empty($fontname)) {
$fontname = $fontfullname;
}
if (substr_count($fontname, '?') > 3 && substr_count($fontfullname, '?') > 3) {
continue;
}
$fontsInfoArr[] = [$fontname, $fontfullname, $fontpsname, $fontsInfo->getFontSubfamily(3, $i, $languageID)];
}
}
}
try {
$fontsInfo->close();
} catch (Throwable $e) {
}
unset($fontsInfo);
} catch (Throwable $e) {
LogStr("跳过错误字体: {$e->getMessage()}", -1);
continue;
}
$hasDupe = false;
foreach ($fontsInfoArr as $key => &$fontsInfo) {
$fontArr = DetectDuplicateFont($fontExt, $fontsInfo[0], $fontsInfo[1], $fontsInfo[3], true);
if ($fontArr[0] > 0) {
//if (preg_replace('/\d{10,}/', '', strtolower($fontArr[1])) === strtolower("{$fontFilename}.{$fontExt}")) {
$hasDupe = true;
LogStr("跳过重复字体: {$fontsInfo[0]}, {$fontsInfo[1]}, {$fontsInfo[2]}, {$fontsInfo[3]}", -1);
unset($fontsInfoArr[$key]);
continue;
//}
} else if ($fontArr[0] < 0) {
LogStr("跳过错误字体: {$fontsInfo[0]}, {$fontsInfo[1]}, {$fontsInfo[2]}, {$fontsInfo[3]}", -1);
unset($fontsInfoArr[$key]);
}
}
if (count($fontsInfoArr) <= 0) {
LogStr("跳过空字体", -1);
continue;
}
if ($hasDupe) {
$fontFilename .= time();
}
$fontFilename .= ".{$fontExt}";
$fontPath = GetMainFontPath($fontFilename);
if (!rename($oldFontPath, $fontPath)) {
LogStr("移动字体失败: {$oldFontPath} -> {$fontPath}", -1);
continue;
}
LogStr("移动字体成功: {$oldFontPath} -> {$fontPath}");
$rowID = AddFontMeta(1, $fontFilename, $fileSize, true);
if ($rowID <= 0) {
copy($fontPath, $oldFontPath);
LogStr("添加字体元数据失败: {$fontPath}", -1);
continue;
}
foreach ($fontsInfoArr as &$fontsInfo) {
if (!AddFont($rowID, $fontsInfo[0], $fontsInfo[1], $fontsInfo[2], $fontsInfo[3])) {
LogStr("添加字体失败: {$rowID}, {$fontsInfo[0]}, {$fontsInfo[1]}, {$fontsInfo[2]}, {$fontsInfo[3]}", -1);
continue;
}
LogStr("添加字体成功: {$rowID}, {$fontsInfo[0]}, {$fontsInfo[1]}, {$fontsInfo[2]}, {$fontsInfo[3]}");
}
}
?>