问题
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:
- 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).
- Apply gaussian blur filter to this temporary image.
- 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