Skip to content
PHP Logo

Are there best practices for rotating images in PHP to ensure transparency is preserved?

When rotating images in PHP, it is important to preserve transparency if the image contains transparent areas. To ensure transparency is maintained, you can use the `imagealphablending()` and `imagesavealpha()` functions before rotating the image.

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

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

// Save alpha channel
imagesavealpha($source, true);

// Rotate the image (example: 90 degrees)
$rotated = imagerotate($source, 90, 0);

// Output the rotated image
header('Content-Type: image/png');
imagepng($rotated);

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