imagefilter 


gd apg

APPLIES a filter to an image.





This function returns TRUE on success or FALSE on failure.



<?php

bool imagefilter
(GdImage $imageint $filter
                          array|
int|float|bool ...$args )

where,

$image The image identifier

$filter 
The filter type
                  
SEE the below TABLE )
                  
                  . . . . . . . . . .
              
$args Additional filter type
             
SEE the below TABLE )
             
?> 

  $image   


The image identifier.



  $filtertype   


The filter type may be designated by one of the following constants:

IMAGE FILTER CONSTANTS
CONSTANT VALUE MEANING
IMG_FILTER_NEGATE 0 Reverses all colors of the image.
IMG_FILTER_GRAYSCALE 1 Converts the image into grayscale by changing the red, green and blue components to their weighted sum using the same coefficients
as the [*] REC.601 luma (Y') calculation.
The alpha components are retained.
For palette images the result may differ due to palette limitations.
IMG_FILTER_BRIGHTNESS 2 Changes the brightness of the image.
Use arg1 to set the level of brightness.
The range for the brightness is -255 to 255.
IMG_FILTER_CONTRAST 3 Changes the contrast of the image.
Use arg1 to set the level of contrast.
IMG_FILTER_COLORIZE 4 Like IMG_FILTER_GRAYSCALE, except you can specify the color.
Use arg1, arg2 and arg3 in the form of red, green, blue and arg4 for the alpha channel.
The range for each color is 0 to 255.
IMG_FILTER_EDGEDETECT 5 Uses edge detection to highlight the edges in the image.
IMG_FILTER_EMBOSS 6 Embosses the image.
IMG_FILTER_GAUSSIAN_BLUR 7 Blurs the image using the Gaussian method.
IMG_FILTER_SELECTIVE_BLUR 8 Blurs the image.
IMG_FILTER_MEAN_REMOVAL 9 Uses mean removal to achieve a "sketchy" effect.
IMG_FILTER_SMOOTH 10 Makes the image smoother.
Use arg1 to set the level of smoothness.
IMG_FILTER_PIXELATE 11 Applies pixelation effect to the image, use arg1 to set the block size and arg2 to set the pixelation effect mode.
IMG_FILTER_SCATTER 12 Applies scatter effect to the image, use arg1 and arg2 to define the effect strength and additionally arg3 to only apply the on select pixel colors.
Available as of PHP 7.4.0
ed48

[*] luma represents the brightness in an image, (the "black-and-white" or achromatic portion of the image) and typically paired with chrominance.


  $arg1   

IMAGE FILTER CONSTANTS
CONSTANT VALUE MEANING
IMG_FILTER_BRIGHTNESS [-255, 255]D Brightness level.
IMG_FILTER_CONTRAST [-100, 100]D Contrast level.
IMG_FILTER_COLORIZE [0, 255]D [00, FF]H Value of red component.
IMG_FILTER_SMOOTH [0, 100]D Smoothness level.
IMG_FILTER_PIXELATE # pixels Block size in pixels.
IMG_FILTER_SCATTER $arg1 < $arg2 Effect substraction level.
Available as of PHP 7.4.0
ed48


  $arg2   

IMAGE FILTER CONSTANTS
CONSTANT VALUE MEANING
IMG_FILTER_COLORIZE [0, 255]D [00, FF]H Value of green component.
IMG_FILTER_PIXELATE FALSE (DEFAULT) Whether to use advanced pixelation effect or not.
TRUE
IMG_FILTER_SCATTER $arg1 ≥ $arg2 Effect addition level.
Available as of PHP 7.4.0
ed48


  $arg3   

IMAGE FILTER CONSTANTS
CONSTANT VALUE MEANING
IMG_FILTER_COLORIZE [0, 255]D [00, FF]H Value of blue component.
IMG_FILTER_SCATTER $arg3 Optional array indexed color values
to apply effect at.
Available as of PHP 7.4.0
ed48


  $arg4   

IMAGE FILTER CONSTANTS
CONSTANT VALUE MEANING
IMG_FILTER_COLORIZE [0, 127]D Alpha channel.
[ 0 completely opaque,
127 ] completely transparent.
ed48


  1 EXERCISE   

<?php

echo 'PHP VERSION: ' PHP_VERSION '<br><br>';

$xpl_img 'png/PNG 074 01.png';

$arr_fvn3 = [ 
"IMG_FILTER_BRIGHTNESS" => IMG_FILTER_BRIGHTNESS
"IMG_FILTER_CONTRAST" => IMG_FILTER_CONTRAST
"IMG_FILTER_SMOOTH" => IMG_FILTER_SMOOTH
"IMG_FILTER_PIXELATE" => IMG_FILTER_PIXELATE ];

$arr_fvn5 = [ 
"IMG_FILTER_NEGATE" => IMG_FILTER_NEGATE
"IMG_FILTER_GRAYSCALE" => IMG_FILTER_GRAYSCALE
"IMG_FILTER_COLORIZE" => IMG_FILTER_COLORIZE
"IMG_FILTER_EDGEDETECT" => IMG_FILTER_EDGEDETECT
"IMG_FILTER_EMBOSS" => IMG_FILTER_EMBOSS
"IMG_FILTER_GAUSSIAN_BLUR" => IMG_FILTER_GAUSSIAN_BLUR,
"IMG_FILTER_SELECTIVE_BLUR" => IMG_FILTER_SELECTIVE_BLUR
"IMG_FILTER_MEAN_REMOVAL" => IMG_FILTER_MEAN_REMOVAL ];

$arr_fvn = [ "IMG_FILTER_SCATTER" => IMG_FILTER_SCATTER ];

$lang 'en';

$en '<br><br>EXISTING ORIGINAL IMAGE';

$id_01 imagecreatefrompng($xpl_img);

echo 
basename($xpl_img); ?>&nbsp;&nbsp;
<?php echo $$lang?><br><br>
<img src="<?php echo $xpl_img?>"
alt="<?php echo $xpl_img?>
title="<?php echo $xpl_img?>"><br><br>

<?php

$arg1 
5;
$arg2 1;
$arg3 8;

// These are the arg values used in this exercise
// You must try another values

echo '<br><br>Used 5 parameters.';

foreach(
$arr_fvn5 as $fvn5 => $fv5)
{
$flt_img P2PNGN 'PNG 074 01 ' $fvn5 '.png';
imagefilter($id_01$fv5$arg1$arg2$arg3);
imagepng($id_01$flt_img);
echo 
'<br><br>( ' $fv5 ' ) ' basename($flt_img) . 
'<br><br>' $fvn5?><br><br>
<img src="<?php echo $flt_img?>"
alt="<?php echo $flt_img?>
title="<?php echo $flt_img?>">
<?php
}
echo 
'<br><br>Used 3 parameters.';

foreach(
$arr_fvn3 as $fvn3 => $fv3)
{

$flt_img P2PNGN 'PNG 074 01 ' $fvn3 '.png';
imagefilter($id_01$fv3$arg1);
imagepng($id_01$flt_img);
echo 
'<br><br>( ' $fv3 ' ) ' basename($flt_img) . 
'<br><br>' $fvn3?><br><br>
<img src="<?php echo $flt_img?>"
alt="<?php echo $flt_img?>
title="<?php echo $flt_img?>">

<?php
}
echo 
'<br><br>Used 4 parameters.';

foreach(
$arr_fvn as $fvn => $fv)
{

$flt_img P2PNGN 'PNG 074 01 ' $fvn '.png';
imagefilter($id_01$fv$arg1$arg2);
imagepng($id_01$flt_img);
echo 
'<br><br>( ' $fv ' ) ' basename($flt_img) . 
'<br><br>' $fvn?><br><br>
<img src="<?php echo $flt_img?>"
alt="<?php echo $flt_img?>
title="<?php echo $flt_img?>">

<?php
}
?>


 RESULT   

PNG 074 01.png

EXISTING ORIGINAL IMAGE

PNG 074 01.png apr



PNG 074 01 IMG_FILTER_NEGATE.png

IMG_FILTER_NEGATE

PNG 074 01 IMG_FILTER_NEGATE.png apr

PNG 074 01 IMG_FILTER_GRAYSCALE.png

IMG_FILTER_GRAYSCALE

PNG 074 01 IMG_FILTER_GRAYSCALE.png apr

PNG 074 01 IMG_FILTER_COLORIZE.png

IMG_FILTER_COLORIZE

PNG 074 01 IMG_FILTER_COLORIZE.png apr

PNG 074 01 IMG_FILTER_EDGEDETECT.png

IMG_FILTER_EDGEDETECT

PNG 074 01 IMG_FILTER_EDGEDETECT.png apr

PNG 074 01 IMG_FILTER_EMBOSS.png

IMG_FILTER_EMBOSS

PNG 074 01 IMG_FILTER_EMBOSS.png apr

PNG 074 01 IMG_FILTER_GAUSSIAN_BLUR.png

IMG_FILTER_GAUSSIAN_BLUR

PNG 074 01 IMG_FILTER_GAUSSIAN_BLUR.png apr

PNG 074 01 IMG_FILTER_SELECTIVE_BLUR.png

IMG_FILTER_SELECTIVE_BLUR

PNG 074 01 IMG_FILTER_SELECTIVE_BLUR.png apr

PNG 074 01 IMG_FILTER_MEAN_REMOVAL.png

IMG_FILTER_MEAN_REMOVAL

PNG 074 01 IMG_FILTER_MEAN_REMOVAL.png apr

PNG 074 01 IMG_FILTER_BRIGHTNESS.png

IMG_FILTER_BRIGHTNESS

PNG 074 01 IMG_FILTER_BRIGHTNESS.png apr

PNG 074 01 IMG_FILTER_CONTRAST.png

IMG_FILTER_CONTRAST

PNG 074 01 IMG_FILTER_CONTRAST.png apr

PNG 074 01 IMG_FILTER_SMOOTH.png

IMG_FILTER_SMOOTH

PNG 074 01 IMG_FILTER_SMOOTH.png apr

PNG 074 01 IMG_FILTER_PIXELATE.png

IMG_FILTER_PIXELATE

PNG 074 01 IMG_FILTER_PIXELATE.png apr

PNG 074 01 IMG_FILTER_SCATTER.png

IMG_FILTER_SCATTER

PNG 074 01 IMG_FILTER_SCATTER.png apr

  2 EXERCISE   

<?php

// RUN this code several times

echo 'PHP VERSION: ' PHP_VERSION '<br><br>';

$lang 'en';

$xpl_img 'png/PNG 074 01.png';

$en '<br>EXISTING ORIGINAL IMAGE';

$id_01 imagecreatefrompng($xpl_img);

echo 
basename($xpl_img) . '<br><br>' . $$lang?><br><br>
<img src="<?php echo $xpl_img?>"
alt="<?php echo $xpl_img?>
title="<?php echo $xpl_img?>"><br><br>

<?php

$arg1 
mt_rand(0255);
$arg2 mt_rand(0255);
$arg3 mt_rand(0255);;

$fvn5 'IMG_FILTER_COLORIZE'
$fv5 IMG_FILTER_COLORIZE;

$flt_img P2PNGN 'PNG 074 02 ' $fvn5 '.png';
imagefilter($id_01$fv5$arg1$arg2$arg3);
imagepng($id_01$flt_img);

echo 
'<br><br>( ' $fv5 ' ) ' basename($flt_img) . 
'<br><br>' $fvn5?><br>
<?php echo '( arg1 = ' $arg1 ' ) 
( arg2 = ' 
$arg2 ' ) 
( arg3 = ' 
$arg3 ' )<br><br>'?>
<img src="<?php echo $flt_img?>"
alt="<?php echo $flt_img?>
title="<?php echo $flt_img?>">


 RESULT   

PNG 074 01.png

EXISTING ORIGINAL IMAGE

PNG 074 01.png apr



PNG 074 02 IMG_FILTER_COLORIZE.png

IMG_FILTER_COLORIZE
( arg1 = 100 ) ( arg2 = 14 ) ( arg3 = 273 )


PNG 074 02 IMG_FILTER_COLORIZE.png apr

This is a particular result.

For each new run a new result will be displayed.

Try yourself.