4 $cfgFile = __DIR__ . '/data/surrogator.config.php';
5 if (!file_exists($cfgFile)) {
6 $cfgFile = '/etc/surrogator.config.php';
7 if (!file_exists($cfgFile)) {
9 "Configuration file does not exist.\n"
10 . "Copy data/surrogator.config.php.dist to data/surrogator.config.php"
19 foreach ($argv as $arg) {
20 if ($arg == '-v' || $arg == '--verbose') {
22 } else if ($arg == '-vv') {
24 } else if ($arg == '-q' || $arg == '--quiet') {
26 } else if ($arg == '-f' || $arg == '--force') {
28 } else if ($arg == '-h' || $arg == '--help') {
31 } else if ($arg == '--version') {
32 echo "surrogator 0.0.1\n";
34 } else if (file_exists($arg)) {
37 logErr('Unknown argument: ' . $arg);
45 Usage: php surrogator.php [options] [filename(s)]
47 surrogator - a simple libravatar server
48 Put files in raw/ dir and run surrogator.php to generate different sizes
53 -v, --verbose Be verbose (more log messages, also -vv)
54 -q, --quiet Be quiet (no log messages)
55 -f, --force Force update of all files
56 --version Show program version
58 filenames One or several files whose small images shall get generated.
59 If none given, all will be checked
64 if (!isset($rawDir)) {
65 logErr('$rawDir not set');
68 if (!isset($varDir)) {
69 logErr('$varDir not set');
73 logErr('$sizes not set');
76 if (!isset($maxSize)) {
77 logErr('$maxSize not set');
80 if (!isset($logLevel)) {
81 logErr('$logLevel not set');
86 if (!is_dir($varDir . '/square')) {
87 log('creating square dir: ' . $varDir . '/square');
88 mkdir($varDir . '/square', 0755, true);
90 log('sizes: ' . implode(', ', $sizes), 2);
91 foreach ($sizes as $size) {
92 if (!is_dir($varDir . '/' . $size)) {
93 log('creating size dir: ' . $varDir . '/' . $size);
94 mkdir($varDir . '/' . $size, 0755);
100 foreach ($files as $file) {
101 $fileInfos[] = new \SplFileInfo($file);
104 $fileInfos = new \RegexIterator(
105 new \DirectoryIterator($rawDir),
109 foreach ($fileInfos as $fileInfo) {
110 $origPath = $fileInfo->getPathname();
111 $fileName = $fileInfo->getFilename();
112 $ext = strtolower(substr($fileName, strrpos($fileName, '.') + 1));
113 $squarePath = $varDir . '/square/'
114 . substr($fileName, 0, -strlen($ext)) . 'png';
116 log('processing ' . $fileName, 1);
117 if (image_uptodate($origPath, $squarePath)) {
118 log(' image up to date', 2);
122 if (!createSquare($origPath, $ext, $squarePath, $maxSize)) {
126 if ($fileName == 'default.png') {
127 $md5 = $sha256 = 'default';
129 list($md5, $sha256) = getHashes($fileName);
132 log(' creating sizes for ' . $fileName, 2);
133 log(' md5: ' . $md5, 3);
134 log(' sha256: ' . $sha256, 3);
135 $imgSquare = imagecreatefrompng($squarePath);
136 foreach ($sizes as $size) {
137 log(' size ' . $size, 3);
138 $sizePathMd5 = $varDir . '/' . $size . '/' . $md5 . '.png';
139 $sizePathSha256 = $varDir . '/' . $size . '/' . $sha256 . '.png';
141 $imgSize = imagecreatetruecolor($size, $size);
142 imagealphablending($imgSize, false);
143 imagefilledrectangle(
144 $imgSize, 0, 0, $size - 1, $size - 1,
145 imagecolorallocatealpha($imgSize, 0, 0, 0, 127)
148 $imgSize, $imgSquare,
150 $size, $size, $maxSize, $maxSize
152 imagesavealpha($imgSize, true);
153 imagepng($imgSize, $sizePathMd5);
154 imagepng($imgSize, $sizePathSha256);
155 imagedestroy($imgSize);
158 imagedestroy($imgSquare);
161 function getHashes($fileName)
163 $fileNameNoExt = substr($fileName, 0, -strlen(strrpos($fileName, '.')) - 2);
164 $emailAddress = trim(strtolower($fileNameNoExt));
167 md5($emailAddress), hash('sha256', $emailAddress)
172 function createSquare($origPath, $ext, $targetPath, $maxSize)
175 $imgOrig = imagecreatefrompng($origPath);
176 } else if ($ext == 'jpg' || $ext == 'jpeg') {
177 $imgOrig = imagecreatefromjpeg($origPath);
183 if ($imgOrig === false) {
184 logErr('Error loading image file: ' . $origPath);
188 $imgSquare = imagecreatetruecolor($maxSize, $maxSize);
189 imagealphablending($imgSquare, false);
190 imagefilledrectangle(
191 $imgSquare, 0, 0, $maxSize - 1, $maxSize - 1,
192 imagecolorallocatealpha($imgSquare, 0, 0, 0, 127)
194 imagealphablending($imgSquare, true);
196 $oWidth = imagesx($imgOrig);
197 $oHeight = imagesy($imgOrig);
198 if ($oWidth > $oHeight) {
199 $flScale = $maxSize / $oWidth;
201 $flScale = $maxSize / $oHeight;
203 $nWidth = (int)($oWidth * $flScale);
204 $nHeight = (int)($oHeight * $flScale);
207 $imgSquare, $imgOrig,
208 ($maxSize - $nWidth) / 2, ($maxSize - $nHeight) / 2,
214 imagesavealpha($imgSquare, true);
215 imagepng($imgSquare, $targetPath);
217 imagedestroy($imgSquare);
218 imagedestroy($imgOrig);
222 function image_uptodate($sourcePath, $targetPath)
228 if (!file_exists($targetPath)) {
231 if (filemtime($sourcePath) > filemtime($targetPath)) {
239 function log($msg, $level = 1)
242 if ($level <= $logLevel) {
247 function logErr($msg)
249 file_put_contents('php://stderr', $msg . "\n");