Rotating Image to any degree with transparent background


The following code will rotate an image to any given degree and if you use png image then the background color can be transparent. So, you can use that rotated image upon any image or color.

$filename = 'image.png';
$degrees = 20;

header('Content-type: image/png');

$source = imagecreatefrompng($filename);

$rotate = imagerotate($source, $degrees, -1); // -1 will cause the bg color as white

imagealphablending($rotate, true);
imagesavealpha($rotate, true);