What role does imagealphablending play in maintaining transparency when rotating images in PHP?

When rotating images in PHP, the imagealphablending function is crucial for maintaining transparency. This function allows for the blending of alpha channel information when drawing one image onto another, ensuring that transparent pixels remain transparent after rotation. By enabling imagealphablending before rotating the image, you can preserve the transparency of the image throughout the process.

// Load the original image
$source = imagecreatefrompng('original.png');

// Enable alpha blending
imagealphablending($source, true);

// Rotate the image
$rotated = imagerotate($source, 45, 0);

// Save or output the rotated image
imagepng($rotated, 'rotated.png');

// Free up memory
imagedestroy($source);
imagedestroy($rotated);