b9f724d05446bca7a7fee401775b68ef7b5d5e2f
[surrogator.git] / surrogator.php
1 <?php
2 namespace surrogator;
3 require __DIR__ . '/data/surrogator.config.php';
4
5 if (!isset($rawDir)) {
6     logErr('$rawDir not set');
7 }
8 if (!isset($varDir)) {
9     logErr('$varDir not set');
10 }
11 if (!isset($sizes)) {
12     logErr('$sizes not set');
13 }
14 if (!isset($maxSize)) {
15     logErr('$maxSize not set');
16 }
17
18
19 if (!is_dir($varDir . '/square')) {
20     log('Creating square dir: ' . $varDir . '/square');
21     mkdir($varDir . '/square', 0755, true);
22 }
23 log('sizes: ' . implode(', ', $sizes));
24 foreach ($sizes as $size) {
25     if (!is_dir($varDir . '/' . $size)) {
26         log('Creating size dir: ' . $varDir . '/' . $size);
27         mkdir($varDir . '/' . $size, 0755);
28     }
29 }
30
31 $dir = new \RegexIterator(
32     new \DirectoryIterator($rawDir),
33     '#^.+\.(png|jpg)$#'
34 );
35 foreach ($dir as $fileInfo) {
36     $origPath   = $fileInfo->getPathname();
37     $fileName   = $fileInfo->getFilename();
38     $ext        = strtolower(substr($fileName, strrpos($fileName, '.') + 1));
39     $squarePath = $varDir . '/square/'
40         . substr($fileName, 0, -strlen($ext)) . 'png';
41
42     log('processing ' . $fileName);
43     if (image_uptodate($origPath, $squarePath)) {
44         log(' image up to date');
45         continue;
46     }
47
48     if (!createSquare($origPath, $ext, $squarePath, $maxSize)) {
49         continue;
50     }
51
52     if ($fileName == 'default.png') {
53         $md5 = $sha256 = 'default';
54     } else {
55         list($md5, $sha256) = getHashes($fileName);
56     }
57
58     log(' Creating sizes for ' . $fileName);
59     log(' md5:    ' . $md5);
60     log(' sha256: ' . $sha256);
61     $imgSquare = imagecreatefrompng($squarePath);
62     foreach ($sizes as $size) {
63         $sizePathMd5    = $varDir . '/' . $size . '/' . $md5 . '.png';
64         $sizePathSha256 = $varDir . '/' . $size . '/' . $sha256 . '.png';
65
66         $imgSize = imagecreatetruecolor($size, $size);
67         imagealphablending($imgSize, false);
68         imagefilledrectangle(
69             $imgSize, 0, 0, $size - 1, $size - 1,
70             imagecolorallocatealpha($imgSize, 0, 0, 0, 127)
71         );
72         imagecopyresampled(
73             $imgSize, $imgSquare, 
74             0, 0, 0, 0,
75             $size, $size, $maxSize, $maxSize
76         );
77         imagesavealpha($imgSize, true);
78         imagepng($imgSize, $sizePathMd5);
79         imagepng($imgSize, $sizePathSha256);
80         imagedestroy($imgSize);
81         
82     }
83     imagedestroy($imgSquare);
84 }
85
86
87 function getHashes($fileName)
88 {
89     $fileNameNoExt = substr($fileName, 0, -strlen(strrpos($fileName, '.')) - 2);
90     $emailAddress  = trim(strtolower($fileNameNoExt));
91
92     return array(
93         md5($emailAddress), hash('sha256', $emailAddress)
94     );
95 }
96
97
98 function createSquare($origPath, $ext, $targetPath, $maxSize)
99 {
100     if ($ext == 'png') {
101         $imgOrig = imagecreatefrompng($origPath);
102     } else if ($ext == 'jpg' || $ext == 'jpeg') {
103         $imgOrig = imagecreatefromjpeg($origPath);
104     } else {
105         //unsupported format
106         return false;
107     }
108
109     if ($imgOrig === false) {
110         logErr('Error loading image file: ' . $origPath);
111         return false;
112     }
113
114     $imgSquare = imagecreatetruecolor($maxSize, $maxSize);
115     imagealphablending($imgSquare, false);
116     imagefilledrectangle(
117         $imgSquare, 0, 0, $maxSize - 1, $maxSize - 1,
118         imagecolorallocatealpha($imgSquare, 0, 0, 0, 127)
119     );
120     imagealphablending($imgSquare, true);
121
122     $oWidth    = imagesx($imgOrig);
123     $oHeight   = imagesy($imgOrig);
124     if ($oWidth > $oHeight) {
125         $flScale = $maxSize / $oWidth;
126     } else {
127         $flScale = $maxSize / $oHeight;
128     }
129     $nWidth  = (int)($oWidth * $flScale);
130     $nHeight = (int)($oHeight * $flScale);
131
132     imagecopyresampled(
133         $imgSquare, $imgOrig, 
134         ($maxSize - $nWidth) / 2, ($maxSize - $nHeight) / 2,
135         0, 0,
136         $nWidth, $nHeight,
137         $oWidth, $oHeight
138     );
139
140     imagesavealpha($imgSquare, true);
141     imagepng($imgSquare, $targetPath);
142
143     imagedestroy($imgSquare);
144     imagedestroy($imgOrig);
145     return true;
146 }
147
148 function image_uptodate($sourcePath, $targetPath)
149 {
150     if (!file_exists($targetPath)) {
151         return false;
152     }
153
154     if (filemtime($sourcePath) > filemtime($targetPath)) {
155         //source newer
156         return false;
157     }
158
159     return true;
160 }
161
162 function log($msg)
163 {
164     echo $msg . "\n";
165 }
166
167 function logErr($msg)
168 {
169     file_put_contents('php://stderr', $msg . "\n");
170 }
171
172 ?>