繁体   English   中英

通过PHP和GD平滑透明背景的圆形PNG

[英]Smooth round PNG with transparent background via PHP and GD

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

看起来背景需要从左上角到其他角连续,以允许各处透明,因此在构建椭圆时我添加了一些空间。

我将imagefilledellipse行编辑为imagefilledellipse($mask, $tempWidth/2, $tempHeight/2, $tempWidth-2, $tempHeight-2, $transparent);

谢谢,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM