我正在尝试用PHP和gd创建一个经典照片的圆形版本(正方形),在一个透明背景的PNG中…我通过在互联网上学习一些教程(如
http://thedebuggers.com/transparent-circular-crop-using-php-gd/
,但是圆不光滑,所以质量不适合我使用……
我尝试了以下方法,但我面临一个问题:只有左上角是透明的(在pi/2中,圆是完全完美的),但是用gd构建的黑色背景的3/4仍然在这里(下角和右上角)。
你能帮我解决这个问题吗?
事先谢谢,
//$image_s is a resource (photo loaded to be processed)
$width = imagesx($image_s);
$height = imagesy($image_s);
$newwidth = 500;
$newheight = 500;
$image = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($image, true);
imagecopyresampled($image, $image_s, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//create masking
$mask = imagecreatetruecolor($newwidth, $newheight);
$transparent = imagecolorallocate($mask, 255, 0, 0);
imagecolortransparent($mask,$transparent);
imagefilledellipse($mask, $newwidth/2, $newheight/2, $newwidth, $newheight, $transparent);
$red = imagecolorallocate($mask, 0, 0, 0);
imagecopymerge($image, $mask, 0, 0, 0, 0, $newwidth, $newheight, 100);
imagecolortransparent($image,$red);
imagefill($image, 0, 0, $red);
$finalImage = imagecreatetruecolor(100, 100);
$transparentColor = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $transparentColor);
imagealphablending($finalImage, false);
imagesavealpha( $finalImage, true );
imagecopyresampled($finalImage, $image, 0, 0, 0, 0, 100, 100, $newwidth, $newheight);
imagepng($finalImage, $markerPicturePath);
imagedestroy($image);
imagedestroy($mask);
imagedestroy($finalImage);
//$markerPicturePath is now a PNG picture with a round extracted from the photo, with a top left transparent corner and 3 other black corners...