使用一个 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
文件,它在隐藏真实源路径的同时获取站点的所有图形。
编辑:我不需要 Sessions 或 Cookies,我希望图片通过 PHP 永久显示在页面上,即使在用户 refreshes/reloads 页面之后。
编辑 2:否(我的)SQL。 PHP 文件本身必须是一种数据库,用于 存储和 显示图像。
我希望你自己付出努力。当您的代码有问题时,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!";
}
?>
您可以自行修改。
我希望有人能帮助我解决这个问题。也就是说,我需要一个 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 永久显示在页面上,即使在用户 refreshes/reloads 页面之后。
编辑 2:否(我的)SQL。 PHP 文件本身必须是一种数据库,用于 存储和 显示图像。
我希望你自己付出努力。当您的代码有问题时,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!";
}
?>
您可以自行修改。