imageconvolution APPLIES a 3x3 convolution matrix on the image, using the given coefficient and offset.
CONVOLUTION
is a mathematical operation on two functions, ( f and g ), to produce a third function that expresses how the shape of one is modified by the other.
The term CONVOLUTION refers to both the result function and to the process of computing it.
It is defined as the integral of the product of the two functions after one is reversed and shifted.
This function returns TRUE on success or FALSE on failure.
<?php
bool imageconvolution ( GdImage $image,
array $matrix,
float $divisor,
float $offset )
where,
$image = An image identifier
$matrix = A 3x3 matrix: an ARRAY of
three arrays of three floats
$divisor = The divisor of the result
of the convolution, used for normalization
$offset = The color offset
?>
$image
An image identifier.
$matrix
A 3x3 array of three arrays of three floats.
$divisor
The divisor of the result of the convolution, used for normalization.
$offset
The color offset.
EXERCISE
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$lang = 'en';
$org_img01 = 'png/PNG 084 01.png';
$sharp_img01 = 'png/PNG 084 01 (SHARPEN).png';
$emboss_img01 = 'png/PNG 084 01 (EMBOSS).png';
?>
<?php echo $org_img01; ?><br><br>
<img src="<?php echo $org_img01; ?>"
alt="<?php echo $org_img01; ?>"
title="<?php echo $org_img01; ?>" width="400"><br><br>
<?php
$id_img01 = imagecreatefrompng($org_img01);
/* - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
3X3 matrix
Values for SHARPEN filter
- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - */
$mt_sharp = [ [-1, -1, -1 ], [ -1, 9, -1], [ -1, -1, -1 ] ];
/* - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
3X3 matrix
Values for EMBOSS filter
- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - */
$mt_emboss = [ [-2, -1, 0 ], [ -1, 1, 1], [ 0, 1, 2 ] ];
/* - -- -- -- -- -- -- -- -- -- -- -- -- --
Divider
Try other values
- -- -- -- -- -- -- -- -- -- -- -- -- -- */
$divider = 0.6;
/* - -- -- -- -- -- -- -- -- -- -- -- -- -- --
Offset
Try other values
- -- -- -- -- -- -- -- -- -- -- -- -- -- -- */
$offset = 5;
$quality = 9;
// Remember $quality between 0 and 9 for .png
imageconvolution($id_img01, $mt_sharp, $divider, $offset);
imagepng($id_img01, $sharp_img01, $quality);
echo $sharp_img01 . '<br><pre>';
print_r($mt_sharp);
echo '</pre><br>divider = ' . $divider . '<br>offset = ' .
$offset . '<br>quality = ' . $quality; ?><br><br>
<img src="<?php echo $sharp_img01; ?>"
alt="<?php echo $sharp_img01; ?>"
title="<?php echo $sharp_img01; ?>" width="400"><br><br>
<?php
imageconvolution($id_img01, $mt_emboss, $divider, $offset);
imagepng($id_img01, $emboss_img01, $quality);
echo $emboss_img01 . '<br><pre>';
print_r($mt_emboss);
echo '</pre><br>divider = ' . $divider . '<br>offset = ' .
$offset . '<br>quality = ' . $quality; ?><br><br>
<img src="<?php echo $emboss_img01; ?>"
alt="<?php echo $emboss_img01; ?>"
title="<?php echo $emboss_img01; ?>" width="400"><br><br>
RESULT PNG 084 01.png
PNG 084 01 (SHARPEN).pngArray
(
[0] => Array
(
[0] => -1
[1] => -1
[2] => -1
)
[1] => Array
(
[0] => -1
[1] => 9
[2] => -1
)
[2] => Array
(
[0] => -1
[1] => -1
[2] => -1
)
)
divider = 0.6
offset = 5
quality = 9
PNG 084 01 (EMBOSS).pngArray
(
[0] => Array
(
[0] => -2
[1] => -1
[2] => 0
)
[1] => Array
(
[0] => -1
[1] => 1
[2] => 1
)
[2] => Array
(
[0] => 0
[1] => 1
[2] => 2
)
)
divider = 0.6
offset = 5
quality = 9
EXERCISE
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$lang = 'en';
$org = 'png/PNG 085 02.png';
?>
<?php echo $org; ?><br><br>
<img src="<?php echo $org; ?>"
alt="<?php echo $org; ?>"
title="<?php echo $org; ?>"><br><br>
<?php
$id_org = imagecreatefrompng($org);
$divider = 0.02;
$offset = 0.001;
$quality = 9;
// Remember $quality between 0 and 9 for .png
$names = [ "SHARP", "EMBOSS", "HORIZONTAL EDGES",
"VERTICAL EDGES", "LEFT DIAGONAL EDGES",
"RIGHT DIAGONAL EDGES", "EMBOSSING-WEST",
"EMBOSSING-EAST", "EMBOSSING-SOUTH",
"EMBOSSING-NORTHEAST", "EMBOSSING-SOUTHWEST",
"ARTHIMETIC_MEAN", "SMOOTHING-BASIC", "HIGH-PASS",
"LAPLACIAN", "SOBEL-HORIZONTAL", "SOBEL-VERTICAL" ];
$mt = [ "SHARP" => [ [-1, -1, -1 ], [ -1, 9, -1], [ -1, -1, -1 ] ],
"EMBOSS" => [ [-2, -1, 0 ], [ -1, 1, 1], [ 0, 1, 2 ] ],
"HORIZONTAL EDGES" => [ [-1, 2, -1], [-1, 2, -1], [-1, 2, -1] ],
"VERTICAL EDGES" => [ [-1, 2, -1], [-1, 2, -1], [-1, 2, -1] ],
"LEFT DIAGONAL EDGES" => [ [2, -1, -1], [-1, 2, -1 ], [-1, -1, 2] ],
"RIGHT DIAGONAL EDGES" => [ [-1, -1, 2], [-1, 2, -1], [2, -1, -1] ],
"EMBOSSING-WEST" => [ [-1, 0, 1], [-2, 0, 2], [-1, 0, 1] ],
"EMBOSSING-EAST" => [ [1, 0, -1], [2, 0, -2], [1, 0, -1] ],
"EMBOSSING-SOUTH" => [ [1, 2, 1], [0, 0, 0], [-1, -2, -1] ],
"EMBOSSING-NORTHEAST" => [ [0, -1, -2], [1, 0, -1], [2, 1, 0] ],
"EMBOSSING-SOUTHWEST" => [ [0, 1, 2], [-1, 0, 1], [-2, -1, 0] ],
"ARTHIMETIC_MEAN" => [ [0.11, 0.11, 0.11],
[0.11, 0.11, 0.11],
[0.11, 0.11, 0.11] ],
"SMOOTHING-BASIC" => [ [1, 2, 1], [2, 4, 2], [1, 2, 1] ],
"HIGH-PASS" => [ [-1, -1, -1], [-1, 9, -1], [-1, -1, -1] ],
"LAPLACIAN" => [ [0, -1, 0], [-1, 4, -1], [0, -1, 0] ],
"SOBEL-HORIZONTAL" => [ [-1, -2, -1], [0, 0, 0], [1, 2, 1] ],
"SOBEL-VERTICAL" => [ [-1, 0, -1], [-2, 0, -2], [-1, 0, 1] ] ];
for($a = 0; $a <= 16; $a++ )
{
$arr[$a] = $mt[$names[$a]];
imageconvolution($id_org, $arr[$a], $divider, $offset);
imagepng($id_org, P2PNGN . 'PNG 085 02 ' . $names[$a] .
'.png', $quality);
echo '<br><br>PNG 085 02 ' . $names[$a] . '.png' . '<br><br>';
print_r($arr[$a]);
echo '<br><br>divider = ' . $divider . '<br>offset = ' .
$offset . '<br>quality = ' . $quality; ?><br><br>
<img src="<?php echo P2PNGN . 'PNG 085 02 ' .
$names[$a] . '.png'; ?>"
alt="<?php echo 'PNG 085 02 ' . $names[$a] . '.png'; ?>"
title="<?php echo 'PNG 085 02 ' . $names[$a] . '.png'; ?>">
<?php
}
?>
RESULT PNG 085 02.png
Based on any image, such as the one provided, run this exercise on your machine to see the result.