简体   繁体   中英

getimagesize function is not working properly

I created a function, i am using this function to resize image to make it fit as div background image, i send div size and image path to function.. It works well when height is bigger than width but it does not work when width is bigger than height.. How can this be possible?

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;
}

Edit: This functions are used for the following code below.

<? $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>

You have a typo in function imgw($divsize, $imgw)

list($width, $height) = getimagesize($imgw);

right code

list($width, $height) = getimagesize($imgurl);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM