initial working image resizer
[surrogator.git] / surrogator.php
1 <?php
2 require __DIR__ . '/data/surrogator.config.php';
3
4 if (!is_dir($varDir . '/square')) {
5     mkdir($varDir . '/square', 0755, true);
6 }
7 foreach ($sizes as $size) {
8     if (!is_dir($varDir . '/' . $size)) {
9         mkdir($varDir . '/' . $size, 0755);
10     }
11 }
12
13 $dir = new RegexIterator(
14     new DirectoryIterator($rawDir),
15     '#^.+@.+\.(png|jpg)$#'
16 );
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';
23
24     if (image_uptodate($origPath, $squarePath)) {
25         continue;
26     }
27
28     if (!createSquare($origPath, $ext, $squarePath, $maxSize)) {
29         continue;
30     }
31
32     list($md5, $sha256) = getHashes($fileName);
33
34     $imgSquare = imagecreatefrompng($squarePath);
35     foreach ($sizes as $size) {
36         $sizePathMd5    = $varDir . '/' . $size . '/' . $md5 . '.png';
37         $sizePathSha256 = $varDir . '/' . $size . '/' . $sha256 . '.png';
38
39         $imgSize = imagecreatetruecolor($size, $size);
40         imagealphablending($imgSize, false);
41         imagefilledrectangle(
42             $imgSize, 0, 0, $size - 1, $size - 1,
43             imagecolorallocatealpha($imgSize, 0, 0, 0, 127)
44         );
45         imagecopyresampled(
46             $imgSize, $imgSquare, 
47             0, 0, 0, 0,
48             $size, $size, $maxSize, $maxSize
49         );
50         imagesavealpha($imgSize, true);
51         imagepng($imgSize, $sizePathMd5);
52         imagepng($imgSize, $sizePathSha256);
53         imagedestroy($imgSize);
54         
55     }
56     imagedestroy($imgSquare);
57 }
58
59
60 function getHashes($fileName)
61 {
62     $fileNameNoExt = substr($fileName, 0, -strlen(strrpos($fileName, '.')) - 2);
63     $emailAddress  = trim(strtolower($fileNameNoExt));
64
65     return array(
66         md5($emailAddress), hash('sha256', $emailAddress)
67     );
68 }
69
70
71 function createSquare($origPath, $ext, $targetPath, $maxSize)
72 {
73     if ($ext == 'png') {
74         $imgOrig = imagecreatefrompng($origPath);
75     } else if ($ext == 'jpg' || $ext == 'jpeg') {
76         $imgOrig = imagecreatefromjpeg($origPath);
77     } else {
78         //unsupported format
79         return false;
80     }
81
82     if ($imgOrig === false) {
83         file_put_contents(
84             'php://stderr',
85             'Error loading image file: ' . $origPath
86         );
87         return false;
88     }
89
90     $imgSquare = imagecreatetruecolor($maxSize, $maxSize);
91     imagealphablending($imgSquare, false);
92     imagefilledrectangle(
93         $imgSquare, 0, 0, $maxSize - 1, $maxSize - 1,
94         imagecolorallocatealpha($imgSquare, 0, 0, 0, 127)
95     );
96     imagealphablending($imgSquare, true);
97
98     $oWidth    = imagesx($imgOrig);
99     $oHeight   = imagesy($imgOrig);
100     if ($oWidth > $oHeight) {
101         $flScale = $maxSize / $oWidth;
102     } else {
103         $flScale = $maxSize / $oHeight;
104     }
105     $nWidth  = (int)($oWidth * $flScale);
106     $nHeight = (int)($oHeight * $flScale);
107
108     imagecopyresampled(
109         $imgSquare, $imgOrig, 
110         ($maxSize - $nWidth) / 2, ($maxSize - $nHeight) / 2,
111         0, 0,
112         $nWidth, $nHeight,
113         $oWidth, $oHeight
114     );
115
116     imagesavealpha($imgSquare, true);
117     imagepng($imgSquare, $targetPath);
118
119     imagedestroy($imgSquare);
120     imagedestroy($imgOrig);
121     return true;
122 }
123
124 function image_uptodate($sourcePath, $targetPath)
125 {
126     if (!file_exists($targetPath)) {
127         return false;
128     }
129
130     if (filemtime($sourcePath) > filemtime($targetPath)) {
131         //source newer
132         return false;
133     }
134
135     return true;
136 }
137
138 ?>