我正在使用下面的代码在php中重新调整图像大小。它内部有一个功能。如果$ imgSource为true,则执行它。如果此中的某些内容失败,我希望它返回false(可能是imagecopyresampled失败或其他失败)。事情是,我在哪里放回真或假的陈述。现在,即使事情没有问题,它也会返回假。我是否必须为其中的所有内容编写if语句。你可以建议一个很好的方法来做到这一点。
if ($imgSource)
{
list($width,$height)=getimagesize($thisImage);
$dispImageWidth=500;
$dispImageHeight=($height/$width)*$dispImageWidth;
$tempDisplayImage=imagecreatetruecolor($dispImageWidth,$dispImageHeight);
$thumbImageWidth=250;
$thumbImageHeight=($height/$width)*$thumbImageWidth;
$tempThumbImage=imagecreatetruecolor($thumbImageWidth,$thumbImageHeight);
imagecopyresampled($tempDisplayImage,$imgSource,0,0,0,0,$dispImageWidth,$dispImageHeight,$width,$height);
imagecopyresampled($tempThumbImage,$imgSource,0,0,0,0,$thumbImageWidth,$thumbImageHeight,$width,$height);
$displayImageTarget = $thisPath.'disp_'.$fileName;
$thumbImageTarget = $thisPath.'thumb_'.$fileName;
imagejpeg($tempDisplayImage,$displayImageTarget,100);
imagejpeg($tempThumbImage,$thumbImageTarget,100);
imagedestroy($imgSource);
imagedestroy($tempDisplayImage);
imagedestroy($tempThumbImage);
unlink($thisImage);
//Where do I put the return true or false?
}
答案 0 :(得分:1)
做一些像if(! your statement ) return false;
<强> CODE 强> if($ imgSource) {
list($width,$height)=getimagesize($thisImage);
$dispImageWidth=500;
$dispImageHeight=($height/$width)*$dispImageWidth;
$tempDisplayImage=imagecreatetruecolor($dispImageWidth,$dispImageHeight);
$thumbImageWidth=250;
$thumbImageHeight=($height/$width)*$thumbImageWidth;
$tempThumbImage=imagecreatetruecolor($thumbImageWidth,$thumbImageHeight);
if(! imagecopyresampled($tempDisplayImage,$imgSource,0,0,0,0,$dispImageWidth,$dispImageHeight,$width,$height)) return false;
if(! imagecopyresampled($tempThumbImage,$imgSource,0,0,0,0,$thumbImageWidth,$thumbImageHeight,$width,$height)) return false;
$displayImageTarget = $thisPath.'disp_'.$fileName;
$thumbImageTarget = $thisPath.'thumb_'.$fileName;
if(!imagejpeg($tempDisplayImage,$displayImageTarget,100)) return false;
if(!imagejpeg($tempThumbImage,$thumbImageTarget,100)) return false;
if(!imagedestroy($imgSource)) return false;
if(!imagedestroy($tempDisplayImage)) return false;
if(!imagedestroy($tempThumbImage)) return false;
if(!unlink($thisImage)) return false;
return true;
}
如果您只想检查未设置的
只做return unlink($thisImage);
unlink成功时返回TRUE,失败时返回FALSE。请参阅php manual
答案 1 :(得分:1)
当某些内容失败时,您可以使用'Try catch'返回false。
if ($imgSource)
{
try
{
list($width,$height)=getimagesize($thisImage);
$dispImageWidth=500;
$dispImageHeight=($height/$width)*$dispImageWidth;
$tempDisplayImage=imagecreatetruecolor($dispImageWidth,$dispImageHeight);
$thumbImageWidth=250;
$thumbImageHeight=($height/$width)*$thumbImageWidth;
$tempThumbImage=imagecreatetruecolor($thumbImageWidth,$thumbImageHeight);
imagecopyresampled($tempDisplayImage,$imgSource,0,0,0,0,$dispImageWidth,$dispImageHeight,$width,$height);
imagecopyresampled($tempThumbImage,$imgSource,0,0,0,0,$thumbImageWidth,$thumbImageHeight,$width,$height);
$displayImageTarget = $thisPath.'disp_'.$fileName;
$thumbImageTarget = $thisPath.'thumb_'.$fileName;
imagejpeg($tempDisplayImage,$displayImageTarget,100);
imagejpeg($tempThumbImage,$thumbImageTarget,100);
imagedestroy($imgSource);
imagedestroy($tempDisplayImage);
imagedestroy($tempThumbImage);
unlink($thisImage);
return true;
}
catch(Exception $e)
{
return false;
}
//Where do I put the return true or false?
}
答案 2 :(得分:0)
许多图像函数根据成功或失败返回true或false,因此您可以执行以下操作:
if(imagejpeg($tempDisplayImage,$displayImageTarget,100)==false){
return false;
}
对于您支持的每个图像功能。
然后添加return true;在你的功能结束时。