1

I want to check if any of the three functions inside the IF did not execute successfully. If any of them did not run, i want to get a return value false.

if($ext == "gif" or $ext == "png"){
    imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
    imagealphablending($new, false);
    imagesavealpha($new, true);
    }

For example I want to know if all the three functions imagecolortransparent, imagealphablending, imagesavealpha did execute successfully, if not, return false. Do i need to check each function like following or is there a better way?

if($ext == "gif" or $ext == "png"){
    if (!@imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127)))
        return false;
    if (!@imagealphablending($new, false))
        return false;
    if (!@imagesavealpha($new, true))
        return false;
}

Thanks.

1
  • i was just wondering if i need to return the false for each of the function, because in my image resize function im using many built in image related functions, so i have to return each of the function return value individually. Commented Jul 5, 2011 at 12:14

6 Answers 6

1
  if($ext == "gif" or $ext == "png"){
if ((!@imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127)))
    || (!@imagealphablending($new, false))
    || (!@imagesavealpha($new, true)) )
   {
        return false;
   }

}    

this will also do the same function. But in both the way you wont know which function failed.

Sign up to request clarification or add additional context in comments.

Comments

0

You can join them in 1 single if, but your method is probably the better way because it won't execute the following statements if one of the function calls fails.

1 Comment

The boolean && operator will short-circuit as well, stopping execution when a false is encountered.
0

You can set up a custom error handler that translates all PHP errors into ErrorExceptions and register it globally. Once you've done that, an error in any of the three functions will raise an exception, which you can handle with a try/catch block.

Comments

0
if (!@imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127))
  || !@imagealphablending($new, false)
  || !@imagesavealpha($new, true)) return false;

or just

return (@imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127))
     && @imagealphablending($new, false)
     && @imagesavealpha($new, true));

Don't know, if the @ is required here. You should try to avoid it.

Comments

0

Below is how I would do it.

if($ext == "gif" or $ext == "png"){
  $return = TRUE;
  $return = $return && imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
  $return = $return && imagealphablending($new, false);
  $return = $return && imagesavealpha($new, true);
  return $return;
}

Basically I am And-ing all the responses to verify that they are all true. If any one of them returns FALSE, the function will return FALSE.

There are more beautiful ways to do it, but I find this way to be reasonably clear exactly what is happening.

1 Comment

May I ask why the downvote? I'm always open to constructive criticism.
0

I think what you do is quite okay, you can concatenate the expressions however. It's doing the same, and in case a function returns false, the expression will not be followed for the rest (runtime optimization):

if($ext == "gif" or $ext == "png"){
    if (
        @imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127))
        and @imagealphablending($new, false)
        and @imagesavealpha($new, true)
    ) return true;
    return false;
}

Or by using a variable and sparing the second if thus making it more expressive:

if($ext == "gif" or $ext == "png")
    return 
      @imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127))
      && @imagealphablending($new, false)
      && @imagesavealpha($new, true)
    ;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.