【问题标题】:Where do I add a return true or false here我在哪里添加 return true 或 false 在这里
【发布时间】:2023-03-09 17:48:01
【问题描述】:

我正在使用下面的代码来重新调整 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?

}

【问题讨论】:

  • 把它包裹在一个try catch中,在try的最后加上return true,在catch中,return false。这取决于 gd 函数在错误时抛出异常。

标签: php


【解决方案1】:

做一些类似if(! your statement ) return false;的事情

代码 如果($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

【讨论】:

  • 不确定这是否真的回答了 OP 问题?
  • @JonStirling:对不起,我误解了阙
【解决方案2】:

您可以使用 '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?

}

【讨论】:

    【解决方案3】:

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

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

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

    然后加上return true;在函数结束时。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-09
      • 1970-01-01
      • 1970-01-01
      • 2020-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多