Home  >  Article  >  Backend Development  >  How to apply a filter to an image using the imagefilter() function in PHP?

How to apply a filter to an image using the imagefilter() function in PHP?

WBOY
WBOYforward
2023-09-07 09:45:021213browse

imagefilter() is a built-in function in PHP that is used to apply a given filter to an image.

Syntax

bool imagefilter(resource $image, int $filtertype, int $arg1, int $arg2, int $arg3, int $arg4)

Parameters

imagefilter() Takes six different parameters - $image, int $filtertype, int $arg1, int $arg2, int $arg3, int $arg4.

  • $image - It holds image resources.

  • $filtertype - Specifies the filter to use, which is an integer.

Below are the different image filter constants given - p>

  • ##IMG_FILTER_NEGATE - Inverts all colors of the image.

  • IMG_FILTER_GRAYSCALE - Converts the image to grayscale by changing the red, green and blue components to their weighted sum.

  • IMG_FILTER_BRIGHTNESS - Change the brightness of the image. arg1 is used to set the brightness level. Brightness range is -255 to 255.

  • IMG_FILTER_CONSTRAST - Changes the contrast of the image. $arg1 is used to set the contrast level.

  • IMG_FILTER_COLORIZE - This image filter is similar to IMG_FILTER_GARYSCALE except we can specify the color, it uses parameters arg1, arg2 and $arg3 , in the form of red, green, blue, arg4 is used for the Alpha channel. Each color ranges from 0 to 255.

  • IMG_FILTER_EDGEDETECT - This filter is used for edge detection to highlight edges in an image.

  • IMG_FILTER_GAUSSIAN_BLUR - Applies a Gaussian blur to the image.

  • IMG_FILTER_SELECTIVE_BLUR > - Applies a selective blur to the image.

  • IMG_FILTER_EMBOSS - Applies an embossing to the image.

  • IMG_FILTER_MEAN_REMOVAL - Removes noise from images and provides a rough effect.
  • IMG_FILTER_SMOOTH - Makes the image smoother. $arg1 is used to set the smoothness.

  • IMG_FILTER_PIXELATE - Apply pixelation effect to the image. $arg1 is used to set the block size, $arg2 is used to set the pixelation effect mode. p>

  • IMG_FILTR_SCATTER - Applies a scattering effect to the image. $arg1 and arg2 are used to define the effect strength, $arg3 is used to define the effect strength for application to the selected pixel color.

Optional parameter list

arg1

  • IMG_FILTER_BRIGHTNESS - Use at the brightness level.

  • IMG_FILT_CONTRAST - Value used for contrast

  • IMG_FILTER_COLORIZE - Value used for the red component.

  • IMG_FILTER_SMOOTH - for smoothness.

  • IMG_FILTER_PIXELATE - For block size in pixels.

  • IMG_FILTER_SCATTER - Used for effect deduction levels.

arg2

  • IMG_FILTER_COLORIZE - Value to use for the blue component.

  • IMG_FILTER_PIXELATE - Whether to use advanced pixelation effect (default is false).

  • IMG_FILTER_SCATTER - Affects the added level. >

arg3

  • IMG_FILTER_COLORIZE - Use the value of the blue component.

  • IMG_FILTER_SCATTER - Optional array of indexed color values ​​used to apply the effect.

  • arg4

    • IMG_FILTER_COLORIZE - Alpha channel, value between 0 and 127 . 0 means fully opaque, 127 means fully transparent.

    Return value

    Returns True on success and False on failure.

    Example 1
    <?php
       // Load the gif image from the local drive folder.
       $img = imagecreatefromgif(&#39;C:\xampp\htdocs\Images\img39.gif&#39;);
    
       // Colorize the image
       imagefilter($img, IMG_FILTER_COLORIZE, 140, 0, 140, 20);
    
       // Show the output image
       header(&#39;Content-type: image/gif&#39;);
       imagepng($img);
    ?>
    Output

    How to apply a filter to an image using the imagefilter() function in PHP?

    Example 2

    <?php
       // Load the gif image from the local drive folder.
       $img = imagecreatefromgif(&#39;C:\xampp\htdocs\Images\img39.gif&#39;);
    
       // Negative the image
       imagefilter($img, IMG_FILTER_NEGATE);
    
       // Show the output image
       header(&#39;Content-type: image/gif&#39;);
       imagepng($img);
    ?>

    Output

    How to apply a filter to an image using the imagefilter() function in PHP?

The above is the detailed content of How to apply a filter to an image using the imagefilter() function in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete