【问题标题】:Smooth round PNG with transparent background via PHP and GD通过 PHP 和 GD 实现具有透明背景的平滑圆形 PNG
【发布时间】:2018-11-06 00:07:19
【问题描述】:

我正在尝试使用带有 GD 的 PHP 创建圆形版本的经典照片(正方形),在具有透明背景的 PNG 中......我已经通过遵循互联网上的一些教程成功地做到了(如http://thedebuggers.com/transparent-circular-crop-using-php-gd/),但是圆不光滑所以质量不适合我使用...

我尝试了以下方式,但我遇到了一个问题:只有左上角是透明的(在 PI/2 期间,圆圈是完全完美的),但黑色背景的 3/4 是用GD 还在(底角和右上角)。

你能帮我解决这个问题吗?

提前致谢,

    //$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...

【问题讨论】:

    标签: php png transparency gd php-gd


    【解决方案1】:

    背景似乎需要从左上角到其他角是连续的,以允许到处透明,所以我在构建椭圆时添加了一些空间。

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

    谢谢,

    【讨论】:

      猜你喜欢
      • 2015-04-20
      • 1970-01-01
      • 2013-02-21
      • 1970-01-01
      • 2011-01-09
      • 1970-01-01
      • 2013-07-26
      • 2017-06-30
      • 2012-12-19
      相关资源
      最近更新 更多