PHP/GD Gaussian Blur Effect

限于喜欢 提交于 2019-12-03 09:32:18

问题


I need to obfuscate a certain area of an image using PHP and GD, currently I'm using the following code:

for ($x = $_GET['x1']; $x < $_GET['x2']; $x += $pixel)
{
    for ($y = $_GET['y1']; $y < $_GET['y2']; $y += $pixel)
    {
        ImageFilledRectangle($image, $x, $y, $x + $pixel - 1, $y + $pixel - 1, ImageColorAt($image, $x, $y));
    }
}

This basically replaces the selected area with squares of $pixel pixels. I want to accomplish some kind of blur (gaussian preferably) effect, I know I can use the ImageFilter() function:

ImageFilter($image, IMG_FILTER_GAUSSIAN_BLUR);

But it blurs the entire canvas, my problem is that I just want to blur a specific area.


回答1:


You can copy a specific part of the image into a new image, apply the blur on the new image and copy the result back.

Sort of like this:

$image2 = imagecreate($width, $height);
imagecopy  ( $image2  , $image  , 0  , 0  , $x  , $y  , $width  , $height);
imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR);
imagecopy ($image, $image2, $x, $y, 0, 0, $width, $height);



回答2:


I did not check the documentation for imagefilter and I don't know if this is impossible or if there is an equivalent to applying this to (a part) of an image. But assuming there isn't, why not:

  1. Copy the part you want to blur to a new (temporary) GD image (no need to write it to disk, just assign it to a new temp variable).
  2. Apply gaussian blur filter to this temporary image.
  3. Copy the resulting (filtered) image right back where it came from (functionality to do this is definitely in the GD library)


来源:https://stackoverflow.com/questions/1248866/php-gd-gaussian-blur-effect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!