tell people about mm support and more infos about server testing
[surrogator.git] / surrogator.php
old mode 100644 (file)
new mode 100755 (executable)
index e48190e..dd907e0
+#!/usr/bin/env php
 <?php
-require __DIR__ . '/data/surrogator.config.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     http://git.cweiske.de/?p=surrogator.git
+ */
+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')) {
-    mkdir($varDir . '/square', 0755, true);
+    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);
     }
 }
+if (!file_exists($rawDir . '/mm.png')) {
+    log('mm.png missing, copying it from res/', 2);
+    copy(__DIR__ . '/res/mm.png', $rawDir . '/mm.png');
+}
 
-$dir = new RegexIterator(
-    new DirectoryIterator($rawDir),
-    '#^.+@.+\.(png|jpg)$#'
-);
-foreach ($dir as $fileInfo) {
+if (count($files)) {
+    $fileInfos = array();
+    foreach ($files as $file) {
+        $fileInfos[] = new \SplFileInfo($file);
+    }
+} else {
+    $fileInfos = new \RegexIterator(
+        new \DirectoryIterator($rawDir),
+        '#^.+\.(png|jpg)$#'
+    );
+}
+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';
 
-    if (image_uptodate($origPath, $squarePath)) {
+    log('processing ' . $fileName, 1);
+    if (imageUptodate($origPath, $squarePath)) {
+        log(' image up to date', 2);
         continue;
     }
 
@@ -29,10 +148,20 @@ foreach ($dir as $fileInfo) {
         continue;
     }
 
-    list($md5, $sha256) = getHashes($fileName);
+    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';
 
@@ -43,7 +172,7 @@ foreach ($dir as $fileInfo) {
             imagecolorallocatealpha($imgSize, 0, 0, 0, 127)
         );
         imagecopyresampled(
-            $imgSize, $imgSquare, 
+            $imgSize, $imgSquare,
             0, 0, 0, 0,
             $size, $size, $maxSize, $maxSize
         );
@@ -51,12 +180,18 @@ foreach ($dir as $fileInfo) {
         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)
 {
     $fileNameNoExt = substr($fileName, 0, -strlen(strrpos($fileName, '.')) - 2);
@@ -67,7 +202,17 @@ function getHashes($fileName)
     );
 }
 
-
+/**
+ * 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') {
@@ -80,10 +225,7 @@ function createSquare($origPath, $ext, $targetPath, $maxSize)
     }
 
     if ($imgOrig === false) {
-        file_put_contents(
-            'php://stderr',
-            'Error loading image file: ' . $origPath
-        );
+        logErr('Error loading image file: ' . $origPath);
         return false;
     }
 
@@ -106,7 +248,7 @@ function createSquare($origPath, $ext, $targetPath, $maxSize)
     $nHeight = (int)($oHeight * $flScale);
 
     imagecopyresampled(
-        $imgSquare, $imgOrig, 
+        $imgSquare, $imgOrig,
         ($maxSize - $nWidth) / 2, ($maxSize - $nHeight) / 2,
         0, 0,
         $nWidth, $nHeight,
@@ -121,12 +263,23 @@ function createSquare($origPath, $ext, $targetPath, $maxSize)
     return true;
 }
 
-function image_uptodate($sourcePath, $targetPath)
+/**
+ * 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;
@@ -135,4 +288,32 @@ function image_uptodate($sourcePath, $targetPath)
     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");
+}
+
 ?>