<span><span><span class="hljs-meta"><?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>('example.jpg');
<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.
<span><span><span class="hljs-meta"><?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.
<span><span><span class="hljs-meta"><?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.
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"><?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>
Applying matrices may cause image edges to be clipped. You can adjust the output size using the $clip parameter of imageaffine.
The matrix returned by imageaffinematrixget must use floating-point numbers; otherwise, unexpected results may occur.
The order of matrices matters: scaling before rotating produces different results than rotating before scaling.