我合并了两个图像。第一张图片将始终为白色,扩展名为PNG,大小为1200px X 628px。第二张图片的大小为 1000 像素 X 495 像素。但是当我合并该图像时,白色图像将转换为黑色图像。并显示第二张图像的背景为黑色。请帮助我如何解决此问题并将黑色图像更改为白色图像。
将以下行添加到您的代码中,您的问题应该得到解决,
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
$transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);
仅供参考,用于调整图像大小的完整源代码,
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
if ($this->image_type == IMAGETYPE_PNG){
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
$transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);
}
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}