使用一个PHP文件显示大量图像


Displaying Lots of Images Using One PHP File

我希望有人能帮我。也就是说,我需要一个PHP代码,它将根据图像的id在HTML/PHP页面中显示图像。例如:文件ShowPicture.php,其中的代码如下:

PictureID = "MyPicture1"
MyPicture1_Source = "/Pictures/Picture1.jpg";
PictureID = "MyPicture2"
MyPicture2_Source = "/Pictures/Picture2.png";
PictureID = "MyPicture3"
MyPicture3_Source = "/Pictures/Picture3.gif";
PictureID = "MyPicture4"
MyPicture4_Source = "/Pictures/Picture4.bmp";

页面上的用法示例:

HTML/PHP PAGE 1: <IMG src="ShowPicture.php?id=MyPicture4">
HTML/PHP PAGE 4: <IMG src="ShowPicture.php?id=MyPicture2">
HTML/PHP PAGE 2: <IMG src="ShowPicture.php?id=MyPicture3">
HTML/PHP PAGE 3: <IMG src="ShowPicture.php?id=MyPicture1">

我目前没有使用任何代码,因为我没有找到足够好的代码来满足我的特殊需求。该文件应该类似于Facebook的rsrc.php文件,它获取网站的所有图形,同时隐藏真实的源路径。

编辑:我不需要会话或Cookie,我希望图片通过PHP永久显示在页面上,即使在用户刷新/重新加载页面后也是如此。

第2版:没有(我的)SQL。PHP文件本身必须是存储和显示图像的数据库。

我想让你自己把efford放进去。当您的代码出现问题时,堆栈溢出可以帮助您。对于那些只想让某个程序员为他们做所有工作的人来说,这是不可能的。不管怎样,我现在已经花了几分钟为你做了,所以它在这里:

<?php
$id = $_GET["id"];
switch($id){
    case "MyPicture1":
        $file = "img/img1.jpg";
        break;
    case "MyPicture2":
        $file = "img/img2.jpg";
        break;
    case "MyPicture3":
        $file = "img/img3.jpg";
        break;
    case "MyPicture4":
        $file = "img/img4.jpg";
        break;
    default:
        echo "Invalid ID given!";
        exit;
}
if(file_exists($file)){
    $size = getimagesize($file);
    $fp = fopen($file, 'rb');
    if($size and $fp){
        header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($file)).' GMT');
        header('Content-Type: '.$size['mime']);
        header('Content-Length: '.filesize($file));
        fpassthru($fp);
    }
    exit;
} else {
    echo "File not found!";
}
?>

你可以自己进一步修改它。