/** * Parse the filename of the requested image * for size information and resize accordingly. */ function image_resize_scale($path) { $params = explode("/", $path); $namepart = array_pop($params); $public_path = URL_PUBLIC . "/" . join("/", $params); $server_path = FROG_ROOT . "/" . join("/", $params); // Dissect filename to find dimension information $pattern = <<<FILENAME_PATTERN /^ # Acceptable input examples (non-exhaustive): # Width scaling: file.200.jpg file.200x.jpg # Height scaling: file.x200.jpg # Arbitrary scaling: file.200x200.jpg # Cropping: file.200c.jpg file.200x200c.jpg (.+) # source file name-part \\.(?!x?c?\\.) # prevents "image..jpg" and so forth (\\d+)? # width x?(\\d+)? # height (c)? # optional crop (\\.[a-z]+) # source file extension \$/ix FILENAME_PATTERN; if (preg_match($pattern, $namepart, $match)) { $filename = $match[1] . $match[5]; $width = (int) $match[2]; $height = (int) $match[3]; $crop = 'c' == $match[4]; $source = $server_path . "/" . $filename; $destination = $server_path . "/" . $namepart; } else { return false; } if ($crop) { return ImageResize::image_scale_cropped($source, $destination, $width, $height); } else { return ImageResize::image_scale($source, $destination, $width, $height); } }
<?php header("cache-control: no-cache"); require_once 'ImageResize.php'; foreach (glob("./tmp/*") as $source) { $file = pathinfo($source); if (!preg_match('#-100x100\\.([a-z]+)$#i', $source)) { $destination = $file['dirname'] . '/' . $file['filename'] . '-100x100.' . $file['extension']; ImageResize::image_scale_cropped($source, $destination, 100, 100); printf('<a href="%s"><img src="%s" widht="100" height="100" /></a>', $source, $destination); } }