【发布时间】:2012-02-05 06:31:47
【问题描述】:
为了安全起见,我正在显示一个存储在根目录下的图片库。每个 jpeg 都有缩略图。显示图库的时候,我已经设置成功了
<img src='./php/getfile.php?file=xyz-thumb.jpg'></a>
getfile.php 使用以下代码处理每个缩略图。单击缩略图时,相同的代码会加载更大版本的图像。
我已经可以看出这段代码比 html 慢,并且一个页面上可能有 20-30 个缩略图,我正在讨论是否需要保持缩略图对 public_html 可见以提高性能。有没有更快的方法来显示缩略图?出于其他原因,fpassthru() 是否更快或更理想?
// File Exists?
if( file_exists($fullfilename)){
// Parse Info / Get Extension
$fsize = filesize($fullfilename);
$path_parts = pathinfo($fullfilename);
$ext = strtolower($path_parts["extension"]);
// Determine Content Type
switch ($ext) {
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: $ctype");
if ($mode == "view"){
// View file
header('Content-Disposition: inline; filename='.basename($fullfilename));
}
else {
// Download file
header('Content-Disposition: attachment; filename='.basename($fullfilename));
}
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$fsize);
ob_clean();
flush();
readfile( $fullfilename );
} else
die('File Not Found:' . $fullfilename);
【问题讨论】:
-
将缩略图存储在 Web 根目录下并使用 php 提供什么安全性?
-
安全性适用于全尺寸图像。将缩略图保留在根目录下的唯一原因是将它们保留在同一目录中很方便。否则我必须为完整尺寸的图像复制一个复杂的目录结构。我将数百张图片分组到许多目录中。
-
php coder,你可以将图片返回给你想要的用户,它不会阻止复制只是未经授权的查看,为了改善这种查看方式你可以添加缓存
-
我假设必须一次查看一个图像,右键单击并保存。如果它们完全可见,我担心有人能够自动复制它们。如果这过于偏执或无效,我很高兴听到不值得付出努力或影响性能。
-
正常的、未经授权的用户是否能够简单地单击一个链接,该链接将在任何时候向他们显示全尺寸图像?如果是,那么通过 PHP 为它们提供服务绝对没有意义,因为它们是可公开访问的。无论您在幕后挥手多少次,如果一个可公开访问的 URL 指向完整尺寸的图像,这就是所有客户所关心的。
标签: php performance image src