使用GD的背景图像的透明度

时间:2022-07-02 19:35:56

Using GD2 to fill in a transparent png with a solid colour, here is my code and the result. Basically as soon as the transparency begins, the fill colour stops abruptly instead of blending in with the transparency.

使用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);
}

Original image:

alt text http://far.id.au/jkf/so/blank.png

alt text http://far.id.au/jkf/so/blank.png

Resulting image:

alt text http://far.id.au/jkf/so/filled.png

alt text http://far.id.au/jkf/so/filled.png

1 个解决方案

#1


I think you are going at it the wrong way, if you are trying to have the transparent image appear on top of the colour then you need to fill first then copy the image.

我认为你的方法是错误的,如果你想让透明图像出现在颜色的顶部,那么你需要首先填充然后复制图像。

Also if you are working with transparency you need to call imagecreatetruecolor(); instead of imagecreate();

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

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

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

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

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

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

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

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

        imagedestroy($original);
        imagedestroy($image);
}

If you are trying to draw the red on top of the image then use imagefilledrectangle() instead of imagefill(). For some reason imagefill doesn't seem to work well with transparencies.

如果您尝试在图像顶部绘制红色,则使用imagefilledrectangle()而不是imagefill()。由于某些原因,图像填充似乎不适用于透明胶片。

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

#1


I think you are going at it the wrong way, if you are trying to have the transparent image appear on top of the colour then you need to fill first then copy the image.

我认为你的方法是错误的,如果你想让透明图像出现在颜色的顶部,那么你需要首先填充然后复制图像。

Also if you are working with transparency you need to call imagecreatetruecolor(); instead of imagecreate();

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

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

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

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

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

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

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

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

        imagedestroy($original);
        imagedestroy($image);
}

If you are trying to draw the red on top of the image then use imagefilledrectangle() instead of imagefill(). For some reason imagefill doesn't seem to work well with transparencies.

如果您尝试在图像顶部绘制红色,则使用imagefilledrectangle()而不是imagefill()。由于某些原因,图像填充似乎不适用于透明胶片。

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