Php 用GD实现背景图像的透明

Php 用GD实现背景图像的透明,php,transparency,gd,Php,Transparency,Gd,使用GD2以纯色填充透明png,下面是我的代码和结果。基本上,一旦透明度开始,填充颜色就会突然停止,而不是与透明度混合 private function GenerateImage() { $original = imagecreatefrompng($this->ImagePath()); $x = imagesx($original); $y = imagesy($original); $image = imagecreate($x,$y);

使用GD2以纯色填充透明png,下面是我的代码和结果。基本上,一旦透明度开始,填充颜色就会突然停止,而不是与透明度混合

private function GenerateImage()
{
    $original = imagecreatefrompng($this->ImagePath());

    $x = imagesx($original);
    $y = imagesy($original);

    $image = imagecreate($x,$y);

    imagealphablending($image,false);
    imagesavealpha($image,true);

    imagecopyresampled($image,$original,0,0,0,0,$x,$y,$x,$y);

    $colour = imagecolorallocate($image,$this->RGB[0],$this->RGB[1],$this->RGB[2]);
    imagefill($image,0,0,$colour);

    return imagepng($image,$this->GeneratedPath());

    imagedestroy($original);
    imagedestroy($image);
}
原始图像:

结果图像:


我认为你做得不对,如果你想让透明图像出现在颜色的顶部,那么你需要先填充,然后复制图像

另外,如果您正在使用透明度,则需要调用ImageCreateTureColor();而不是imagecreate()

如果试图在图像顶部绘制红色,请使用imagefilledrectangle()而不是imagefill()。出于某种原因,imagefill似乎不能很好地与透明胶片配合使用

// Replace
imagefill($image,0,0,$colour);
// With
imagefilledrectangle( $image, 0,0, $x,$y,$colour);

我认为你是走错了路,如果你想让透明图像出现在颜色的上面,那么你需要先填充,然后复制图像

另外,如果您正在使用透明度,则需要调用ImageCreateTureColor();而不是imagecreate()

如果试图在图像顶部绘制红色,请使用imagefilledrectangle()而不是imagefill()。出于某种原因,imagefill似乎不能很好地与透明胶片配合使用

// Replace
imagefill($image,0,0,$colour);
// With
imagefilledrectangle( $image, 0,0, $x,$y,$colour);

非常感谢。我喜欢在PHP中使用GD,但仍然有很多东西需要学习。谢谢!我喜欢将GD与PHP结合使用,但仍然有很多东西需要学习。