3 require_once(__DIR__.'/'.'config.php');
4 require_once(__DIR__.'/'.'inc/wfpl/upload.php');
6 function cms_images_autoresize_main_abort_404() {
7 http_response_code('404');
8 header('HTTP/1.0 404 File Not Found');
9 header('Content-Type: text/plain');
10 print('404: File not found');
14 function cms_images_autoresize_main() {
15 # figure out what file was requested
16 $out_fn = $_SERVER['REDIRECT_URL'];
17 $out_fn = preg_replace('|[?].*|', '', $out_fn); # apache 2.4.17
18 $out_fn = preg_replace('|.*/|', '', $out_fn);
20 if (!preg_match('/^([0-9a-f]+)w([0-9]+)[.](png|jpg)$/', $out_fn, $matches)) {
21 cms_images_autoresize_main_abort_404();
24 $basename = $matches[1];
25 $width = (int)$matches[2];
27 $in_fn = "$basename.$ext";
28 $in_path = __DIR__.'/'.'cms_images/' . $in_fn;
29 $out_path = __DIR__.'/'.'cms_images/' . $out_fn;
30 $lock_path = $out_path . '.lock';
32 if (!in_array($width, $GLOBALS['wfpl_image_widths'], true)) {
33 cms_images_autoresize_main_abort_404();
36 if (!file_exists($in_path)) {
37 cms_images_autoresize_main_abort_404();
40 @$lock = fopen($lock_path, 'x');
42 # delete lock file if it's stale
43 $s = stat($lock_path);
44 if ($s && $s['mtime'] + 3 < time()) {
47 # if it's fresh, exit with temporary error
48 header('HTTP/1.0 503 Service Unavailable');
49 header('Content-Type: text/plain');
50 header('Retry-After: 4');
51 print("503 Service Unavailable (try again soon)\n");
58 imagemagick_convert($in_path, $out_path,
70 if (!headers_sent()) {
71 header('Content-Type: ' . ($ext = 'jpg' ? 'image/jpeg' : 'image/png'));
75 cms_images_autoresize_main();