当$image/$imgsrc的路径上有一个空格时,下面的代码返回一个空格。例如www.domain.com/my pic.gif
但是,如果您在其中添加%20,则正常工作。我如何修改它,以便当图像有一个空间运行正确而不编辑路径?
谢谢!
<?php function resizeImg($imgsrc ,$maxW='*', $maxH='*', $allowScaleUp=0, $returnHTML="alt='image'"){
if($s=getimagesize($imgsrc)){
$oW=$s[0];$oH=$s[1];
if(($oW>$maxW && $maxW!='*') || ($oH>$maxH && $maxH!='*') || $allowScaleUp){//if resize is needed:
if($maxW && $maxH=='*'){ //constrain by width:
$proportion=$oH/$oW;
$w=$maxW;
$h=$maxW*$proportion;
}else if($maxH && $maxW=='*'){ //constrain by height:
$proportion=$oW/$oH;
$h=$maxH;
$w=$maxH*$proportion;
}else if(!$maxW && $maxH){ //constrain by smallest side:
return($oW>$oH ? resizeImg($imgsrc, '*', $maxH, $allowScaleUp, $returnHTML) : resizeImg($imgsrc, $maxW, '*', $allowScaleUp, $returnHTML));
}else if($maxW && !$maxH){ //constrain by largest side:
return($oW>$oH ? resizeImg($imgsrc, $maxW, '*', $allowScaleUp, $returnHTML) : resizeImg($imgsrc, '*', $maxH, $allowScaleUp, $returnHTML));
}else{
return($maxW>$maxH ? resizeImg($imgsrc, '*', $maxH, $allowScaleUp, $returnHTML) : resizeImg($imgsrc, $maxW, '*', $allowScaleUp, $returnHTML));
}
}else{
$w=$oW;$h=$oH;
}
//echo "orig: ".$oW."x:".$oH."<br />max: ".$maxW."x".$maxH."<br />new: ".$w."x".$h."<br />"; //debug
$w=round($w); $h=round($h);
return ($returnHTML ? "<img src='$imgsrc' width='$w' height='$h' $returnHTML />" : array(0=>$w,1=>$h,"width"=>$w,"height"=>$h));
}else{//file does not exist or is not an image:
return false;
}
}
?>
<?php echo resizeImg($picture,250,'*') ?>
是的,如果使用URI绑定,getimagesize需要一个正确的、有效的URI。因此,将空格替换为%20
(参见示例#3)。
urlencode
编码各种实体,所以我的建议是:if ($s = getimagesize(str_replace(' ', '%20', $imgsrc))) {