PHP& GD:图像分割然后黑色变为透明

时间:2017-01-10 17:11:33

标签: php image transparency gd

通常,透明度在我通过互联网找到的问题上变黑了。但对我来说恰恰相反。

我试图将几张图片分成几张。它工作得很好,但主要问题是结果可能包含黑色,变成透明......请帮忙! :)

// I get the image to split and dimensions
$source = @imagecreatefrompng('images/'."full_to_split.png");
$source_width = imagesx( $source );
$source_height = imagesy( $source );

// Setting alpha for transparency
imageAlphaBlending($source, true);
imageSaveAlpha($source, true);

// I will split the image vertically by height of 100px
$width = $source_width;
$height = 100;

for( $row = 0; $row < $source_height / $height; $row++)
{
    // I get the template image (a "part") which is a 100px height and full width of the source image
    $fn_d = sprintf( 'images/'."part_%02d.png", $row+1 );

    $destination = imagecreatefrompng($fn_d);

    imagecopyresampled( $destination, $source, 0, 0,
      0, $row * $height, $width, $height,
      $width, $height );

    imagepng( $destination, $fn_d );
    imagedestroy( $destination );
}

像这样:我的部件有透明背景(所以这是完美的)但是如果有一个(真正的)黑色&#34;斑点&#34;在完整的图像中,这一个变为透明。

如果我在$ destination上使用alpha混合,我就不再具有透明度(它会变黑)。

我尝试使用imagecopyresized和imagecopyresampled ......这让我很紧张:p

谢谢

Original colors 原色

Result after split 拆分后的结果

full_to_split.png full_to_split.png

part_01.png part_01.png

part_02 part_02.png

part_03.png part_03.png

0 个答案:

没有答案