Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP/GD Gaussian Blur Effect

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.

like image 747
Alix Axel Avatar asked Aug 08 '09 13:08

Alix Axel


1 Answers

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);
like image 200
Scharrels Avatar answered Oct 09 '22 07:10

Scharrels