编辑
从上面和下面的 cmets 中读取,您需要一个解决方案来隐藏图像解析器的真实路径,为其提供固定的图像宽度。
第 1 步:http://www.example.com/tn/full/animals/images/lion.jpg
你可以通过.htaccess获利来实现一个基本的“缩略图”
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule tn/(full|small)/(.*) index.php?size=$1&img=$2 [QSA,L]
您的 PHP 文件:
$basedir="/public/content/";
$filename=realpath($basedir.$_GET["img"]);
## check that file is in $basedir
if ((!strncmp($filename, $basedir, strlen($basedir))
||(!file_exists($filename)) die("Bad file path");
switch ($_GET["size"]) {
case "full":
$width=700;
$height=500;
## you can also use getimagesize() to test if the image is landscape or portrait
break;
default:
$width=350;
$height=250;
break;
}
## here is your old code for resizing images
## Note that the "tn" directory can exist and store the actual reduced images
这使您可以使用 URL www.example.com/tn/full/animals/images/lion.jpg 来查看缩小后的图像。
这有利于 SEO 保留原始文件名。
第二步:http://www.example.com/tn/full/lion.jpg
如果你想要一个更短的 url,如果你拥有的图片数量不是太多,你可以使用文件的基本名称(例如“lion.jpg”)并递归搜索。碰撞时使用索引来识别您想要的(例如“1--lion.jpg”)
function matching_files($filename, $base) {
$directory_iterator = new RecursiveDirectoryIterator($base);
$iterator = new RecursiveIteratorIterator($directory_iterator);
$regex_iterator = new RegexIterator($iterator, "#$filename\$#");
$regex_iterator->setFlags(RegexIterator::USE_KEY);
return array_map(create_function('$a', 'return $a->getpathName();'), iterator_to_array($regex_iterator, false));
}
function encode_name($filename) {
$files=matching_files(basename($filename), realpath('public/content'));
$tot=count($files);
if (!$tot) return NULL;
if ($tot==1) return $filename;
return "/tn/full/".array_search(realpath($filename), $files)."--".basename($filename);
}
function decode_name($filename) {
$i=0;
if (preg_match("#^([0-9]+)--(.*)#", $filename, $out)) {
$i=$out[1];
$filename=$out[2];
}
$files=matching_files($filename, realpath('public/content'));
return $files ? $files[$i] : NULL;
}
echo $name=encode_name("gallery/animals/images/lion.jpg").PHP_EOL;
## --> returns lion.jpg
## You can use with the above solution the url http://www.example.com/tn/lion.jpg
echo decode_name(basename($name)).PHP_EOL;
## -> returns the full path opn disk to the image "lion.jpg"
原帖:
基本上,如果您在示例中添加一些格式,您的缩短网址实际上会更长:
img=/dir/dir/hi-res-img.jpg&w=700&h=500 // 39 chars
y8xNt9VPySwC44xM3aLUYt3M3HS9rIJ0tXJbcwMDtQxbUwMDAA // 50 chars
使用base64_encode 总是会产生更长的字符串。并且gzcompress 将需要更少的存储不同字符的出现;对于小字符串,这不是一个好的解决方案。
因此,如果您想缩短之前的结果,那么什么都不做(或简单的str_rot13)显然是第一个考虑的选项。
您还可以使用您选择的简单字符替换方法:
$raw_query_string = 'img=/dir/dir/hi-res-img.jpg&w=700&h=500';
$from="0123456789abcdefghijklmnopqrstuvwxyz&=/ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// the following line if the result of str_shuffle($from)
$to="0IQFwAKU1JT8BM5npNEdi/DvZmXuflPVYChyrL4R7xc&SoG3Hq6ks=e9jW2abtOzg";
echo strtr($raw_query_string, $from, $to)."\n";
// Result: EDpL4MEu4MEu4NE-u5f-EDp.dmprYLU00rNLA00 // 39 chars
阅读您的评论,您真正想要的是“防止任何人获得高分辨率图像”。
实现这一目标的最佳方法是使用私钥生成校验和。
编码:
$secret="ujoo4Dae";
$raw_query_string = 'img=/dir/dir/hi-res-img.jpg&w=700&h=500';
$encoded_query_string = $raw_query_string."&k=".hash("crc32", $raw_query_string.$secret);
结果:img=/dir/dir/hi-res-img.jpg&w=700&h=500&k=2ae31804
解码:
if (preg_match("#(.*)&k=([^=]*)$#", $encoded_query_string, $out)
&& (hash("crc32", $out[1].$secret) == $out[2])) {
$decoded_query_string=$out[1];
}
这不会隐藏原始路径,但该路径没有理由公开,您的“index.php”可以在检查密钥后从本地目录输出您的图像。
如果你真的想缩短你的原始网址,你必须考虑限制原始网址中可接受的字符。许多压缩方法都基于这样一个事实,即您可以使用一个完整字节来存储多个字符。