问题描述
我正在尝试使用以下代码来锐化调整大小的图像:
I'm trying to sharpen resized images using this code:
imageconvolution($imageResource, array(
array( -1, -1, -1 ),
array( -1, 16, -1 ),
array( -1, -1, -1 ),
), 8, 0);
使用上面的代码对透明PNG图像进行锐化时,它会在左上角显示一个黑点(我尝试了不同的卷积核,但是结果是相同的).调整图像大小后,看起来还可以.
When the transparent PNG image is sharpened, using code above, it appears with a black dot in the upper left corner (I have tried different convolution kernels, but the result is the same). After resizing the image looked OK.
第一张图片是原始图片
第二张图像是锐化的
编辑:我怎么了?我正在使用从像素中提取的颜色.
EDIT: What am I going wrong? I'm using the color retrieved from pixel.
$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);
imagecolorat
是正确的功能吗?还是位置正确?
Is imagecolorat
the right function? Or is the position correct?
EDIT2 :我已经更改了坐标,但是仍然没有运气.我已经检查了imagecolorat
给出的透明度(根据此帖子).这是转储:
EDIT2: I have changed coordinates, but still no luck. I've check the transparency given by imagecolorat
(according to this post). This is the dump:
array(4) {
red => 0
green => 0
blue => 0
alpha => 127
}
Alpha 127 = 100%透明.这些零可能会导致问题...
Alpha 127 = 100% transparent. Those zeroes might cause the problem...
推荐答案
看起来像卷积代码中的错误(在某些实现中,角落是特殊情况).
Looks like a bug in the convolution code (corners are special cases in some implementations).
作为一种解决方法,您可以使用imageSetPixel()
将像素值保存在卷积之前的那个角上,然后再进行恢复.
As a workaround, you can save the pixel value in that corner just before the convolution and restore it afterwards, with imageSetPixel()
.
您需要保存的像素为(0,0),可能还需要检查透明度(但我认为它应该仅与imageColorAt
和imageSetPixel
一起使用).
The pixel you need to save is at (0,0), and possibly you will need to also check the transparency (but I think it should work with just imageColorAt
and imageSetPixel
).
测试代码
我从上面发布的文件中忘了文件"giants.png".如果我不使用imageSetPixel
,我会遇到与您相同的额外像素.使用imageSetPixel
,图像对我来说看起来是正确的.
The file 'giants.png' I wgot from the one you posted above. If I do not use imageSetPixel
I experience the same extra pixel you got. With imageSetPixel
, the image looks correct to me.
我运行ImageSaveAlpha
或设置Alpha混合的顺序可能略有不同.
Possibly there's some slight difference in the sequence I run ImageSaveAlpha
or set alpha blending.
<?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');
?>
这篇关于PHP imageconvolution()在左上角留下黑点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!