我使用下面的代码重新调整大小的图像在PHP中。它的内部功能。它在$ imgSource为true时执行。如果这个失败了,我希望它返回false(也许imagecopyresampled失败或其他失败)。事情是,我在哪里放置返回真或假陈述。现在即使事情好转也不会返回错误。我是否必须写出如果所有在那里的声明。你能否建议一个好方法来做到这一点。我在哪里添加返回true或false在这里
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?
}
将它包裹在try catch中,在try的结尾添加return true,在catch中返回false。尽管如此,这依赖于gd函数抛出异常。 –