PHP & GD - transparent background being filled with nearby color

前端 未结 1 717
感情败类
感情败类 2021-01-05 15:34

I know PHP+GD Transparency issues have been beat to death on this and many other sites, but I\'ve followed all the recommendations and I can\'t seem to fix my issue.

相关标签:
1条回答
  • 2021-01-05 16:22

    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).

    0 讨论(0)
提交回复
热议问题