php gd png透明,通过php和gd使用透明背景的平滑圆形png

博主在尝试使用PHP的GD库将一张照片处理成圆形并保存为带有透明背景的PNG图片,但遇到了问题:生成的图片只有左上角是透明的,其余部分仍有黑色背景。尝试了不同的方法,包括创建遮罩和填充椭圆,但效果不理想,导致图片质量下降。寻求帮助解决GD库创建透明圆形图片时出现的透明度问题。
摘要由CSDN通过智能技术生成

我正在尝试用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...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值