How to get an affine transformation matrix in PHP using imageaffinematrixget()?

PHPServer Side ProgrammingProgramming

imageaffinematrixget() is an inbuilt function in PHP that is used to get an affine transformation matrix. This function is often used in linear algebra and computer graphics.

Syntax

array imageaffinematrixget(int $type, mixed $options)

Parameters

imageaffinematrixget() accepts only two parameters: $type and $options.

  • $type − The $type parameter specifies the integer to IMG_AFFINE constants.

    • IMG_AFFINE_TRANSLATE

    • IMG_AFFINE_SCALE

    • IMG_AFFINE_ROTATE

    • IMG_AFFINE_SHEAR_HORIZONTAL

    • IMG_AFFINE_SHEAR_VERTICAL

  • $options − If type is IMG_AFFINE_TRANSLATE or IMG_AFFINE_SCALE, options has to be an array with keys x and y, both having float values. If type is IMG_AFFINE_ROTATE, IMG_AFFINE_SHEAR_HORIZONTAL or IMG_AFFINE_SHEAR_VERTICAL, options has to be a float specifying the angle.

Return Values

It returns an affine transformation matrix, an array with keys from 0 to 5 and float values. On failure, it returns false.

Example 1: Using IMG_AFFINE_SCALE

 Live Demo

<?php
   $matrix_scale = imageaffinematrixget(IMG_AFFINE_SCALE, array('x' => 2, 'y' => 3));
   print_r($matrix_scale);
?>

Output

Array
(
   [0] => 2
   [1] => 0
   [2] => 0
   [3] => 3
   [4] => 0
   [5] => 0
)

Example 2

 Live Demo

<?php
   $angle = 280;
   
   // Get the image affine matrix using imageaffinematrixget() function
   $matrix_vertical = imageaffinematrixget(IMG_AFFINE_SHEAR_VERTICAL, $angle);
   
   // Output the matrix values
   print("<pre>".print_r($matrix_vertical, true)."</pre>");
?>

Output

Array
(
   [0] => 1
   [1] => -5.6712818196177
   [2] => 0
   [3] => 1
   [4] => 0
   [5] => 0
)
raja
Published on 09-Aug-2021 13:58:36
Advertisements