以下功能应动态调整用户上传的图像大小,并循环播放以将副本放入5个不同的文件夹中。它可以正常工作,除了一个问题:循环成功执行一次然后停止。我已经检查过count()返回正确的上限值,即5.不确定为什么它在第一次迭代后停止。提前感谢您的帮助。
<?PHP
public function imgResize($image){
$userPath=$_SESSION['userFolder'].'/images';
$imgDetails=explode('/',$image);
$imgName=$imgDetails[2];
$newWidth='1024';
$newHeight='768';
$size=getimagesize($image);
$imgType=$size['mime'];
$reqdWidths=array('800','1024','1400','1600','2400');
$width=$size[0];
$height=$size[1];
$dest=imagecreatetruecolor($newWidth,$newHeight);
/*Put image create functions in a switch-case to deal with multiple image types*/
function createImage($img){
$size=getimagesize($img);
$typeImg=$size['mime'];
switch($typeImg){
case 'image/png':
$newImage=imagecreatefrompng($img);
return $newImage;
break;
case 'image/jpeg':
$newImage=imagecreatefromjpeg($img);
return $newImage;
break;
case 'image/gif' :
$newImage=imagecreatefromgif($img);
return $newImage;
break;
}
}
function finalImg($imgType,$dest,$userDestImage){
switch($imgType){
case 'image/png':
$final=imagepng($dest,$userDestImage);
return $final;
break;
case 'image/jpeg':
$final=imagejpeg($dest,$userDestImage);
return $final;
break;
case 'image/gif' :
$final=imagegif($dest,$userDestImage);
return $final;
break;
}
}
$source=call_user_func('createImage',$image);
/*LOOP AND CREATE DIFFERENT IMAGES IN DIFFERENT FOLDERS*/
for($i=0;$i<count($reqdWidths);$i++){
$userDestImage=$userPath.'/width_'.$reqdWidths[$i].'/'.$imgName;
imagecopyresized($dest,$source,0,0,0,0,$newWidth,$newHeight,$width,$height );
$imgFinal=call_user_func('finalImg',$imgType,$dest,$userDestImage);
return $imgFinal;
imagedestroy($dest);
}
}//END FUNCTION