0

我知道该imagefilter函数需要一个 long 但有没有办法将变量转换为 long 或者我被迫简单地为每个过滤器创建单独的函数。我的想法是这样的:

public function ImgFilter($filter, $arg1=null, $arg2=null){
    $this->lazyLoad();
    if($this->_cache_skip) return;
    if(isset($this->_image_resource)){
        imagefilter($this->_image_resource, $filter);
    }
}

它在抱怨我的$filter变量。对于这个例子,我的$filter价值是:IMG_FILTER_GRAYSCALE

这可能吗?

4

3 回答 3

3

假如:

$filter = "IMG_FILTER_GRAYSCALE"

您应该能够使用函数常量

imagefilter($this->_image_resource, constant($filter));

但是请注意,以下内容也可以正常工作:

$filter = IMG_FILTER_GRAYSCALE
imagefilter($this->_image_resource, $filter);

如果需要,您可以毫无问题地将常量作为参数传递。前者仅在您确实需要动态常量名称时才有用。

于 2012-07-12T14:30:26.297 回答
0

以下功能将满足您的需求:

public function ImgFilter($filter, $arguments = array())
{
    $this->lazyLoad();

    if ($this->_cache_skip) {
        return;
    }

    if (isset($this->_image_resource)) {
        $params = array($this->_image_resource, $filter);

        if (!empty($arguments)) {
            $params = array_merge($params, $arguments);
        }

        call_user_func_array('imagefilter', $params);
    }
}

然后像这样使用它:

$this->ImgFilter(IMG_FILTER_GRAYSCALE);
$this->ImgFilter(IMG_FILTER_COLORIZE, array(0, 255, 0));
于 2012-07-12T14:32:33.877 回答
0

铸造是这样进行的:

<holder> = (<type>) <expression>

$var = (int) "123";
于 2012-07-12T14:30:39.087 回答