Php Imageaffinematrixget
## PHP imageaffinematrixget() - Get Affine Transformation Matrix
The `imageaffinematrixget()` function is a built-in PHP function used to generate an affine transformation matrix. This matrix is commonly used with `imageaffine()` to apply complex geometric transformationsβsuch as translation, scaling, rotation, and shearingβto an image.
---
## Introduction to Affine Transformations
An affine transformation is a geometric transformation that preserves lines and parallelism (but not necessarily distances and angles). In 2D computer graphics, these transformations are represented mathematically by a $3 \times 3$ matrix.
In PHP's GD library, this matrix is simplified and returned as a 1D array of 6 float values (representing the elements `[a0, a1, b0, b1, a2, b2]` of the transformation matrix):
$$
\begin{bmatrix}
x' \\
y' \\
1
\end{bmatrix} =
\begin{bmatrix}
a_0 & a_1 & a_2 \\
b_0 & b_1 & b_2 \\
0 & 0 & 1
\end{bmatrix}
\begin{bmatrix}
x \\
y \\
1
\end{bmatrix}
$$
---
## Syntax
```php
imageaffinematrixget(int $type, array|float $options): array|false
```
### Parameters
* **`type`**: One of the `IMG_AFFINE_*` constants specifying the type of transformation.
* **`options`**: The parameters required for the chosen transformation type. This can be an integer, float, or an array depending on the `type`.
### Supported Transformation Types (`type`)
| Constant | Description | Expected `options` Format |
| :--- | :--- | :--- |
| **`IMG_AFFINE_TRANSLATE`** | Translates (moves) the image. | `array('x' => float, 'y' => float)` |
| **`IMG_AFFINE_SCALE`** | Scales (resizes) the image. | `array('x' => float, 'y' => float)` |
| **`IMG_AFFINE_ROTATE`** | Rotates the image. | `float` (Angle in degrees) |
| **`IMG_AFFINE_SHEAR_HORIZONTAL`** | Shears the image horizontally. | `float` (Shear factor) |
| **`IMG_AFFINE_SHEAR_VERTICAL`** | Shears the image vertically. | `float` (Shear factor) |
### Return Value
Returns an **array** of 6 float values representing the affine transformation matrix on success, or **`false`** on failure.
---
## Code Examples
### Example 1: Creating a Rotation Matrix
This example demonstrates how to generate a matrix for a $45^\circ$ rotation and apply it to an image.
```php
```
### Example 2: Combining Translation and Scaling
You can multiply matrices together using `imageaffinematrixconcat()` (if you need to chain transformations), but first, you must generate the individual matrices using `imageaffinematrixget()`.
```php
50.0, 'y' => 100.0];
$translate_matrix = imageaffinematrixget(IMG_AFFINE_TRANSLATE, $translate_options);
// 2. Create a scaling matrix (Scale down to 50% size)
$scale_options = ['x' => 0.5, 'y' => 0.5];
$scale_matrix = imageaffinematrixget(IMG_AFFINE_SCALE, $scale_options);
// Print the generated translation matrix array
print_r($translate_matrix);
/*
Output will look like:
Array
(
=> 1
=> 0
=> 0
=> 1
=> 50
=> 100
)
*/
?>
```
---
## Considerations & Best Practices
1. **GD Library Requirement**: This function requires PHP compiled with the GD extension (GD 2.1.0 or higher is recommended).
2. **Memory Management**: Transforming images (especially rotating and scaling) can be memory-intensive. Ensure your `memory_limit` in `php.ini` is sufficient when processing high-resolution images.
3. **Floating Point Precision**: The `options` parameters (like angles and scale factors) should ideally be passed as `float` values to prevent unexpected rounding errors.
4. **Error Handling**: Always check if `imageaffinematrixget()` returns `false` before passing the matrix to `imageaffine()`.
YouTip