imageaffine RETURN an image containing the affine transformed source image, using an optional clipping area.
AFFINE
is a geometric transformation operation involving MATRICES, covering both, 2D and 3D environment.
Transformations are often used in linear algebra and computer graphics.
In geometric transformations of images, the pixel coordinate is mapped.
This means that, each pixel is localized by two coordinates, in the rectangular domain of the image.
Without going into more details about pixel mapping, let's get to what really matters: the AFFINE transformations.
There are several classes of pixel mapping, one is called AFFINE.
AFFINE transformations include: scaling, rotation, shearing and translation.
This function returns TRUE on success or FALSE on failure.
<?php
GdImage|false imageaffine ( GdImage $image,
array $affine,
?array $clip = null )
where,
$image = An image identifier
$affine = An ARRAY with keys 0 to 5
$clip = An ARRAY to clip image
with keys "x", "y", "width" and "height" or null
?>
$image
An image identifier.
$affine
An AFFINE ARRAY with keys 0 to 5.
KEYS |
MEANING |
0 |
a0 |
1 |
a1 |
2 |
b0 |
3 |
b1 |
4 |
a2 |
5 |
b2 |
ed48 |
$clip
An ARRAY to clip image with keys "x", "y", "width" and "height" or null.
ÍNDICES |
SIGNIFICADO |
x |
x-coordinate - start of clipping section |
y |
y-coordinate - start of clipping section |
width |
clipping section width |
height |
clipping section height |
null |
since PHP 8.0.0 |
ed48 |
EXERCISE
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$base_img = 'png/baseXy.png';
$tgt_img1 = 'png/PNG 086 01 triangle-A.png';
$tgt_img2 = 'png/PNG 086 01 triangle-B.png';
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
IDENTITY => the simplest of transformations
Described by:
$arr_affine = [ 1, 0, 0, 1, 0, 0 ];
ALL POINTS ARE KEPT UNCHANGED
x' = 1x + 0y + 0 = x
y' = 0x + 1y + 0 = y
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
$arr_affine = [ 1, 0, 0, 1, 0, 0 ];
$id_base = imagecreatefrompng($base_img);
$w = imagesx($id_base);
$h = imagesy($id_base);
$arr_clip = [ 'x' => 0, 'y' => 0, 'width' => $w, 'height' => $h ];
$fillcolor = imagecolorallocate($id_base, 248, 248, 248);
imagefill($id_base, 10,10, $fillcolor);
imagepng($id_base, $base_img);
$drawcolor = imagecolorallocate($id_base, 255, 0, 0);
$triangle = [ 50, 50, 50, 150, 200, 150 ];
$points = 3;
imageantialias($id_base, 1);
$drawtriangle =
imagefilledpolygon($id_base, $triangle, $points, $drawcolor);
imagepng($id_base, $tgt_img1);
$id_aff2 = imageaffine($id_base, $arr_affine, $arr_clip);
imagepng($id_aff2, $tgt_img2, 9);
echo $base_img; ?><br><br>
<img src="<?php echo $base_img; ?>"
alt="SUPPORT IMAGE"><br><br>
<?php echo $tgt_img1; ?><br><br>
<img src="<?php echo $tgt_img1; ?>"
alt="BASE IMAGE"><br><br>
<?php echo $tgt_img2; ?><br><br>
<img src="<?php echo $tgt_img2; ?>"
alt="RESULTING IMAGE">
RESULT baseXy.png
PNG 086 01 triangle-A.png
PNG 086 01 triangle-B.png
EXERCISE
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$base_img = 'png/baseXy.png';
$tgt_img1 = 'png/PNG 086 02 triangle-C.png';
$tgt_img2 = 'png/PNG 086 02 triangle-D.png';
$arr_affine = [ 1, 0, 0, 2, 15, 15 ];
$id_base = imagecreatefrompng($base_img);
$w = imagesx($id_base);
$h = imagesy($id_base);
$arr_clip = [ 'x' => 0, 'y' => 0, 'width' => $w, 'height' => $h ];
$drawcolor = imagecolorallocate($id_base, 0, 255, 255);
$triangle = [ 50, 50, 50, 150, 200, 150 ];
$points = 3;
imageantialias($id_base, 1);
$drawtriangle =
imagefilledpolygon($id_base, $triangle, $points, $drawcolor);
imagepng($id_base, $tgt_img1);
$id_aff2 = imageaffine($id_base, $arr_affine, $arr_clip);
imagepng($id_aff2, $tgt_img2, 9); ?>
<?php echo $base_img; ?><br><br>
<img src="<?php echo $base_img; ?>"
alt="<?php echo $base_img; ?>"><br><br>
<?php echo $tgt_img1; ?><br><br>
<img src="<?php echo $tgt_img1; ?>"
alt="<?php echo $tgt_img1; ?>"><br><br>
<?php echo $tgt_img2; ?><br><br>
<img src="<?php echo $tgt_img2; ?>"
alt="<?php echo $tgt_img2; ?>">
RESULT baseXy.png
PNG 086 02 triangle-C.png
PNG 086 02 triangle-D.png
EXERCISE
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$base_img = 'png/baseXy.png';
$tgt_img1 = 'png/PNG 086 03 triangle-E.png';
$tgt_img2 = 'png/PNG 086 03 triangle-F.png';
$arr_affine = [ 1.2, 0, 0, 0.6, 0, 0 ];
$id_base = imagecreatefrompng($base_img);
$w = imagesx($id_base);
$h = imagesy($id_base);
$arr_clip = [ 'x' => 0, 'y' => 0, 'width' => $w, 'height' => $h ];
$drawcolor = imagecolorallocate($id_base, 0, 0, 255);
$triangle = [ 50, 50, 50, 150, 200, 150 ];
$points = 3;
imageantialias($id_base, 1);
$drawtriangle =
imagefilledpolygon($id_base, $triangle, $points, $drawcolor);
imagepng($id_base, $tgt_img1);
$id_aff2 = imageaffine($id_base, $arr_affine, $arr_clip);
imagepng($id_aff2, $tgt_img2, 9); ?>
<?php echo $base_img; ?><br><br>
<img src="<?php echo $base_img; ?>"
alt="<?php echo $base_img; ?>"><br><br>
<?php echo $tgt_img1; ?><br><br>
<img src="<?php echo $tgt_img1; ?>"
alt="<?php echo $tgt_img1; ?>"><br><br>
<?php echo $tgt_img2; ?><br><br>
<img src="<?php echo $tgt_img2; ?>"
alt="<?php echo $tgt_img2; ?>">
RESULT baseXy.png
PNG 086 03 triangle-E.png
PNG 086 03 triangle-F.png
EXERCISE
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$base_img = 'png/baseXy.png';
$tgt_img1 = 'png/PNG 086 04 triangle-G.png';
$tgt_img2 = 'png/PNG 086 04 triangle-H.png';
$arr_affine = [ -1.2, 0, 0, -0.6, 0, 0 ];
$id_base = imagecreatefrompng($base_img);
$w = imagesx($id_base);
$h = imagesy($id_base);
$arr_clip = [ 'x' => 0, 'y' => 0, 'width' => $w, 'height' => $h ];
$drawcolor = imagecolorallocate($id_base, 255, 255, 0);
$triangle = [ 50, 50, 50, 150, 200, 150 ];
$points = 3;
imageantialias($id_base, 1);
$drawtriangle =
imagefilledpolygon($id_base, $triangle, $points, $drawcolor);
imagepng($id_base, $tgt_img1);
$id_aff2 = imageaffine($id_base, $arr_affine, $arr_clip);
imagepng($id_aff2, $tgt_img2, 9); ?>
<?php echo $base_img; ?><br><br>
<img src="<?php echo $base_img; ?>"
alt="<?php echo $base_img; ?>"><br><br>
<?php echo $tgt_img1; ?><br><br>
<img src="<?php echo $tgt_img1; ?>"
alt="<?php echo $tgt_img1; ?>"><br><br>
<?php echo $tgt_img2; ?><br><br>
<img src="<?php echo $tgt_img2; ?>"
alt="<?php echo $tgt_img2; ?>">
RESULT baseXy.png
PNG 086 04 triangle-G.png
PNG 086 04 triangle-H.png
EXERCISE
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$base_img = 'png/baseXy.png';
$tgt_img1 = 'png/PNG 086 05 triangle-I.png';
$tgt_img2 = 'png/PNG 086 05 triangle-J.png';
$arr_affine = [ 1, 1, 0, 1, 1, 1 ];
$id_base = imagecreatefrompng($base_img);
$w = imagesx($id_base);
$h = imagesy($id_base);
$arr_clip = [ 'x' => 0, 'y' => 0, 'width' => $w, 'height' => $h ];
$drawcolor = imagecolorallocate($id_base, 192, 255, 127);
$triangle = [ 50, 50, 50, 150, 200, 150 ];
$points = 3;
imageantialias($id_base, 1);
$drawtriangle =
imagefilledpolygon($id_base, $triangle, $points, $drawcolor);
imagepng($id_base, $tgt_img1);
$id_aff2 = imageaffine($id_base, $arr_affine, $arr_clip);
imagepng($id_aff2, $tgt_img2, 9); ?>
<?php echo $base_img; ?><br><br>
<img src="<?php echo $base_img; ?>"
alt="<?php echo $base_img; ?>"><br><br>
<?php echo $tgt_img1; ?><br><br>
<img src="<?php echo $tgt_img1; ?>"
alt="<?php echo $tgt_img1; ?>"><br><br>
<?php echo $tgt_img2; ?><br><br>
<img src="<?php echo $tgt_img2; ?>"
alt="<?php echo $tgt_img2; ?>">
RESULT baseXy.png
PNG 086 05 triangle-I.png
PNG 086 05 triangle-J.png
EXERCISE
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$base_img = 'png/baseXy.png';
$tgt_img1 = 'png/PNG 086 06 triangle-K.png';
$tgt_img2 = 'png/PNG 086 06 triangle-L.png';
$arr_affine = [ cos(45), sin(45), -sin(45), cos(45), 0, 0 ];
$id_base = imagecreatefrompng($base_img);
$w = imagesx($id_base);
$h = imagesy($id_base);
$arr_clip = [ 'x' => 0, 'y' => 0, 'width' => $w, 'height' => $h ];
$drawcolor = imagecolorallocate($id_base, 192, 255, 127);
$triangle = [ 50, 50, 50, 150, 200, 150 ];
$points = 3;
imageantialias($id_base, 1);
$drawtriangle =
imagefilledpolygon($id_base, $triangle, $points, $drawcolor);
imagepng($id_base, $tgt_img1);
$id_aff2 = imageaffine($id_base, $arr_affine, $arr_clip);
imagepng($id_aff2, $tgt_img2, 9); ?>
<?php echo $base_img; ?><br><br>
<img src="<?php echo $base_img; ?>"
alt="<?php echo $base_img; ?>"><br><br>
<?php echo $tgt_img1; ?><br><br>
<img src="<?php echo $tgt_img1; ?>"
alt="<?php echo $tgt_img1; ?>"><br><br>
<?php echo $tgt_img2; ?><br><br>
<img src="<?php echo $tgt_img2; ?>"
alt="<?php echo $tgt_img2; ?>">
RESULT baseXy.png
PNG 086 06 triangle-K.png
PNG 086 06 triangle-L.png
EXERCISE
<?php
// You must run this exercise to see the result
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$base_img = 'png/baseXy.png';
$tgt_img1 = 'png/PNG 086 07 star-A.png';
$tgt_img2 = 'png/PNG 086 07 star-B.png';
$arr_affine = [
[ 1, 0, 0, 1, 0, 0 ],
[ 1, 0, 0, 1, 150, 0 ],
[ 1.2, 0, 0, 0.6, 0, 0 ],
[ -1.2, 0, 0, -0.6, 0, 0 ],
[ 1, 2, 0, 1, 0, 0 ],
[ 2, 1, 0, 1, 0, 0 ],
[ cos(15), sin(15), -sin(15), cos(15), 0, 0 ],
[ cos(15), -sin(15), sin(15), cos(15), 0, 0 ]
];
$id_base = imagecreatefrompng($base_img);
$w = imagesx($id_base);
$h = imagesy($id_base);
$arr_clip = [ 'x' => 0, 'y' => 0, 'width' => $w, 'height' => $h ];
$drawcolor = imagecolorallocate($id_base, 192, 0, 0);
$star = [191,90, 209,142, 264,143, 220,177,
236,230, 191,199, 146,231,
161,177, 119,143, 172,143];
$points = 10;
imageantialias($id_base, 1);
$drawstar =
imagefilledpolygon($id_base, $star, $points, $drawcolor);
imagepng($id_base, $tgt_img1);
$select = mt_rand(0, 7);
$id_aff2 =
imageaffine($id_base, $arr_affine[$select], $arr_clip);
imagepng($id_aff2, $tgt_img2, 9); ?>
<?php echo basename($base_img); ?><br><br>
<img src="<?php echo $base_img; ?>"
alt="<?php echo $base_img; ?>"><br><br>
<?php echo basename($tgt_img1); ?><br><br>
<img src="<?php echo $tgt_img1; ?>"
alt="<?php echo $tgt_img1; ?>"><br><br>
<?php echo basename($tgt_img2); ?><br><br>
<img src="<?php echo $tgt_img2; ?>"
alt="<?php echo $tgt_img2; ?>">