[英]Replace transparent bg with white PHP GD
I have an image in php generated like so: 我在php中生成了一个图像,如下所示:
$base = imagecreatefromstring(file_get_contents($_GET['imgSRC']));
imagepalettetotruecolor($base);
$background = imagecolorallocate($base, 0, 0, 0);
imagecolortransparent($base, $background);
imagealphablending($base, false);
imagesavealpha($base, true);
The image has a transparent background, I would like to replace any transparent pixels with white. 图像具有透明背景,我想用白色替换所有透明像素。 What is the cleanest way to do this? 什么是最干净的方法?
Creating a solid image and copying the transparent image over it worked for me: 创建一个实体图像并在其上复制透明图像对我有用:
class Solid {
static function white() {
return new self(255, 255, 255);
}
function __construct($red, $green, $blue) {
$this->red = $red;
$this->green = $green;
$this->blue = $blue;
}
function under($pic) {
$w = imagesx($pic);
$h = imagesy($pic);
$buffer = imagecreatetruecolor($w, $h);
imagefilledrectangle($buffer, 0, 0, $w, $h,
imagecolorallocate($buffer, $this->red, $this->green, $this->blue));
imagecopy($buffer, $pic, 0, 0, 0, 0, $w, $h);
return $buffer;
}
}
This is how I use it: 这是我的用法:
$solid_picture = Solid::white()->under($transparent_picture);
In case your picture has an alpha channel, consider using imagecopymerge()
instead of imagecopy
. 如果您的图片具有Alpha通道,请考虑使用imagecopymerge()
而不是imagecopy
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.