I know the imagefilter function expects a long but is there a way to cast a variable to a long or am I forced to simply create separate functions for each filter. My thought is this:

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);
    }
}

It's complaining about my $filter variable. For this example my $filter value is: IMG_FILTER_GRAYSCALE

Is this possible?

有帮助吗?

解决方案

Provided:

$filter = "IMG_FILTER_GRAYSCALE"

You should be able to use the function constant:

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

However note that the following will also work just fine:

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

You can pass around the constant as an argument without a problem if you need to do so. The former is only useful if you really need the constant name to be dynamic.

其他提示

Casting is made this way:

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

$var = (int) "123";

The following function would do what you need:

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);
    }
}

Then use it like this:

$this->ImgFilter(IMG_FILTER_GRAYSCALE);
$this->ImgFilter(IMG_FILTER_COLORIZE, array(0, 255, 0));
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top