一注值得加入是为了确保你的代码不会产生这些图像的“未经授权”的大小。
因此,下面的URL将创建图像1234的200x200版本(如果尚不存在的话)。我会高度建议您确保请求的网址包含您支持的图片尺寸。
/images/get/200x200/1234.jpg
有恶意的人可以先要求随机网址,总是改变图像的高度&宽度。这将导致您的服务器出现一些严重问题,它将坐在那里,基本上处于受到攻击的状态,从而生成您不支持的尺寸图像。
/images/get/0x1/1234.jpg
/images/get/0x2/1234.jpg
...
/images/get/0x9999999/1234.jpg
/images/get/1x1/1234.jpg
...
etc
这里的代码说明这是一个随机剪断:
<?php
$pathOnDisk = getImageDiskPath($_SERVER['REQUEST_URI']);
if(file_exists($pathOnDisk)) {
// send header with image mime type
echo file_get_contents($pathOnDisk);
exit;
} else {
$matches = array();
$ok = preg_match(
'/\/images\/get\/(\d+)x(\d+)\/(\w+)\.jpg/',
$_SERVER['REQUEST_URI'], $matches);
if(! $ok) {
// invalid url
handleInvalidRequest();
} else {
list(, $width, $height, $guid) = $matches;
// you should do this!
if(isSupportedSize($width, $height)) {
// size is supported. all good
// generate the resized image, save it & output it
} else {
// invalid size requested!!!
handleInvalidRequest();
}
}
}
// snip
function handleInvalidRequest() {
// do something w/ invalid request
// show a default graphic, log it etc
}
?>
不错的建议,但... 如果通过上传的新映像(通过管理界面)更改源,则会生成新的GUID,并且缓存的文件名将不再匹配。 图像不应该与(s)FTP上传,但我们都知道假设。 ;) 无论如何将看看实现。 – 2008-09-26 17:31:46
我还检查图像是否被缓存_after_对缓存脚本进行了任何修改,即if(filemtime($ cached_image)> filemtime($ _ SERVER ['SCRIPT_FILENAME'])){...}` – 2011-05-01 12:33:52