将查询字符串URL转换为静态路径


Convert Query String URL to Static Path

我目前正在压缩图像时生成查询字符串URL。

例如example.com/img.php?compressed=图像.jpg&w=280

但是我需要生成静态URL路径。

例如example.com/img/image.jpg/width_280

我使用以下代码来构建查询字符串URL:

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);

是否可以以任何方式更改此代码以生成静态URL?

它必须是硬编码的,而不是mod_rewrite解决方案。

非常感谢!

B。

如果你不能使用mod_rewrite(如果服务器配置允许,可以在.htaccess中)或类似"ErrorDocument 404/img.php"的东西,你可以使用路径重载(我不知道这是否有名称):

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'n");
}
// test output
header('Content-Type: text/plain; charset=utf-8');
var_dump($opts);
var_dump($image);

示例:

http://localhost/img.php/width_200/test/image.jpg
// Output
array(2) {
  ["width"]=>
  string(3) "200"
  ["height"]=>
  int(-1)
}
string(14) "test/image.jpg"

我已经将图像路径放置在末尾,以便在末尾具有真正的扩展。对于客户端,脚本名称img.php只是另一个目录级别。