我在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);
图像有透明背景,我想用白色替换任何透明像素。最干净的方法是什么?
答案 0 :(得分:0)
创建实体图像并在其上复制透明图像对我来说很有效:
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;
}
}
这是我的用法:
$solid_picture = Solid::white()->under($transparent_picture);
如果您的图片具有Alpha通道,请考虑使用imagecopymerge()
而不是imagecopy
。