Php 创建已调整大小的图像缓存,但防止滥用
好的,我有一个想法,我想如何服务和缓存我的图像。 我不知道这样做是否正确,但如果是,我想知道如何防止虐待 情况: index.phpPhp 创建已调整大小的图像缓存,但防止滥用,php,image,.htaccess,caching,Php,Image,.htaccess,Caching,好的,我有一个想法,我想如何服务和缓存我的图像。 我不知道这样做是否正确,但如果是,我想知道如何防止虐待 情况: index.php <img src="images/cache/200x150-picture_001.jpg" /> 上面检查图像是否存在,如果不存在,将重写为image.php image.php 伪码 从文件名获取高度和宽度 调整图像大小并将其保存到缓存文件夹 为图像提供内容类型和readfile 通过这种方式,我试图用readfiles减少HTTP请求和PHP
<img src="images/cache/200x150-picture_001.jpg" />
上面检查图像是否存在,如果不存在,将重写为image.php
image.php
伪码
从文件名获取高度和宽度
调整图像大小并将其保存到缓存文件夹
为图像提供内容类型和readfile
通过这种方式,我试图用readfiles减少HTTP请求和PHP负载。
浏览器将获取图像/jpeg(如果存在),如果不存在,将生成图像/jpeg
最终,所有图像都将被缓存并以适当的高度和宽度提供,这样浏览器就不会下载过大的图像
唯一的陷阱。。如果将图像url更改为不同的维度,则可以填充服务器
我做得对吗?正确的方法是什么。有没有可能阻止这一切的发生
我知道缓存的整个概念已经改进了一百万次,请告诉我。一些方法:
- 维护一个允许的分辨率数组,并检查请求的分辨率是否在该数组中。缺点:如果不编辑阵列,则无法快速添加分辨率
- 如果这是在CMS上下文中:仅允许经过身份验证的用户创建新图像(尚未在缓存中);否则拒绝请求。当一个经过身份验证的用户在CMS中添加一个图像时,他们会预览它,这样做会生成调整大小的图像。缺点:不太容易实现
- 维护一个允许的分辨率数组,并检查请求的分辨率是否在该数组中。缺点:如果不编辑阵列,则无法快速添加分辨率
- 如果这是在CMS上下文中:仅允许经过身份验证的用户创建新图像(尚未在缓存中);否则拒绝请求。当一个经过身份验证的用户在CMS中添加一个图像时,他们会预览它,这样做会生成调整大小的图像。缺点:不太容易实现
您是否看过“现成”的解决方案,例如?缓存是一个非常重要的问题-您的解决方案似乎合理,但确实容易受到有意和无意的拒绝服务攻击。它也没有解决图像更改时会发生什么-如何从缓存中删除所有已调整大小的图像?它没有将缓存头设置为允许“下游”缓存。它不处理同时刷新整个缓存的风险,需要在HTTP请求的上下文中重新生成所有映像,这可能是一个主要的性能消耗 您是否看过“现成”解决方案,例如?我只是:
<img src="/thumbnail.php?thumb=mybigpicture.ext" ... />
我是这样做的。请注意,这个实现可以缩放图像,无论图像是宽是高,还是宽是高,并且与大多数PHP尝试不同,它的缩放效果相当好
<?php
function thumb_image($request = "") {
$cfgthumb['folder'] = "/images/cache";
$cfgthumb['height'] = 150;
$cfgthumb['width'] = 200;
$cfgthumb['error'] = "/images/error.jpg";
$cfgthumb['default'] = "/images/notfound.jpg";
$thumb = $cfgthumb['folder'] . "/" . md5($request);
header("Content-Type: image/jpeg");
if (is_readable($thumb)) echo file_get_contents($thumb);
elseif (is_readable($request)) {
$extension = strtolower(end(explode(".", $request)));
switch ($extension) {
case "gif":
$simage = imagecreatefromgif($request);
break;
case "jpeg":
case "jpg":
$simage = imagecreatefromjpeg($request);
break;
case "png":
$simage = imagecreatefrompng($request);
break;
}
if ($simage) {
$simage_width = imagesx($simage);
$simage_height = imagesy($simage);
if (($simage_width > $cfgthumb['width']) || ($simage_height > $cfgthumb['height'])) {
if ($simage_width > $simage_height) {
$dimage_width = $cfgthumb['width'];
$dimage_height = floor($simage_height * ($cfgthumb['width'] / $simage_width));
} else {
$dimage_width = floor($simage_width * ($cfgthumb['height'] / $simage_height));
$dimage_height = $cfgthumb['height'];
}
} else {
$dimage_width = $simage_width;
$dimage_height = $simage_height;
}
$dimage = imagecreatetruecolor($dimage_width, $dimage_height);
imagegammacorrect($simage, 2.2, 1.0);
imagecopyresampled($dimage, $simage, 0, 0, 0, 0, $dimage_width, $dimage_height, $simage_width, $simage_height);
imagegammacorrect($dimage, 1.0, 2.2);
imagejpeg($dimage, $thumb, 100);
imagejpeg($dimage, NULL, 100);
imagedestroy($simage);
imagedestroy($dimage);
} else echo file_get_contents($cfgthumb['error']);
} else echo file_get_contents($cfgthumb['default']);
}
?>
我只是:
<img src="/thumbnail.php?thumb=mybigpicture.ext" ... />
我是这样做的。请注意,这个实现可以缩放图像,无论图像是宽是高,还是宽是高,并且与大多数PHP尝试不同,它的缩放效果相当好
<?php
function thumb_image($request = "") {
$cfgthumb['folder'] = "/images/cache";
$cfgthumb['height'] = 150;
$cfgthumb['width'] = 200;
$cfgthumb['error'] = "/images/error.jpg";
$cfgthumb['default'] = "/images/notfound.jpg";
$thumb = $cfgthumb['folder'] . "/" . md5($request);
header("Content-Type: image/jpeg");
if (is_readable($thumb)) echo file_get_contents($thumb);
elseif (is_readable($request)) {
$extension = strtolower(end(explode(".", $request)));
switch ($extension) {
case "gif":
$simage = imagecreatefromgif($request);
break;
case "jpeg":
case "jpg":
$simage = imagecreatefromjpeg($request);
break;
case "png":
$simage = imagecreatefrompng($request);
break;
}
if ($simage) {
$simage_width = imagesx($simage);
$simage_height = imagesy($simage);
if (($simage_width > $cfgthumb['width']) || ($simage_height > $cfgthumb['height'])) {
if ($simage_width > $simage_height) {
$dimage_width = $cfgthumb['width'];
$dimage_height = floor($simage_height * ($cfgthumb['width'] / $simage_width));
} else {
$dimage_width = floor($simage_width * ($cfgthumb['height'] / $simage_height));
$dimage_height = $cfgthumb['height'];
}
} else {
$dimage_width = $simage_width;
$dimage_height = $simage_height;
}
$dimage = imagecreatetruecolor($dimage_width, $dimage_height);
imagegammacorrect($simage, 2.2, 1.0);
imagecopyresampled($dimage, $simage, 0, 0, 0, 0, $dimage_width, $dimage_height, $simage_width, $simage_height);
imagegammacorrect($dimage, 1.0, 2.2);
imagejpeg($dimage, $thumb, 100);
imagejpeg($dimage, NULL, 100);
imagedestroy($simage);
imagedestroy($dimage);
} else echo file_get_contents($cfgthumb['error']);
} else echo file_get_contents($cfgthumb['default']);
}
?>
我不确定我的答案是否还来得及,但你为什么不简单地使用它呢?它可以做任何你需要的事情(包括缓存和缓存管理),所以你只需把它放进去就可以使用。不知道我的答案是否还来得及,但你为什么不简单地使用它呢?它可以做您需要的任何事情(包括缓存和缓存管理),因此您只需将其放入并使用即可。考虑到这种特殊情况,允许的分辨率数组听起来并不太糟糕。CMS上下文是可能的,我通常在我的CMS中使用CKeditor,可以编辑代码,使其像我希望的那样指向文件。我决定接受这个答案,因为我觉得这些解决方案在当前情况下最容易应用。谢谢。考虑到这种特殊情况,允许的分辨率数组听起来并不太糟糕。CMS上下文是可能的,我通常在我的CMS中使用CKeditor,可以编辑代码,使其像我希望的那样指向文件。我决定接受这个答案,因为我觉得这些解决方案在当前情况下最容易应用。谢谢。确实,重新生成缓存是第一次访问时的一个主要性能损失。对于这种特殊情况,网站是在IIS上运行的,我没有安装模块的权限。然而,对于我的其他项目,我肯定会研究这一点。谢谢。确实,重新生成缓存是第一次访问时的一个主要性能损失。对于这种特殊情况,网站是在IIS上运行的,我没有安装模块的权限。然而,对于我的其他项目,我肯定会研究这一点。谢谢。我已经有了这段代码,除了我的代码,它处理动态尺寸,加上我这样做的方式,我减少了服务器负载,让浏览器获得图像文件,而不是:服务器,然后是浏览器。问题是我需要一种方法来防止人们填满我的服务器。这可以通过@Pekka的允许维度数组来实现。如果这不能真正解决您的具体问题,那么很抱歉。我更愿意让他去