I am currently producing query string URLs when compressing images.
e.g. example.com/img.php?compressed=image.jpg&w=280
But I need to produce static URL paths.
e.g. example.com/img/image.jpg/width_280
I am using the following code to construct the query string URLs:
require_once 'img.class.php';
$getImage = new GetImage();
$getImage->setCacheFolder(FOLDER_CACHE);
$getImage->setErrorImagePath(FILEPATH_IMAGE_NOT_FOUND);
$getImage->setJpegQuality(JPEG_QUALITY);
$img = $_GET["img"];
$width = -1;
$width = isset($_GET["w"])?$_GET["w"]:-1;
$height = isset($_GET["h"])?$_GET["h"]:-1;
$type = "";
if(isset($_GET["exact"])) $type = GetImage::TYPE_EXACT;
else if(isset($_GET["exacttop"])) $type = GetImage::TYPE_EXACT_TOP;
$getImage->showImage($img,$width,$height,$type);
Is it possible to change this code in any way to produce static URLs?
It must be hard-coded, rather than a mod_rewrite solution.
Many thanks in advance!
B.
If you cannot use mod_rewrite (can be in .htaccess if server config allows) or something like "ErrorDocument 404 /img.php", you can use path overloading (I don't know if this has a name):
PHP:
$subpath = substr($_SERVER['PHP_SELF'], strlen($_SERVER['SCRIPT_NAME']) + 1);
$parts = explode('/', $subpath);
$opts = array(
'width' => -1,
'height' => -1,
);
while ($parts) {
if (preg_match('/^(width|height)_(\d+)$/', $parts[0], $matches)) {
$opts[$matches[1]] = $matches[2];
// more options with "} elseif () {"
} else {
break;
}
array_shift($parts);
}
$image = implode('/', $parts);
if (!$image) {
die("No image given
");
}
// test output
header('Content-Type: text/plain; charset=utf-8');
var_dump($opts);
var_dump($image);
Example:
http://localhost/img.php/width_200/test/image.jpg
// Output
array(2) {
["width"]=>
string(3) "200"
["height"]=>
int(-1)
}
string(14) "test/image.jpg"
I've placed the image path at the end to have the real extension at the end. For the client side, the script name img.php is just another directory level.