PHP image卷积() 在左上角留下黑点

2024-02-18

我正在尝试使用以下代码锐化调整大小的图像:

imageconvolution($imageResource, array(
        array( -1, -1, -1 ),
        array( -1, 16, -1 ),
        array( -1, -1, -1 ),
    ), 8, 0);

当透明PNG图像被锐化时,使用上面的代码,它在左上角出现一个黑点(我尝试过不同的卷积核,但结果是相同的)。调整大小后图像看起来不错。

第一张图片是原始图片

第二张图片是锐化后的图片

EDIT: 我这是怎么了?我正在使用从像素检索的颜色。

$color = imagecolorat($imageResource, 0, 0);
    imageconvolution($imageResource, array(
        array( -1, -1, -1 ),
        array( -1, 16, -1 ),
        array( -1, -1, -1 ),
    ), 8, 0);
            imagesetpixel($imageResource, 0, 0, $color);

Is imagecolorat正确的功能?或者立场是否正确?

EDIT2: 我已经改变了坐标,但仍然没有运气。我检查了给出的透明度imagecolorat(根据这个post https://stackoverflow.com/questions/5702953/imagecolorat-and-transparency)。这是转储:

array(4) {
   red => 0
   green => 0
   blue => 0
   alpha => 127
}

Alpha 127 = 100% 透明。这些零可能会导致问题......


看起来像是卷积代码中的错误(角点在某些实现中是特殊情况)。

作为解决方法,您可以在卷积之前保存该角的像素值并在之后恢复它,使用imageSetPixel().

您需要保存的像素位于 (0,0),并且可能您还需要检查透明度(但我认为它应该只适用于imageColorAt and imageSetPixel).

测试代码

文件“giants.png”是我从您上面发布的文件中获取的。如果我不使用imageSetPixel我体验到了与你相同的额外像素。和imageSetPixel,图像对我来说看起来是正确的。

我运行的顺序可能略有不同ImageSaveAlpha或设置 alpha 混合。

<?php
        $giants = ImageCreateFromPNG('giants.png');

        $imageResource = ImageCreateTrueColor(190, 190);

        ImageColorTransparent($imageResource, ImageColorAllocateAlpha($imageResource, 0, 0, 0, 127));
        ImageAlphaBlending($imageResource, False);
        ImageSaveAlpha($imageResource, True);

        ImageCopyResampled($imageResource, $giants, 0, 0, 0, 0, 190, 190, ImageSX($giants), ImageSY($giants));

        $color = ImageColorAt($imageResource, 0, 0);
        ImageConvolution($imageResource, array(
                        array( -1, -1, -1 ),
                        array( -1, 16, -1 ),
                        array( -1, -1, -1 ),
                ), 8, 0);
        ImageSetPixel($imageResource, 0, 0, $color);
        ImagePNG($imageResource, 'dwarves.png');
?>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PHP image卷积() 在左上角留下黑点 的相关文章

随机推荐