【问题标题】:Gaussian blur with image convolution图像卷积的高斯模糊
【发布时间】:2025-12-15 00:05:02
【问题描述】:

一段时间以来,我一直在尝试使用 PHP 中的以下代码来模糊一些文本:

$image = imagecreate(150,25);
$background = ImageColorAllocate($image, 255, 255, 255);
$foreground = ImageColorAllocate($image, 0, 0, 0);

ImageColorTransparent($image, $background);
ImageInterlace($image, false);

ImageTTFText($image, 20, 0, 0, 20, $foreground, "font.ttf", "some text");

$gaussian = array(array(1.0, 2.0, 1.0), array(2.0, 4.0, 2.0), array(1.0, 2.0, 1.0));
imageconvolution($image, $gaussian, 16, 0);

imagePNG($image)

但是图像没有模糊,只是分辨率降低,变得颗粒状...Demo here..

【问题讨论】:

  • 某事告诉我imageconvolution 不会自动分配生成的颜色(灰色,介于黑色和白色之间)。您获得黑白结果的原因是因为它选择了与模糊像素值最相似的颜色。尝试使用imagecreatetruecolor 创建图像。 (我评论而不是回答,因为我不确定而且我无法从这里进行测试。)

标签: php blur gaussian image-conversion


【解决方案1】:

去掉ImageColorTransparent($image, $background);看看效果

例子

$image = imagecreate(150,25);
$background = ImageColorAllocate($image, 255, 255, 255);
$foreground = ImageColorAllocate($image, 0, 0, 0);

ImageInterlace($image, false);
ImageTTFText($image, 20, 0, 0, 20, $foreground, "verdana.ttf", "some text");

$gaussian = array(array(1.0,2.0,1.0),array(2.0,4.0,2.0),array(1.0,2.0,1.0));
imageconvolution($image, $gaussian, 16, 0);

header('Content-Type: image/png');
imagepng($image, null, 9);

输出

 ^ Before ^           ^ After ^

【讨论】:

  • 是否可以控制模糊量?
最近更新 更多