Reputation: 4895
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?
Upvotes: 1
Views: 62
Reputation: 43795
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) { }
Upvotes: 2