2015-11-12 61 views
0

我正在生成压缩图像时的查询字符串URL。将查询字符串URL转换为静态路径

例如example.com/img.php?compressed=image.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.

+0

你是什么意思的静态网址? – gabo

+0

对不起,我的意思是一个没有QS的URL路径。例如example.com/img/image.jpg/width_280 – bjc999

+0

我不相信这将是可能的出来使用mod_rewrite –

回答

0

如果您不能使用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只是另一个目录级别。