我已经为函数中的额外参数添加了默认值,现在有时需要,有时则不需要。这个函数有效,但标记了一个警告,因为它预期过滤器的3个参数但得到5,但显然$ arg2和$ arg3的值为NULL。
我能想到的唯一方法就是将'@'操作符放在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 #####
啊,我得到你们所说的所有感谢,所以她是我用你的指针解决的代码。谢谢大家!
<?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);
?>
答案 0 :(得分:2)
这是Imagefilter函数本身的抱怨,而不是关于参数计数或值的PHP:
$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
输出:
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和SMOOTH是罪魁祸首。
现在,您可以执行以下操作,而不是检查所有参数:
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;
}
或者如果你感到幸运(因为只有带有1个参数的函数抱怨)
if (func_num_args() == 4) imagefilter($img,$filter,$arg1);
else imagefilter($img,$filter,$arg1,$arg2,$arg3);
当然这是有风险的,因为较新版本的PHP可以执行更彻底的检查,并为具有0或2个参数的过滤器发出新警告。
答案 1 :(得分:0)
此外,PHP函数可以有变量参数......
function someFunc() {
$args = func_get_args();
// ...
}
它没有产生警告,AFAIK,所以你所说的让我感到惊讶。
答案 2 :(得分:0)
尝试使用unset()取消设置变量,是否会删除警告?
is_null($arg1) and unset($arg1);
is_null($arg2) and unset($arg2);
is_null($arg3) and unset($arg3);
PS:它可能会发出关于未定义变量的通知,但我仍然认为值得一试,只是为了看看会发生什么。