我在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);图像有一个透明的背景,我想用白色替换任何透明的像素。做这件事最干净的方法是什么?
发布于 2018-09-12 16:43:07
创建一个实体图像并在其上复制透明图像对我来说很有效:
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);如果您的图片具有imagecopy通道,请考虑使用imagecopymerge()而不是alpha。
https://stackoverflow.com/questions/45254690
复制相似问题