-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsurrogator.php
executable file
·337 lines (305 loc) · 8.94 KB
/
surrogator.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/env php
<?php
/**
* Tool to create avatar images in different sizes.
*
* Part of Surrogator - a simple libravatar avatar image server
*
* PHP version 5
*
* @category Tools
* @package Surrogator
* @author Christian Weiske <cweiske@cweiske.de>
* @license http://www.gnu.org/licenses/agpl.html AGPLv3 or later
* @link https://sourceforge.net/p/surrogator/
*/
namespace surrogator;
$cfgFile = __DIR__ . '/data/surrogator.config.php';
if (!file_exists($cfgFile)) {
$cfgFile = '/etc/surrogator.config.php';
if (!file_exists($cfgFile)) {
logErr(
"Configuration file does not exist.\n"
. "Copy data/surrogator.config.php.dist to data/surrogator.config.php"
);
exit(2);
}
}
require $cfgFile;
array_shift($argv);
$files = array();
foreach ($argv as $arg) {
if ($arg == '-v' || $arg == '--verbose') {
++$logLevel;
} else if ($arg == '-vv') {
$logLevel += 2;
} else if ($arg == '-q' || $arg == '--quiet') {
$logLevel = 0;
} else if ($arg == '-f' || $arg == '--force') {
$forceUpdate = true;
} else if ($arg == '-h' || $arg == '--help') {
showHelp();
exit(4);
} else if ($arg == '--version') {
echo "surrogator 0.0.1\n";
exit();
} else if (file_exists($arg)) {
$files[] = $arg;
} else {
logErr('Unknown argument: ' . $arg);
exit(3);
}
}
/**
* Echos the --help screen.
*
* @return void
*/
function showHelp()
{
echo <<<HLP
Usage: php surrogator.php [options] [filename(s)]
surrogator - a simple libravatar server
Put files in raw/ dir and run surrogator.php to generate different sizes
Options:
-h, --help Show help
-v, --verbose Be verbose (more log messages, also -vv)
-q, --quiet Be quiet (no log messages)
-f, --force Force update of all files
--version Show program version
filenames One or several files whose small images shall get generated.
If none given, all will be checked
HLP;
}
if (!isset($rawDir)) {
logErr('$rawDir not set');
exit(1);
}
if (!isset($varDir)) {
logErr('$varDir not set');
exit(1);
}
if (!isset($sizes)) {
logErr('$sizes not set');
exit(1);
}
if (!isset($maxSize)) {
logErr('$maxSize not set');
exit(1);
}
if (!isset($logLevel)) {
logErr('$logLevel not set');
exit(1);
}
if (!is_dir($varDir . '/square')) {
log('creating square dir: ' . $varDir . '/square');
if (!mkdir($varDir . '/square', 0755, true)) {
logErr('cannot create square dir');
exit(5);
}
}
log('sizes: ' . implode(', ', $sizes), 2);
foreach ($sizes as $size) {
if (!is_dir($varDir . '/' . $size)) {
log('creating size dir: ' . $varDir . '/' . $size);
mkdir($varDir . '/' . $size, 0755);
}
}
foreach (array('mm.png', 'default.png') as $resFile) {
if (!file_exists($rawDir . '/' . $resFile)) {
log($resFile . ' missing, copying it from res/', 2);
copy($resDir . '/' . $resFile, $rawDir . '/' . $resFile);
}
}
foreach (array('index.html', 'robots.txt', 'favicon.ico') as $resFile) {
if (!file_exists($wwwDir . '/' . $resFile) && is_writable($wwwDir)) {
log('no www/' . $resFile . ' found, copying default over', 1);
copy($resDir . '/www/' . $resFile, $wwwDir . '/' . $resFile);
}
}
if (count($files)) {
$fileInfos = array();
foreach ($files as $file) {
$fileInfos[] = new \SplFileInfo($file);
}
} else {
$fileInfos = new \RegexIterator(
new \DirectoryIterator($rawDir),
'#^.+\.(png|jpg|svg|svgz)$#'
);
}
foreach ($fileInfos as $fileInfo) {
$origPath = $fileInfo->getPathname();
$fileName = $fileInfo->getFilename();
$ext = strtolower(substr($fileName, strrpos($fileName, '.') + 1));
$squarePath = $varDir . '/square/'
. substr($fileName, 0, -strlen($ext)) . 'png';
log('processing ' . $fileName, 1);
if (imageUptodate($origPath, $squarePath)) {
log(' image up to date', 2);
continue;
}
if (!createSquare($origPath, $ext, $squarePath, $maxSize)) {
continue;
}
if ($fileName == 'default.png') {
$md5 = $sha256 = 'default';
} else if ($fileName == 'mm.png') {
$md5 = $sha256 = 'mm';
} else {
list($md5, $sha256) = getHashes($fileName);
}
log(' creating sizes for ' . $fileName, 2);
log(' md5: ' . $md5, 3);
log(' sha256: ' . $sha256, 3);
$imgSquare = imagecreatefrompng($squarePath);
foreach ($sizes as $size) {
log(' size ' . $size, 3);
$sizePathMd5 = $varDir . '/' . $size . '/' . $md5 . '.png';
$sizePathSha256 = $varDir . '/' . $size . '/' . $sha256 . '.png';
$imgSize = imagecreatetruecolor($size, $size);
imagealphablending($imgSize, false);
imagefilledrectangle(
$imgSize, 0, 0, $size - 1, $size - 1,
imagecolorallocatealpha($imgSize, 0, 0, 0, 127)
);
imagecopyresampled(
$imgSize, $imgSquare,
0, 0, 0, 0,
$size, $size, $maxSize, $maxSize
);
imagesavealpha($imgSize, true);
imagepng($imgSize, $sizePathMd5);
imagepng($imgSize, $sizePathSha256);
imagedestroy($imgSize);
}
imagedestroy($imgSquare);
}
/**
* Create and return md5 and sha256 hashes from a filename.
*
* @param string $fileName filename without path, e.g. "foo@example.org.png"
*
* @return array Array with 2 values: md5 and sha256 hash
*/
function getHashes($fileName)
{
//OpenIDs have their slashes "/" url-encoded
$fileName = rawurldecode($fileName);
$fileNameNoExt = pathinfo($fileName, PATHINFO_FILENAME);
$emailAddress = trim(strtolower($fileNameNoExt));
return array(
md5($emailAddress), hash('sha256', $emailAddress)
);
}
/**
* Creates the square image from the given image in maximum size.
* Scales the image up or down and makes the non-covered parts transparent.
*
* @param string $origPath Full path to original image
* @param string $ext File extension ("jpg" or "png")
* @param string $targetPath Full path to target image file
* @param integer $maxSize Maxium image size the server supports
*
* @return boolean True if all went well, false if there was an error
*/
function createSquare($origPath, $ext, $targetPath, $maxSize)
{
if ($ext == 'png') {
$imgOrig = imagecreatefrompng($origPath);
} else if ($ext == 'jpg' || $ext == 'jpeg') {
$imgOrig = imagecreatefromjpeg($origPath);
} else if ($ext == 'svg' || $ext == 'svgz') {
$imagickImg = new \Imagick();
$imagickImg->setBackgroundColor(new \ImagickPixel('transparent'));
$imagickImg->readImage($origPath);
$imagickImg->setImageFormat('png32');
$imgOrig = imagecreatefromstring($imagickImg->getImageBlob());
} else {
//unsupported format
logErr('Unsupported image format: ' . $origPath);
return false;
}
if ($imgOrig === false) {
logErr('Error loading image file: ' . $origPath);
return false;
}
$imgSquare = imagecreatetruecolor($maxSize, $maxSize);
imagealphablending($imgSquare, false);
imagefilledrectangle(
$imgSquare, 0, 0, $maxSize - 1, $maxSize - 1,
imagecolorallocatealpha($imgSquare, 0, 0, 0, 127)
);
imagealphablending($imgSquare, true);
$oWidth = imagesx($imgOrig);
$oHeight = imagesy($imgOrig);
if ($oWidth > $oHeight) {
$flScale = $maxSize / $oWidth;
} else {
$flScale = $maxSize / $oHeight;
}
$nWidth = (int)($oWidth * $flScale);
$nHeight = (int)($oHeight * $flScale);
imagecopyresampled(
$imgSquare, $imgOrig,
($maxSize - $nWidth) / 2, ($maxSize - $nHeight) / 2,
0, 0,
$nWidth, $nHeight,
$oWidth, $oHeight
);
imagesavealpha($imgSquare, true);
imagepng($imgSquare, $targetPath);
imagedestroy($imgSquare);
imagedestroy($imgOrig);
return true;
}
/**
* Check if the target file is newer than the source file.
*
* @param string $sourcePath Full source file path
* @param string $targetPath Full target file path
*
* @return boolean True if target file is newer than the source file
*/
function imageUptodate($sourcePath, $targetPath)
{
global $forceUpdate;
if ($forceUpdate) {
return false;
}
if (!file_exists($targetPath)) {
return false;
}
if (filemtime($sourcePath) > filemtime($targetPath)) {
//source newer
return false;
}
return true;
}
/**
* Write a log message to stdout
*
* @param string $msg Message to write
* @param integer $level Log level - 1 is important, 3 is unimportant
*
* @return void
*/
function log($msg, $level = 1)
{
global $logLevel;
if ($level <= $logLevel) {
echo $msg . "\n";
}
}
/**
* Write an error message to stderr
*
* @param string $msg Message to write
*
* @return void
*/
function logErr($msg)
{
file_put_contents('php://stderr', $msg . "\n");
}
?>