The code returns an error when the image path has a space on it. How to correct it through the code?
The following code returns an eroor when the $image/$imgsrc has a space on it's path. For example www.domain.com/my pic.gif
However if you add a %20 in it, works correctly. How can I modify it so when the image has a space to run correct without editing the path?
Thank you!
<?php functi开发者_C百科on 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,'*') ?>
Yea, getimagesize requires a proper, valid URI if you use the URI binding. So, replace spaces with %20
(see example #3).
urlencode
encodes all sorts of entities, so my recommendation is: if ($s = getimagesize(str_replace(' ', '%20', $imgsrc))) {
精彩评论