2 require __DIR__ . '/data/surrogator.config.php';
4 if (!is_dir($varDir . '/square')) {
5 mkdir($varDir . '/square', 0755, true);
7 foreach ($sizes as $size) {
8 if (!is_dir($varDir . '/' . $size)) {
9 mkdir($varDir . '/' . $size, 0755);
13 $dir = new RegexIterator(
14 new DirectoryIterator($rawDir),
15 '#^.+@.+\.(png|jpg)$#'
17 foreach ($dir as $fileInfo) {
18 $origPath = $fileInfo->getPathname();
19 $fileName = $fileInfo->getFilename();
20 $ext = strtolower(substr($fileName, strrpos($fileName, '.') + 1));
21 $squarePath = $varDir . '/square/'
22 . substr($fileName, 0, -strlen($ext)) . 'png';
24 if (image_uptodate($origPath, $squarePath)) {
28 if (!createSquare($origPath, $ext, $squarePath, $maxSize)) {
32 list($md5, $sha256) = getHashes($fileName);
34 $imgSquare = imagecreatefrompng($squarePath);
35 foreach ($sizes as $size) {
36 $sizePathMd5 = $varDir . '/' . $size . '/' . $md5 . '.png';
37 $sizePathSha256 = $varDir . '/' . $size . '/' . $sha256 . '.png';
39 $imgSize = imagecreatetruecolor($size, $size);
40 imagealphablending($imgSize, false);
42 $imgSize, 0, 0, $size - 1, $size - 1,
43 imagecolorallocatealpha($imgSize, 0, 0, 0, 127)
48 $size, $size, $maxSize, $maxSize
50 imagesavealpha($imgSize, true);
51 imagepng($imgSize, $sizePathMd5);
52 imagepng($imgSize, $sizePathSha256);
53 imagedestroy($imgSize);
56 imagedestroy($imgSquare);
60 function getHashes($fileName)
62 $fileNameNoExt = substr($fileName, 0, -strlen(strrpos($fileName, '.')) - 2);
63 $emailAddress = trim(strtolower($fileNameNoExt));
66 md5($emailAddress), hash('sha256', $emailAddress)
71 function createSquare($origPath, $ext, $targetPath, $maxSize)
74 $imgOrig = imagecreatefrompng($origPath);
75 } else if ($ext == 'jpg' || $ext == 'jpeg') {
76 $imgOrig = imagecreatefromjpeg($origPath);
82 if ($imgOrig === false) {
85 'Error loading image file: ' . $origPath
90 $imgSquare = imagecreatetruecolor($maxSize, $maxSize);
91 imagealphablending($imgSquare, false);
93 $imgSquare, 0, 0, $maxSize - 1, $maxSize - 1,
94 imagecolorallocatealpha($imgSquare, 0, 0, 0, 127)
96 imagealphablending($imgSquare, true);
98 $oWidth = imagesx($imgOrig);
99 $oHeight = imagesy($imgOrig);
100 if ($oWidth > $oHeight) {
101 $flScale = $maxSize / $oWidth;
103 $flScale = $maxSize / $oHeight;
105 $nWidth = (int)($oWidth * $flScale);
106 $nHeight = (int)($oHeight * $flScale);
109 $imgSquare, $imgOrig,
110 ($maxSize - $nWidth) / 2, ($maxSize - $nHeight) / 2,
116 imagesavealpha($imgSquare, true);
117 imagepng($imgSquare, $targetPath);
119 imagedestroy($imgSquare);
120 imagedestroy($imgOrig);
124 function image_uptodate($sourcePath, $targetPath)
126 if (!file_exists($targetPath)) {
130 if (filemtime($sourcePath) > filemtime($targetPath)) {