You can apply a filter on a layer and its sublayers.

$arg1 = $arg2 = $arg3 = $arg4 = null;
$recursive = false; // apply the filter on sublayers

$layer->applyFilter($filterType, $arg1, $arg2, $arg3, $arg4, $recursive);

Warning: rather than using the recursive attribute, merge your layers if possible before applying a filter because it could be a possible source of display bugs.

Lets see it in action, we will apply a simple negative filter on the layer:

$layer->applyFilter(IMG_FILTER_NEGATE); // With this filter, we can ignore other params...

Our filter in image: