gpt4 book ai didi

php - html/php 随机图像生成器 - 根据屏幕尺寸对齐图像

转载 作者:太空宇宙 更新时间:2023-11-04 01:23:07 25 4
gpt4 key购买 nike

大家好,谢谢大家

请帮助我修复下面的 html 代码,我试图在每次用户/浏览器刷新页面时刷新图像(随机顺序),此时它工作正常。但是图像不会自动调整大小以适应屏幕,换句话说,如果图像比屏幕大,那么我必须向下/向右滚动才能看到图像请帮我解决这个让我发疯的问题

我必须将文件夹命名为domain.com/index.php包含所有图像的 domain.com/images 文件夹

	<!DOCTYPE html>
<html>
<body style="background-color:transparent;">

<style type="text/css">
.myImage
{
// margin: auto;
// display: block;
height: auto;
width: auto;
// padding: 0px;
//object-fit: contain
// overflow-x: auto;
// white-space: nowrap;
}
</style>


<?php
$folder = opendir(images);

$i = 0;
while(false !=($file = readdir($folder))){
if($file != "." && $file != ".."){
$images[$i]= $file;
$i++;
}
}
$random_img=rand(0,count($images)-1);
echo '<img src="images/'.$images[$random_img].'" alt="image" class="myImage" ';

?>

</body>
</html>

最佳答案

首先,您没有关闭标签。您缺少结束符 >。我知道当你已经做了一段时间的工作时,这些东西有时很容易被忽略。

它应该是这样的:

echo '<img src="images/'.$images[$random_img].'" alt="image" class="myImage">';

您可能能够解决您的问题,但将这部分 HTML 放在另一个 div 中并为该 div 定义一个宽度,使其小于您的屏幕尺寸。

例如:

echo '<div class="imgbox">';
echo '<img src="images/'.$images[$random_img].'" alt="image" class='myImage'>';
echo '</div>';

然后使用固定像素或响应百分比为其添加 CSS

.imgbox{
width: px OR %;
height: px OR %;
}

编辑:

    <!DOCTYPE html>
<html>
<body style="background-color:transparent;">

<style type="text/css">
.myImage{
// margin: auto;
// display: block;
height: auto;
width: auto;
// padding: 0px;
//object-fit: contain;
// overflow-x: auto;
// white-space: nowrap;
}

.imgbox{
width: set px OR % values here;
height: set px OR % values here;
}
</style>


<?php
$folder = opendir(images);

$i = 0;
while(false !=($file = readdir($folder))){
if($file != "." && $file != ".."){
$images[$i]= $file;
$i++;
}
}
$random_img=rand(0,count($images)-1);
echo '<div class="imgbox">';
echo '<img src="images/'.$images[$random_img].'" alt="image" class="myImage">';
echo '</div>';

?>

</body>
</html>

关于php - html/php 随机图像生成器 - 根据屏幕尺寸对齐图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48673702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com