Current Location: Home> Latest Articles> How to Use the imageaffinematrixget Function in PHP? Basic Usage and Detailed Guide

How to Use the imageaffinematrixget Function in PHP? Basic Usage and Detailed Guide

gitbox 2025-09-19

2. Basic Usage Examples

2.1 Translating an Image

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-variable">$image</span></span><span> = </span><span><span class="hljs-title function_ invoke__">imagecreatefromjpeg</span></span><span>(&#039;example.jpg&#039;);
<p></span>// Get the translation matrix<br>
$matrix = imageaffinematrixget(IMG_AFFINE_TRANSLATE, ['x' => 50, 'y' => 30]);</p>
<p></span>// Apply the matrix to the image<br>
$translated = imageaffine($image, $matrix);</p>
<p></span>// Output the result<br>
header('Content-Type: image/jpeg');<br>
</span>imagejpeg($translated);<br>
</span>imagedestroy($image);<br>
</span>imagedestroy($translated);<br>
</span>?><br>
</span>

The code above moves the image 50 pixels to the right and 30 pixels down.


2.2 Scaling an Image

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-variable">$image</span></span><span> = </span><span><span class="hljs-title function_ invoke__">imagecreatefromjpeg</span></span>('example.jpg');
<p></span>// Get the scaling matrix<br>
$matrix = imageaffinematrixget(IMG_AFFINE_SCALE, ['x' => 1.5, 'y' => 0.8]);</p>
<p>// Apply the matrix to the image<br>
$scaled = imageaffine($image, $matrix);</p>
<p></span>header('Content-Type: image/jpeg');<br>
</span>imagejpeg($scaled);<br>
</span>imagedestroy($image);<br>
</span>imagedestroy($scaled);<br>
</span>?><br>
</span>

This code scales the image 1.5 times horizontally and reduces it to 0.8 times vertically.


2.3 Rotating an Image

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-variable">$image</span></span><span> = </span><span><span class="hljs-title function_ invoke__">imagecreatefromjpeg</span></span>('example.jpg');
<p></span>// Get the rotation matrix<br>
$matrix = imageaffinematrixget(IMG_AFFINE_ROTATE, ['angle' => 45]);</p>
<p>$rotated = imageaffine($image, $matrix);</p>
<p>header('Content-Type: image/jpeg');<br>
</span>imagejpeg($rotated);<br>
</span>imagedestroy($image);<br>
</span>imagedestroy($rotated);<br>
</span>?><br>
</span>

This rotates the image 45 degrees clockwise.


3. Advanced Usage

3.1 Combining Matrices

Sometimes, we need to scale and rotate an image at the same time. You can use imageaffinematrixconcat to combine matrices:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-variable">$scale</span></span><span> = </span><span><span class="hljs-title function_ invoke__">imageaffinematrixget</span>(IMG_AFFINE_SCALE, ['x' => 2.0, 'y' => 2.0]);
</span><span><span class="hljs-variable">$rotate</span></span><span> = </span><span><span class="hljs-title function_ invoke__">imageaffinematrixget</span>(IMG_AFFINE_ROTATE, ['angle' => 30]);
<p></span>$combined = imageaffinematrixconcat($scale, $rotate);</p>
<p>$transformed = imageaffine($image, $combined);<br>
?><br>
</span>

3.2 Important Notes

  1. Applying matrices may cause image edges to be clipped. You can adjust the output size using the $clip parameter of imageaffine.

  2. The matrix returned by imageaffinematrixget must use floating-point numbers; otherwise, unexpected results may occur.

  3. The order of matrices matters: scaling before rotating produces different results than rotating before scaling.