gpt4 book ai didi

php - 如何根据屏幕大小在php中调整图像大小?

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

这类问题已经有人问过,但没有一个很有用。在这里,我使用 php 从数据库中获取图像并显示它。我希望图像根据屏幕尺寸自行调整,并且应该有最大宽度限制。我应该如何在 php 中执行此操作?我尽力去做,但没有成功。

<?php
$searchword=$_POST['searchword'];
$con=mysqli_connect('localhost','root','','bookspyramid');
?>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<?php
$sql="SELECT imgno FROM bookinfo where bookname='$searchword'";
if($result=mysqli_query($con,$sql))
{
if(mysqli_num_rows($result)>0)
{
while($row=mysqli_fetch_array($result))
{
$imgname=$row['imgno'];
echo "<img src=imgfile/".$imgname." height= width= >";
}
}
}
else
echo "Sorry! No results found!";
?>
</body>
</html>

图像显示并具有固定尺寸,但我希望它的尺寸​​根据屏幕尺寸而改变。

最佳答案

为此目的,通过 CSS 使用相对大小,如 80% 等。

您还可以调整其他属性,例如 alignment、position、max-width、max-height 等。它会根据可用尺寸调整图像。

<?php
$searchword=$_POST['searchword'];
$con=mysqli_connect('localhost','root','','bookspyramid');
?>
<html>
<head>
<style>
.myImg {
max-width:100%;
width: 80%;
height: auto;
margin: auto auto; /* align center */
}
</style>

<meta name="viewport" content="width=device-width, initial-scale=1"/>
<?php
$sql="SELECT imgno FROM bookinfo where bookname='$searchword'";
if($result=mysqli_query($con,$sql))
{
if(mysqli_num_rows($result)>0)
{
while($row=mysqli_fetch_array($result))
{
$imgname=$row['imgno'];
echo "<img class='myImg' src='imgfile/".$imgname."'>"; //add '' inside "" around imgfile to avoid img name errors
}
}
}
else
echo "Sorry! No results found!";
?>
</body>
</html>

您也可以为此目的使用内联 CSS

echo "<img style='width:80%; height: auto; max-width: 100%; margin: auto;' 
src='imgfile/".$imgname."'>";

关于php - 如何根据屏幕大小在php中调整图像大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56098165/

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