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

时间:2016-05-05 18:44:01

标签: php image loading using

我希望有人会帮助我。也就是说,我需要一个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文件,该文件在隐藏真实源路径的同时获取网站的所有图形。

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

编辑2:否(我的)SQL。 PHP文件本身和单独的必须是存储和显示图像的数据库。

1 个答案:

答案 0 :(得分:1)

我希望你自己把efford放进去。当您的代码出现问题时,Stack Overflow可以帮助您。对于那些只想让一些程序员为他们完成所有工作的人来说,这不是一件容易的事。无论如何,我现在花了几分钟时间为你做这件事,所以这就是:

<?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!";
}

?>

您可以自己修改它。