$resizedImage = imagecreatetruecolor($imgW, $imgH);
imagealphablending($resizedImage, false);
imagesavealpha($resizedImage, true);
imagecopyresampled($resizedImage, $source_image, 0, 0, 0, 0, $imgW, $imgH, $imgInitW, $imgInitH);
$rotated_image = imagerotate($resizedImage, -$angle, 0);
$rotated_width = imagesx($rotated_image);
$rotated_height = imagesy($rotated_image);
$dx = $rotated_width - $imgW;
$dy = $rotated_height - $imgH;
$cropped_rotated_image = imagecreatetruecolor($imgW, $imgH);
imagealphablending($cropped_rotated_image, false);
imagesavealpha($cropped_rotated_image, true);
imagecopyresampled($cropped_rotated_image, $rotated_image, 0, 0, $dx / 2, $dy / 2, $imgW, $imgH, $imgW, $imgH);
$final_image = imagecreatetruecolor($cropW, $cropH);
imagealphablending($final_image, false);
imagesavealpha($final_image, true);
imagecopyresampled($final_image, $cropped_rotated_image, 0, 0, $imgX1, $imgY1, $cropW, $cropH, $cropW, $cropH);