Current Location: Home> Latest Articles> How to Preserve Transparency When Rotating Transparent PNG Images Using imagerotate

How to Preserve Transparency When Rotating Transparent PNG Images Using imagerotate

gitbox 2025-09-18

1. Rotating PNG Images Using imagerotate()

First, load a PNG image and try to rotate it using imagerotate(). Normally, without additional handling, the transparent areas will turn black:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// Load the PNG image</span></span><span>
</span><span><span class="hljs-variable">$imagePath</span></span><span> = </span><span><span class="hljs-string">'image.png'</span></span><span>;
</span><span><span class="hljs-variable">$image</span></span><span> = </span><span><span class="hljs-title function_ invoke__">imagecreatefrompng</span></span><span>(</span><span><span class="hljs-variable">$imagePath</span></span><span>);
<p></span>// Set PNG image to have a transparent background<br>
imagealphablending($image, false);<br>
imagesavealpha($image, true);</p>
<p>// Rotate the image by 90 degrees<br>
$rotatedImage = imagerotate($image, 90, 0);</p>
<p>// Output the rotated image<br>
header('Content-Type: image/png');<br>
imagepng($rotatedImage);</p>
<p>// Destroy image resources<br>
imagedestroy($image);<br>
imagedestroy($rotatedImage);<br>
?><br>
</span>

The code above loads and rotates a PNG image, but note that the third parameter of imagerotate() (the fill color) is 0, which fills the transparent areas with black.

2. Preventing a Black Background

To preserve transparency, the key is to instruct PHP to maintain the PNG image’s transparency. Before calling imagerotate(), make sure to disable blending mode and enable saving the alpha channel:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// Load the PNG image</span></span><span>
</span><span><span class="hljs-variable">$imagePath</span></span><span> = </span><span><span class="hljs-string">'image.png'</span></span><span>;
</span><span><span class="hljs-variable">$image</span></span><span> = </span><span><span class="hljs-title function_ invoke__">imagecreatefrompng</span></span><span>(</span><span><span class="hljs-variable">$imagePath</span></span><span>);
<p></span>// Set PNG image to have a transparent background<br>
imagealphablending($image, </span>false);  // Disable alpha blending<br>
imagesavealpha($image, </span>true);  // Enable alpha channel saving</p>
<p>// Rotate the image by 90 degrees<br>
$rotatedImage = imagerotate($image, 90, 0);</p>
<p>// Output the rotated image<br>
header('Content-Type: image/png');<br>
imagepng($rotatedImage);</p>
<p>// Destroy image resources<br>
imagedestroy($image);<br>
imagedestroy($rotatedImage);<br>
?><br>
</span>

3. Why Use imagealphablending() and imagesavealpha()?

  • imagealphablending($image, false); disables alpha channel blending, ensuring that transparent areas are not merged with any other colors during processing.

  • imagesavealpha($image, true); enables saving of the alpha channel (transparency information), so transparent areas remain intact and the rotated image preserves its transparent background.

4. Filling Transparent Backgrounds

If you want to fill the transparent areas with a color instead of keeping them transparent when rotating, you can set a different fill color in imagerotate(). For example, to fill with white:

<span><span><span class="hljs-variable">$rotatedImage</span></span><span> = </span><span><span class="hljs-title function_ invoke__">imagerotate</span></span><span>(</span><span><span class="hljs-variable">$image</span></span>, </span><span><span class="hljs-number">90</span></span><span>, </span><span><span class="hljs-title function_ invoke__">imagecolorallocatealpha</span></span><span>(</span><span><span class="hljs-variable">$image</span></span><span>, </span><span><span class="hljs-number">255</span></span><span>, </span><span><span class="hljs-number">255</span></span><span>, </span><span><span class="hljs-number">255</span></span><span>, </span><span><span class="hljs-number">127</span></span><span>));
</span></span>

This fills the transparent areas with white instead of black.

5. Outputting and Saving the Image

When outputting the rotated image, remember to use imagepng(), as it handles the transparency in PNG images. You can also save the image to a file:

<span><span><span class="hljs-title function_ invoke__">imagepng</span></span><span>(</span><span><span class="hljs-variable">$rotatedImage</span></span><span>, </span><span><span class="hljs-string">'rotated_image.png'</span></span><span>);
</span></span>