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