【问题标题】:Color-keying image not working in PHP颜色键控图像在 PHP 中不起作用
【发布时间】:2013-04-18 18:24:24
【问题描述】:

输入图像具有洋红色背景。

输出的图像总是有黑色背景。

两次尝试都给出相同的结果。我希望它是 alpha 背景,而不是黑色背景。

尝试 #1:

imagesavealpha($image, true);
imagealphablending($image, false);
$trans = imagecolorallocate($image, 255, 0, 255);
imagecolortransparent($image, $trans);
imagepng($image, $label . '.png');

尝试 #2:

imagesavealpha($image, true);
imagealphablending($image, false);
$trans = imagecolorallocatealpha($image, 0, 0, 0, 0);
for ($x = 0; $x < imagesx($image); ++$x) {
    for ($y = 0; $y < imagesy($image); ++$y) {
        $rgb = imagecolorat($image, $x, $y);
        $r = ($rgb >> 16) & 0xFF;
        $g = ($rgb >> 8) & 0xFF;
        $b = $rgb & 0xFF;
        if ($r == 255 && $g == 0 && $b == 255) {
            imagesetpixel($image, $x, $y, $trans);
        }
    }
}
imagepng($image, $label . '.png');

【问题讨论】:

    标签: php png gd alpha alphablending


    【解决方案1】:

    似乎imagecolorallocatealpha 的 alpha 参数是非标准的。使用 0 到 127,其中 127 是完全透明的。

    【讨论】:

      【解决方案2】:

      你的尝试 #1 在我看来不错,只是不要碰 alphablending。

      为了避免透明度问题,我习惯于在创建图像后设置它:

      $image = imagecreatetruecolor($width, $height);
      $trans = imagecolorallocate($image, 0xFF, 0x0, 0xFF);
      imagefill($image, 0, 0, $trans);
      imagecolortransparent($image, $trans);
      

      在这里,我确定我的整个图像背景是透明的,我只需要把我的东西放进去。

      【讨论】:

        猜你喜欢
        • 2018-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-12
        • 2018-05-24
        • 1970-01-01
        • 2013-01-12
        • 1970-01-01
        相关资源
        最近更新 更多