我创建了一个函数,我正在使用此函数调整图像大小以使其适合作为 div 背景图像,我将 div 大小和图像路径发送给函数。当高度大于宽度时效果很好,但它不起作用当宽度大于高度时。这怎么可能?
function imgw($divsize, $imgw){
$imgurl=$_SERVER["DOCUMENT_ROOT"]."/".$imgw;
list($width, $height) = getimagesize($imgw);
if($width > $height){$nwidth="auto";}
if($width < $height){$nwidth=$divsize."px";}
if($width==$height){$nwidth=$divsize."px";}
return $nwidth;
}
function imgh($divsize, $imgh){
$imgurl=$_SERVER["DOCUMENT_ROOT"]."/".$imgh;
list($width, $height) = getimagesize($imgurl);
if($width > $height){$nheight=$divsize."px";}
if($width < $height){$nheight="auto";}
if($width==$height){$nheight=$divsize."px";}
return $nheight;
}
编辑:此函数用于下面的代码。
<? $anwidth=imgw(70, $solsutunpic); $anheight=imgh(70, $solsutunpic);?>
<div style='background: url(<?php echo $solsutunpic ?>) no-repeat; background-size: <?php echo $anwidth ?> <?php echo $anheight ?>; width: 70px; height: 70px; display:inline-block; border-radius: 3px; margin-right:5px;'></div>
最佳答案
你在函数 imgw($divsize, $imgw) 中有错别字
list($width, $height) = getimagesize($imgw);
正确的代码
list($width, $height) = getimagesize($imgurl);
关于php - getimagesize 函数无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24227548/