代码之家  ›  专栏  ›  技术社区  ›  Andy Moore

PHP+GD:imagecopymerge不保留PNG透明胶片

  •  5
  • Andy Moore  · 技术社区  · 15 年前

    我有两个PNG文件,“red.PNG”和“blue.PNG”;它们基本上都是透明的,但在不同的地方有几个像素的红色或蓝色斑点。

    $original = getPNG('red.png');
    $overlay = getPNG('blue.png');
    
    imagecopymerge($original, $overlay, 0,0, 0,0, imagesx($original), imagesy($original),   100);
    header('Content-Type: image/png');
    imagepng($original);
    

    imagealphablending($original, false);
    imagesavealpha($original, true);
    

    (在原版和套印版上?)这似乎没有任何帮助。

    我在PHP.net上看到了一些变通方法,包括:

    $throwAway = imagecreatefrompng($filename);
    imagealphablending($throwAway, false);
    imagesavealpha($throwAway, true);
    $dstImage = imagecreatetruecolor(imagesx($throwAway), imagesy($throwAway));
    imagecopyresampled($dstImage, $throwAway,0,0,0,0, imagesx($throwAway),     imagesy($throwAway),          imagesx($throwAway), imagesy($throwAway));
    

    我该怎么办?!

    1 回复  |  直到 15 年前
        1
  •  7
  •   Mike    15 年前

    这对我来说非常有效:

    $img1 = imagecreatefrompng('red.png');
    $img2 = imagecreatefrompng('blue.png');
    
    $x1 = imagesx($img1);
    $y1 = imagesy($img1);
    $x2 = imagesx($img2);
    $y2 = imagesy($img2);
    
    imagecopyresampled(
        $img1, $img2,
        0, 0, 0, 0,
        $x1, $y1,
        $x2, $y2);
    
    imagepng($img1, 'merged.png', 0);
    

    PHP版本5.3.2
    GD版本2.0

    你试过把图像保存到一个文件并检查它吗?

    推荐文章