PHP expected exactly 3 parameters, ignore the additional ones to remove the warning?

StackOverflow https://stackoverflow.com/questions/21144207

  •  28-09-2022
  •  | 
  •  

I have added default values for the extra arguments in the function, now sometimes there are needed and other times not. This function works, but flags up a warning as it expected 3 parameters for the filter but got 5, but obviously $arg2 and $arg3 have the value of NULL.

The only way I can think of doing it is to put the '@' operator in front of imagefilter.

//Apply photo effects
function applyEffect($url,$save,$filter,$arg1=NULL,$arg2=NULL,$arg3=NULL) {
    //Ensure filter is in allcaps
    $filter = strtoupper($filter);

    //List valid effects first
    $list_effects = array(
        IMG_FILTER_NEGATE,
        IMG_FILTER_GRAYSCALE,
        IMG_FILTER_BRIGHTNESS,
        IMG_FILTER_CONTRAST,
        IMG_FILTER_COLORIZE,
        IMG_FILTER_EDGEDETECT,
        IMG_FILTER_EMBOSS,
        IMG_FILTER_SMOOTH,
        IMG_FILTER_MEAN_REMOVAL
    );

    //Check to see if the filter exists
    if (in_array($filter,$list_effects)) {
        //Has image loaded into memory
        if($img=imagecreatefromjpeg($url)) {
            //Apply filter
            imagefilter($img,$filter,$arg1,$arg2,$arg3);

....

#####EDIT#####

Ah I get what you are all saying thanks, so her is my solved code using your pointers. Thanks all!

<?php
//Apply photo effects
function applyEffect($url,$save,$filter) {
    $args = func_get_args();
    $url = $args[0];
    $save = $args[1];
    $filter = $args[2];

    //Ensure filter is in allcaps
    $filter = strtoupper($filter);
    //List valid effects first
    $list_effects = array(
        "NEGATE"       => IMG_FILTER_NEGATE,
        "GRAYSCALE"    => IMG_FILTER_GRAYSCALE,
        "BRIGHTNESS"   => IMG_FILTER_BRIGHTNESS,
        "CONTRAST"     => IMG_FILTER_CONTRAST,
        "COLORIZE"     => IMG_FILTER_COLORIZE,
        "EDGEDETECT"   => IMG_FILTER_EDGEDETECT,
        "EMBOSS"       => IMG_FILTER_EMBOSS,
        "SMOOTH"       => IMG_FILTER_SMOOTH,
        "MEAN_REMOVAL" =>IMG_FILTER_MEAN_REMOVAL
    );
    //Check to see if the filter exists
    if (in_array($filter,$list_effects)) {
        //Has image loaded into memory
        if($img = imagecreatefromjpeg($url)) {

            switch (func_num_args()) {
                case 3: $applyeffect = imagefilter($img,$list_effects[$filter]); break;
                case 4: $applyeffect = imagefilter($img,$list_effects[$filter],$args[3]); break;
                case 5: $applyeffect = imagefilter($img,$list_effects[$filter],$args[3],$args[4]); break;
                case 6: $applyeffect = imagefilter($img,$list_effects[$filter],$args[3],$args[4],$args[5]); break;
            }
            if ($applyeffect) {
                //Save the image
                $saveimage = imagejpeg($img,$save);
                if($saveimage == false) {
                    //Unable to save the image
                    return "Unable to apply filter!(unable to save image)";
                } else {
                    //Image successfuly saved
                    return "Filter successfully applied!";  
                }
                //Free up memory
                imagedestroy($img);
            } else {
                return "Unable to apply filter";    
            }
        } else {
            return "Image unable to load into memory!";
        }
    } else {
        //Filter doesn't exist
        return "Unable to find filter!";
    }
}

$imgUrl = "http://localhost:1234/ppa/data/images/18112013/0/image3.jpg";
$saveUrl = "C:/xampp/htdocs/ppa/data/images/18112013/0/image3_applied.jpg";
echo applyEffect($imgUrl,$saveUrl,"BRIGHTNESS",20);
?>
有帮助吗?

解决方案

It's the Imagefilter function itself complaining, not PHP croaking about argument count or value:

$image = imagecreatefrompng('filter.png');
$list_effects = array(
        "NEGATE"     => IMG_FILTER_NEGATE,
        "GRAYSCALE"  => IMG_FILTER_GRAYSCALE,
        "BRIGHTNESS" => IMG_FILTER_BRIGHTNESS,
        "CONTRAST"   => IMG_FILTER_CONTRAST,
        "COLORIZE"   => IMG_FILTER_COLORIZE,
        "EDGEDETECT" => IMG_FILTER_EDGEDETECT,
        "EMBOSS"     => IMG_FILTER_EMBOSS,
        "SMOOTH"     => IMG_FILTER_SMOOTH,
        "MEAN_REMOVAL" => IMG_FILTER_MEAN_REMOVAL
    );
foreach ($list_effects as $name => $effect)
{
echo "$name<br>";
imagefilter($image, $effect, NULL, NULL, NULL); // <-- passing NULL is OK

output:

NEGATE
GRAYSCALE
BRIGHTNESS // <-- argument count is a problem for 3 of the primitives
Warning: imagefilter() expects exactly 3 parameters, 5 given in ...
CONTRAST  
Warning: imagefilter() expects exactly 3 parameters, 5 given in ...
COLORIZE
EDGEDETECT
EMBOSS
SMOOTH
Warning: imagefilter() expects exactly 3 parameters, 5 given in ...
MEAN_REMOVAL

BRIGHTNESS, CONTRAST and SMOOTH are the culprits.

Now instead of checking all parameters, you could do something like that:

switch (func_num_args())
{
case 3: imagefilter($img,$filter); break;
case 4: imagefilter($img,$filter,$arg1); break;
case 5: imagefilter($img,$filter,$arg1,$arg2); break;
case 6: imagefilter($img,$filter,$arg1,$arg2,$arg3); break;
}

or if you feel lucky (since only the functions with 1 parameter complain)

if (func_num_args() == 4) imagefilter($img,$filter,$arg1); 
else imagefilter($img,$filter,$arg1,$arg2,$arg3); 

Of course this is risky, since a newer version of PHP could perform more thorough checks and give off new warnings for filters with 0 or 2 parameters.

其他提示

Uh, why are you not feeding it more arguments than it needs?

Also, PHP functions can have variable arguments...

function someFunc() {
    $args = func_get_args();
    // ...
}

And it does not produce a warning, AFAIK, so what you're saying surprises me a bit.

Try unsetting the variables with unset(), does it remove the warning?

is_null($arg1) and unset($arg1);
is_null($arg2) and unset($arg2); 
is_null($arg3) and unset($arg3); 

PS: It would probably issue a notice about undefined variables, but I still think it's worth a shot, if only to see what happens.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top