1

Let's say I want to send the effect through a function argument, can I also send the additional arguments through that as well, can't really explain it, here is how I would imagine it.

<?php
//Apply photo effects
function applyEffect($url,$effect) {
    //List valid effects first


    $img = imagecreatefromjpeg($url);

    //Testing
    if($img && imagefilter($img, $effect)) {
        header('Content-Type: image/jpeg');
        imagejpeg($img);

        imagedestroy($img); 
    } else {
        return false;
    }
}

applyEffect("http://localhost:1234/ppa/data/images/18112013/0/image3.jpg",IMG_FILTER_BRIGHTNESS[20]);
?>

As you can see I pass IMG_FILTER_BRIGHTNESS through the function arguments, but the filter i'm using needs an additional argument which it would be nice to send when I call the applyEffect function, like so: IMG_FILTER_BRIGHTNESS[20]

But this does not work, any pointers?

5
  • That's really the same amount of characters either way. You might as well use a comma and pass it normally. Actually, it's less with a comma if you don't use a space. Commented Jan 15, 2014 at 2:54
  • But then i'd had to name the function like so: function applyEffect($url,$effect,$arg1,$arg2,$arg3) { Commented Jan 15, 2014 at 2:59
  • But now as the additional arguments have no value unless they are needed an error is thrown.. Commented Jan 15, 2014 at 3:00
  • In that context, I understand a little better why you might want it. Regarding the error, you can set defaults like this function foo($arg1, $arg2=null, $arg3=null) { Commented Jan 15, 2014 at 3:01
  • @m59 exactly what I wanted thanks! Update you answer with that! Commented Jan 15, 2014 at 3:13

1 Answer 1

2

It sounds like you would like func_get_args and then you could create the arguments for the next function call from that and use it like call_user_func_array(theFunction, $args).

function applyEffect($url, $effect, $vals) {
  $img = makeImage($url);

  //get an array of arguments passed in
  $args = func_get_args();

  //update the first item with the changed value
  $args[0] = $img;

  //get rid of the 3rd item, we're about to add on its contents directly to $args array
  unset($args[2]);

  //add all the optional arguments to the end of the $args array
  $args = array_merge($args, $vals);

  //pass the new args argument to the function call
  call_user_func_array(imagefilter, $args);
}

applyEffect('foo.jpg', 'DO_STUFF', array(20,40,90));


function imageFilter() {
  $args = func_get_args();
  foreach ($args as $arg) {
    echo $arg.'<br>';
  }
}

function makeImage($url) {
  return "This is an image.";
}

You can also set default argument values on functions like this:

function foo($arg1, $arg2=null, $arg3=null) { }

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

1 Comment

@MartynLeeBall I updated about the first solution. You might be interested in that also. It lets you pass however many values you want and the final function will be called with all of them.

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.