请注意,在newImage中创建句柄并在其上调用imagealphablending和imagesavealpha并不重要,因为loadImage会抛出该句柄.
它用蓝色“填充”透明区域的原因是它没有任何东西填充透明区域.它只是完全丢弃了alpha通道,而蓝色则恰好存储在那些alpha为零的像素中.请注意,这可能很难在图形程序中看到,因为该程序本身可以用黑色或白色替换完全透明的像素.
至于你的代码有什么问题,我不能肯定地说,因为当我尝试你现有的代码时,我没有得到与你报告的相同的结果.但是,如果我将loadImage更改为类似的东西,那么源图像将被强制为真彩色,它对我有用:
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;
}
(就个人而言,我更喜欢ImageMagick而不是GD).