4 * Tool to create avatar images in different sizes.
6 * Part of Surrogator - a simple libravatar avatar image server
12 * @author Christian Weiske <cweiske@cweiske.de>
13 * @license http://www.gnu.org/licenses/agpl.html AGPLv3 or later
14 * @link http://git.cweiske.de/?p=surrogator.git
17 $cfgFile = __DIR__ . '/data/surrogator.config.php';
18 if (!file_exists($cfgFile)) {
19 $cfgFile = '/etc/surrogator.config.php';
20 if (!file_exists($cfgFile)) {
22 "Configuration file does not exist.\n"
23 . "Copy data/surrogator.config.php.dist to data/surrogator.config.php"
32 foreach ($argv as $arg) {
33 if ($arg == '-v' || $arg == '--verbose') {
35 } else if ($arg == '-vv') {
37 } else if ($arg == '-q' || $arg == '--quiet') {
39 } else if ($arg == '-f' || $arg == '--force') {
41 } else if ($arg == '-h' || $arg == '--help') {
44 } else if ($arg == '--version') {
45 echo "surrogator 0.0.1\n";
47 } else if (file_exists($arg)) {
50 logErr('Unknown argument: ' . $arg);
56 * Echos the --help screen.
63 Usage: php surrogator.php [options] [filename(s)]
65 surrogator - a simple libravatar server
66 Put files in raw/ dir and run surrogator.php to generate different sizes
71 -v, --verbose Be verbose (more log messages, also -vv)
72 -q, --quiet Be quiet (no log messages)
73 -f, --force Force update of all files
74 --version Show program version
76 filenames One or several files whose small images shall get generated.
77 If none given, all will be checked
82 if (!isset($rawDir)) {
83 logErr('$rawDir not set');
86 if (!isset($varDir)) {
87 logErr('$varDir not set');
91 logErr('$sizes not set');
94 if (!isset($maxSize)) {
95 logErr('$maxSize not set');
98 if (!isset($logLevel)) {
99 logErr('$logLevel not set');
104 if (!is_dir($varDir . '/square')) {
105 log('creating square dir: ' . $varDir . '/square');
106 if (!mkdir($varDir . '/square', 0755, true)) {
107 logErr('cannot create square dir');
111 log('sizes: ' . implode(', ', $sizes), 2);
112 foreach ($sizes as $size) {
113 if (!is_dir($varDir . '/' . $size)) {
114 log('creating size dir: ' . $varDir . '/' . $size);
115 mkdir($varDir . '/' . $size, 0755);
120 $fileInfos = array();
121 foreach ($files as $file) {
122 $fileInfos[] = new \SplFileInfo($file);
125 $fileInfos = new \RegexIterator(
126 new \DirectoryIterator($rawDir),
130 foreach ($fileInfos as $fileInfo) {
131 $origPath = $fileInfo->getPathname();
132 $fileName = $fileInfo->getFilename();
133 $ext = strtolower(substr($fileName, strrpos($fileName, '.') + 1));
134 $squarePath = $varDir . '/square/'
135 . substr($fileName, 0, -strlen($ext)) . 'png';
137 log('processing ' . $fileName, 1);
138 if (imageUptodate($origPath, $squarePath)) {
139 log(' image up to date', 2);
143 if (!createSquare($origPath, $ext, $squarePath, $maxSize)) {
147 if ($fileName == 'default.png') {
148 $md5 = $sha256 = 'default';
150 list($md5, $sha256) = getHashes($fileName);
153 log(' creating sizes for ' . $fileName, 2);
154 log(' md5: ' . $md5, 3);
155 log(' sha256: ' . $sha256, 3);
156 $imgSquare = imagecreatefrompng($squarePath);
157 foreach ($sizes as $size) {
158 log(' size ' . $size, 3);
159 $sizePathMd5 = $varDir . '/' . $size . '/' . $md5 . '.png';
160 $sizePathSha256 = $varDir . '/' . $size . '/' . $sha256 . '.png';
162 $imgSize = imagecreatetruecolor($size, $size);
163 imagealphablending($imgSize, false);
164 imagefilledrectangle(
165 $imgSize, 0, 0, $size - 1, $size - 1,
166 imagecolorallocatealpha($imgSize, 0, 0, 0, 127)
169 $imgSize, $imgSquare,
171 $size, $size, $maxSize, $maxSize
173 imagesavealpha($imgSize, true);
174 imagepng($imgSize, $sizePathMd5);
175 imagepng($imgSize, $sizePathSha256);
176 imagedestroy($imgSize);
179 imagedestroy($imgSquare);
183 * Create and return md5 and sha256 hashes from a filename.
185 * @param string $fileName filename without path, e.g. "foo@example.org.png"
187 * @return array Array with 2 values: md5 and sha256 hash
189 function getHashes($fileName)
191 $fileNameNoExt = substr($fileName, 0, -strlen(strrpos($fileName, '.')) - 2);
192 $emailAddress = trim(strtolower($fileNameNoExt));
195 md5($emailAddress), hash('sha256', $emailAddress)
200 * Creates the square image from the given image in maximum size.
201 * Scales the image up or down and makes the non-covered parts transparent.
203 * @param string $origPath Full path to original image
204 * @param string $ext File extension ("jpg" or "png")
205 * @param string $targetPath Full path to target image file
206 * @param integer $maxSize Maxium image size the server supports
208 * @return boolean True if all went well, false if there was an error
210 function createSquare($origPath, $ext, $targetPath, $maxSize)
213 $imgOrig = imagecreatefrompng($origPath);
214 } else if ($ext == 'jpg' || $ext == 'jpeg') {
215 $imgOrig = imagecreatefromjpeg($origPath);
221 if ($imgOrig === false) {
222 logErr('Error loading image file: ' . $origPath);
226 $imgSquare = imagecreatetruecolor($maxSize, $maxSize);
227 imagealphablending($imgSquare, false);
228 imagefilledrectangle(
229 $imgSquare, 0, 0, $maxSize - 1, $maxSize - 1,
230 imagecolorallocatealpha($imgSquare, 0, 0, 0, 127)
232 imagealphablending($imgSquare, true);
234 $oWidth = imagesx($imgOrig);
235 $oHeight = imagesy($imgOrig);
236 if ($oWidth > $oHeight) {
237 $flScale = $maxSize / $oWidth;
239 $flScale = $maxSize / $oHeight;
241 $nWidth = (int)($oWidth * $flScale);
242 $nHeight = (int)($oHeight * $flScale);
245 $imgSquare, $imgOrig,
246 ($maxSize - $nWidth) / 2, ($maxSize - $nHeight) / 2,
252 imagesavealpha($imgSquare, true);
253 imagepng($imgSquare, $targetPath);
255 imagedestroy($imgSquare);
256 imagedestroy($imgOrig);
261 * Check if the target file is newer than the source file.
263 * @param string $sourcePath Full source file path
264 * @param string $targetPath Full target file path
266 * @return boolean True if target file is newer than the source file
268 function imageUptodate($sourcePath, $targetPath)
274 if (!file_exists($targetPath)) {
277 if (filemtime($sourcePath) > filemtime($targetPath)) {
286 * Write a log message to stdout
288 * @param string $msg Message to write
289 * @param integer $level Log level - 1 is important, 3 is unimportant
293 function log($msg, $level = 1)
296 if ($level <= $logLevel) {
302 * Write an error message to stderr
304 * @param string $msg Message to write
308 function logErr($msg)
310 file_put_contents('php://stderr', $msg . "\n");