#!/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)) {
}
}
+/**
+ * Echos the --help screen.
+ *
+ * @return void
+ */
function showHelp()
{
echo <<<HLP
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();
} else {
$fileInfos = new \RegexIterator(
new \DirectoryIterator($rawDir),
- '#^.+\.(png|jpg)$#'
+ '#^.+\.(png|jpg|svg|svgz)$#'
);
}
foreach ($fileInfos as $fileInfo) {
. substr($fileName, 0, -strlen($ext)) . 'png';
log('processing ' . $fileName, 1);
- if (image_uptodate($origPath, $squarePath)) {
+ if (imageUptodate($origPath, $squarePath)) {
log(' image up to date', 2);
continue;
}
if ($fileName == 'default.png') {
$md5 = $sha256 = 'default';
+ } else if ($fileName == 'mm.png') {
+ $md5 = $sha256 = 'mm';
} else {
list($md5, $sha256) = getHashes($fileName);
}
imagecolorallocatealpha($imgSize, 0, 0, 0, 127)
);
imagecopyresampled(
- $imgSize, $imgSquare,
+ $imgSize, $imgSquare,
0, 0, 0, 0,
$size, $size, $maxSize, $maxSize
);
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 = substr($fileName, 0, -strlen(strrpos($fileName, '.')) - 2);
$emailAddress = trim(strtolower($fileNameNoExt));
);
}
-
+/**
+ * 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;
}
$nHeight = (int)($oHeight * $flScale);
imagecopyresampled(
- $imgSquare, $imgOrig,
+ $imgSquare, $imgOrig,
($maxSize - $nWidth) / 2, ($maxSize - $nHeight) / 2,
0, 0,
$nWidth, $nHeight,
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 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;
}
}
+/**
+ * Write an error message to stderr
+ *
+ * @param string $msg Message to write
+ *
+ * @return void
+ */
function logErr($msg)
{
file_put_contents('php://stderr', $msg . "\n");