views:

1634

answers:

2

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.

+5  A: 

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);
Scharrels
Indeed, this is a nice workaround however I would still like to know how to manually create a blur like effect.
Alix Axel
You can look this up on wikipedia: http://en.wikipedia.org/wiki/Gaussian_blur#Implementation or look at similar posts on StackOverflow: http://stackoverflow.com/questions/98359/fastest-gaussian-blur-implementation but I would recommend using a built-in library. These libraries use compiled algorithms to speed up the process.
Scharrels
I took the suggestion you gave me and I've to loop ImageFilter 128 on the selected area to obfuscate it, maybe using a custom implementation I can get to the result more quickly.
Alix Axel
+1  A: 

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)
ChristopheD