Zebra_Image::apply_filter PHP Метод

apply_filter() публичный Метод

This method is available only if the {@link http://php.net/manual/en/function.imagefilter.php imagefilter} 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), ));
public apply_filter ( string $filter, mixed $arg1 = '', mixed $arg2 = '', mixed $arg3 = '', mixed $arg4 = '' ) : boolean
$filter string The (case-insensitive) name of the filter to apply. Can be one of the following: - brightness - changes the brightness of the image; use arg1 to set the level of brightness; the range of brightness is -255 to 255; - colorize - adds (subtracts) specified RGB values to each pixel; use arg1, arg2 and arg3 in the form of red, green, blue and arg4 for the alpha channel. the range for each color is -255 to 255 and 0 to 127 for alpha; alpha support is available only for PHP 5.2.5+; - contrast - changes the contrast of the image; use arg1 to set the level of contrast; the range of contrast is -100 to 100; - gausian_blur - blurs the image using the Gaussian method; - grayscale - converts the image into grayscale; - edgedetect - uses edge detection to highlight the edges in the image; - emboss - embosses the image; - mean_removal - uses mean removal to achieve a "sketchy" effect; - negate - reverses all the colors of the image; - pixelate - applies pixelation effect to the image, use arg1 to set the block size and arg2 to set the pixelation effect mode; this filter is available only for PHP 5.3.0+; - selective_blur - blurs the image; - smooth - makes the image smoother. Use arg1 to set the level of smoothness. applies a 9-cell convolution matrix where center pixel has the weight of arg1 and others weight of 1.0. the result is normalized by dividing the sum with arg1 + 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 boolean 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 mixed
$arg2 mixed
$arg3 mixed
$arg4 mixed
Результат boolean
    public function apply_filter($filter, $arg1 = '', $arg2 = '', $arg3 = '', $arg4 = '')
    {
        // if "imagefilter" function exists and the requested filter exists
        if (function_exists('imagefilter')) {
            // if image resource was successfully created
            if ($this->_create_from_source()) {
                // prepare the target image
                $target_identifier = $this->_prepare_image($this->source_width, $this->source_height, -1);
                // copy the original image
                imagecopyresampled($target_identifier, $this->source_identifier, 0, 0, 0, 0, $this->source_width, $this->source_height, $this->source_width, $this->source_height);
                // if multiple filters are to be applied at once
                if (is_array($filter)) {
                    // iterate through the filters
                    foreach ($filter as $arguments) {
                        // if filter exists
                        if (defined('IMG_FILTER_' . strtoupper($arguments[0]))) {
                            // try to apply the filter...
                            if (!@call_user_func_array('imagefilter', array_merge(array($target_identifier, constant('IMG_FILTER_' . strtoupper($arguments[0]))), array_slice($arguments, 1)))) {
                                // ...and trigger an error if the filter could not be applied
                                trigger_error('Invalid arguments used for "' . strtoupper($arguments[0]) . '" filter', E_USER_WARNING);
                            }
                            // if filter doesn't exists, trigger an error
                        } else {
                            trigger_error('Filter "' . strtoupper($arguments[0]) . '" is not available', E_USER_WARNING);
                        }
                    }
                    // if a single filter is to be applied and it is available
                } elseif (defined('IMG_FILTER_' . strtoupper($filter))) {
                    // get all the arguments passed to the method
                    $arguments = func_get_args();
                    // try to apply the filter...
                    if (!@call_user_func_array('imagefilter', array_merge(array($target_identifier, constant('IMG_FILTER_' . strtoupper($filter))), array_slice($arguments, 1)))) {
                        // ...and trigger an error if the filter could not be applied
                        trigger_error('Invalid arguments used for "' . strtoupper($arguments[0]) . '" filter', E_USER_WARNING);
                    }
                    // if filter doesn't exists, trigger an error
                } else {
                    trigger_error('Filter "' . strtoupper($arguments[0]) . '" is not available', E_USER_WARNING);
                }
                // write image
                return $this->_write_image($target_identifier);
            }
        }
        // if script gets this far, return false
        // note that we do not set the error level as it has been already set
        // by the _create_from_source() method earlier, if the case
        return false;
    }

Usage Example

Пример #1
0
 // and if there is an error, show the error message
 if (!$image->crop(0, 0, 50, 50)) {
     show_error($image->error, $image->source_path, $image->target_path);
 }
 // indicate a target image
 $image->target_path = 'results/rotate.' . $ext;
 // rotate
 // and if there is an error, show the error message
 if (!$image->rotate(45)) {
     show_error($image->error, $image->source_path, $image->target_path);
 }
 // indicate a target image
 $image->target_path = 'results/filter.' . $ext;
 // apply some filters
 // (this combination produces the "sepia" filter)
 $image->apply_filter(array(array('grayscale'), array('colorize', 90, 60, 40)));
 ?>
 <p>Table has background so that transparency can be observed.</p>
 <table style="background:#ABCDEF; border: 2px solid #666">
 	<tr>
         <td width="100" align="center">Resized to 100x100</td>
         <td width="100" align="center">Flipped horizontally</td>
         <td width="100" align="center">Flipped vertically</td>
         <td width="100" align="center">Flipped both horizontally and vertically</td>
         <td width="100" align="center">Cropped from 0, 0 to 50, 50</td>
         <td width="100" align="center">Rotated 45 degrees clockwise</td>
         <td width="100" align="center">Sepia<br>filter</td>
     </tr>
     <tr>
         <td align="center"><img src="results/resize.<?php 
 echo $ext;