apply_filter()
apply_filter(string $filter, $arg1 = '', $arg2 = '', $arg3 = '', $arg4 = '')
Applies one or more filters to the image given as {@link source_path} and outputs it as the file specified as
{@link target_path}.
This method is available only if the [imagefilter](http://php.net/manual/en/function.imagefilter.php)
function is available (available from PHP 5+), and will leave images unaltered otherwise.
// include the Zebra_Image library
require 'path/to/Zebra_Image.php';
// instantiate the class
$img = new Zebra_Image();
// a source image
$img->source_path = 'path/to/source.ext';
// path to where should the resulting image be saved
// note that by simply setting a different extension to the file will
// instruct the script to create an image of that particular type
$img->target_path = 'path/to/target.ext';
// apply the "grayscale" filter
$img->apply_filter('grayscale');
// apply the "contrast" filter
$img->apply_filter('contrast', -20);
You can also apply multiple filters at once. In this case, the method requires a single argument, an array of
arrays, containing the filters and associated arguments, where applicable:
// create a sepia effect
// note how we're applying multiple filters at once
// each filter is in its own array
$img->apply_filter(array(
// first we apply the "grayscale" filter
array('grayscale'),
// then we apply the "colorize" filter with 90, 60, 40 as
// the values for red, green and blue
array('colorize', 90, 60, 40),
));
Parameters
string |
$filter |
The (case-insensitive) name of the filter to apply. Can be one of the following:
- <b>brightness</b> - changes the brightness of the image; use <b>arg1</b>
to set the level of brightness; the range of brightness
is -255 to 255;
- <b>colorize</b> - adds (subtracts) specified RGB values to each pixel;
use <b>arg1</b>, <b>arg2</b> and <b>arg3</b> in the
form of red, green, blue and <b>arg4</b> for the alpha
channel. the range for each color is -255 to 255 and
0 to 127 for alpha; <i>alpha support is available only
for PHP 5.2.5+</i>;
- <b>contrast</b> - changes the contrast of the image; use <b>arg1</b>
to set the level of contrast; the range of contrast
is -100 to 100;
- <b>gausian_blur</b> - blurs the image using the Gaussian method;
- <b>grayscale</b> - converts the image into grayscale;
- <b>edgedetect</b> - uses edge detection to highlight the edges in the image;
- <b>emboss</b> - embosses the image;
- <b>mean_removal</b> - uses mean removal to achieve a "sketchy" effect;
- <b>negate</b> - reverses all the colors of the image;
- <b>pixelate</b> - applies pixelation effect to the image, use <b>arg1</b>
to set the block size and <b>arg2</b> to set the
pixelation effect mode; <i>this filter is available
only for PHP 5.3.0+</i>;
- <b>selective_blur</b> - blurs the image;
- <b>smooth</b> - makes the image smoother. Use <b>arg1</b> to set the
level of smoothness. applies a 9-cell convolution matrix
where center pixel has the weight of <b>arg1</b> and
others weight of 1.0. the result is normalized by dividing
the sum with <b>arg1</b> + 8.0 (sum of the matrix).
any float is accepted;
@param mixed $arg1 Used by the following filters:
- brightness - sets the brightness level (-255 to 255)
- contrast - sets the contrast level (-100 to 100)
- colorize - sets the value of the red component (-255 to 255)
- smooth - sets the smoothness level
- pixelate - sets the block size, in pixels
@param mixed $arg2 used by the following filters:
- colorize - sets the value of the green component (-255 to 255)
- pixelate - whether to use advanced pixelation effect or not (defaults to FALSE)
@param mixed $arg3 Used by the following filters:
- colorize - sets the value of the blue component (-255 to 255)
@param mixed $arg4 Used by the following filters:
-
colorize - alpha channel; a value between 0 and 127. 0 indicates
completely opaque while 127 indicates completely
transparent.
@since 2.2.2
@return bool Returns TRUE on success or FALSE on error.
If {@link http://php.net/manual/en/function.imagefilter.php imagefilter} is not
available the method will return FALSE without setting an {@link error} code.
If the requested filter doesn't exist, or invalid arguments are passed, the method
will trigger a warning.
If FALSE is returned and you are sure that
{@link http://php.net/manual/en/function.imagefilter.php imagefilter} exists and that
the requested filter is valid, check the {@link error} property to see the error code.
|
|
$arg1 |
|
|
$arg2 |
|
|
$arg3 |
|
|
$arg4 |
|