0

我正在使用下面的代码来重新调整 php 中的图像大小。它在一个函数内部。如果 $imgSource 为真,它就会执行。如果其中的某些内容失败,我希望它返回 false(可能 imagecopyresampled 失败或其他失败)。问题是,我在哪里放置 return true 或 false 声明。现在,即使事情进展顺利,它也会返回 false 。我是否必须为那里的所有内容编写 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?

}
4

3 回答 3

1

做一些像if(! your statement ) return 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);

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手册

于 2012-10-04T09:48:52.713 回答
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?

}
于 2012-10-04T09:57:21.520 回答
0

许多图像函数根据成功或失败返回真或假,因此您可以执行以下操作:

if(imagejpeg($tempDisplayImage,$displayImageTarget,100)==false){
    return false;
}

对于您使用的每个支持它的图像功能。

然后加上 return true; 在您的功能结束时。

于 2012-10-04T09:58:22.550 回答