imageaffine 


gd apg

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


  1 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 = [ 10010];

$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_base248248248);

imagefill($id_base10,10$fillcolor);

imagepng($id_base$base_img);

$drawcolor imagecolorallocate($id_base25500); 

$triangle = [ 505050150200150 ];
$points 3;

imageantialias($id_base1);

$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_img29);


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

SUPPORT IMAGE apr

PNG 086 01 triangle-A.png

BASE IMAGE apr

IDENTITY apr

PNG 086 01 triangle-B.png

RESULTING IMAGE apr


  2 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 = [ 10021515 ];

$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_base0255255); 

$triangle = [ 505050150200150 ];

$points 3;

imageantialias($id_base1);

$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_img29); ?>

<?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

baseXy.png apr

PNG 086 02 triangle-C.png

PNG 086 02 triangle-C.png apr

PNG 086 02 triangle-D.png

PNG 086 02 triangle-D.png apr

  3 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.2000.60];

$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_base00255); 

$triangle = [ 505050150200150 ];

$points 3;

imageantialias($id_base1);

$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_img29); ?>

<?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

baseXy.png apr

PNG 086 03 triangle-E.png

PNG 086 03 triangle-E.png apr

PNG 086 03 triangle-F.png

PNG 086 03 triangle-F.png apr

  4 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.200, -0.60];

$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_base2552550); 

$triangle = [ 505050150200150 ];

$points 3;

imageantialias($id_base1);

$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_img29); ?>

<?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

baseXy.png apr

PNG 086 04 triangle-G.png

PNG 086 04 triangle-G.png apr

PNG 086 04 triangle-H.png

PNG 086 04 triangle-H.png apr

  5 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 = [ 11011];

$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_base192255127); 

$triangle = [ 505050150200150 ];

$points 3;

imageantialias($id_base1);

$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_img29); ?>

<?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

baseXy.png apr

PNG 086 05 triangle-I.png

PNG 086 05 triangle-I.png apr

PNG 086 05 triangle-J.png

PNG 086 05 triangle-J.png apr

  6 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];

$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_base192255127); 

$triangle = [ 505050150200150 ];

$points 3;

imageantialias($id_base1);

$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_img29); ?>

<?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

baseXy.png apr

PNG 086 06 triangle-K.png

PNG 086 06 triangle-K.png apr

PNG 086 06 triangle-L.png

PNG 086 06 triangle-L.png apr

  7 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 = [ 
10010], 
1001150], 
1.2000.60], 
[ -
1.200, -0.60], 
12010], 
21010], 
cos(15), sin(15), -sin(15), cos(15), 0], 
cos(15), -sin(15), sin(15), cos(15), 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_base19200); 

$star =  [191,90209,142264,143220,177,  
                         
236,230191,199146,231
                         
161,177119,143172,143]; 

$points 10;

imageantialias($id_base1);

$drawstar 
imagefilledpolygon($id_base$star$points$drawcolor); 

imagepng($id_base$tgt_img1);

$select mt_rand(07);

$id_aff2 
imageaffine($id_base$arr_affine[$select], $arr_clip);

imagepng($id_aff2$tgt_img29); ?>

<?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?>">