PHP & GD - transparent background being filled with nearby color

三世轮回 提交于 2019-11-30 20:24:21

Note that it doesn't matter that you create the handle in newImage and call imagealphablending and imagesavealpha on it, because loadImage throws that handle away.

The reason it is "filling" the transparent area with the blue color is that it is not filling the transparent area with anything at all. It it just completely dropping the alpha channel, and the blue color is what happens to be stored in those pixels with an alpha of zero. Note this may be difficult to see in a graphics program, as that program may itself replace completely-transparent pixels with black or white.

As for what is wrong with your code, I can't say for sure as I don't get the same results as you report when I try your existing code. But if I change your loadImage to something like this so the source images are forced to true color, it works for me:

            private function loadImage()
            {
                    $img = null;
                    switch( $this->type )
                    {
                            case 1:
                                            $img = imagecreatefromgif($this->source);
                                            break;
                            case 2:
                                            $img = imagecreatefromjpeg($this->source);
                                            break;
                            case 3:
                                            $img = imagecreatefrompng($this->source);
                                            break;
                            default:
                                            break;
                    }
                    if (!$img) return false;
                    $this->handle = imagecreatetruecolor($this->width, $this->height);
                    imagealphablending($this->handle, false);
                    imagesavealpha($this->handle, true);
                    imagecopyresampled($this->handle, $img, 0, 0, 0, 0, $this->width, $this->height, $this->width, $this->height);
                    return true;
            }

(Personally, I prefer ImageMagick over GD).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!