Live photo blurring script - faces, number, plates, etc

In our recent project we came across a need for a script that will blur out part of an image. This feature is mostly needed to make people faces blurred out - but also to remove any personal data, like licence plate numbers, etc. We wanted our users to have a smudge tool that would works exactly like in Photoshop, but in their browser. Unfortunately, as it turns out, it is way too much to ask for from Java Script, and even if we would succeed in adding such a feature, it would most likely work really slowly.

At this moment we were left with two options use Adobe Flash or Java Script with PHP.
And as we try to avoid Flash as much as possible, we went for the second one.

Our solution is pretty simple we are using ImageMagick that is being executed by PHP. The blurring itself takes place on the server, but full control of it is through browser. On the client side we have Java Script that allows user to select region that he wants to blur out, and then when user confirms by clicking on a link an AJAX message is sent to the PHP script, containing coordinates of user's selection.

On the browser side we used jQuery with a very nice plugin called imgAreaSelect that allows user to select rectangle-shaped area of image for bluring out. It took us a while to find the blur command for ImageMagick that would blur out only part of the image.

Here is the code for blurring:

PHP image blurring code
convert inputImage \( +clone -blur 10x8 \) \ 
          \( +clone -gamma 0 -fill white \ 
          -draw 'rectangle x1,y1 x2,y2' -blur  10x4 \) \ 
          -composite outputImage

Another way to make some part of an image less recognisable is to pixelate it, you can use following command for that:

PHP image pixelating code
convert inputImage \( +clone -scale 20% -scale 500% \) \ 
          \( +clone -gamma 0 -fill white \ 
          -draw 'rectangle x1,y1 x2,y2' -blur  10x4 \) \ 
          -composite outputImage

Of course you should change inputImage, outputImage filenames and coordinates (x1,x2,y1,y2) to match your requirements. In our example coordinates are passed through GET by AJAX query. The full PHP part of the script should look like following:

PHP part of code
$x1 = $_GET['x1']; 
$y1 = $_GET['y1']; 
$x2 = $_GET['x2']; 
$y2 = $_GET['y2']; 
$inputImage = $_GET['inputImage']; 
$outputImage = 'output_'.$_GET['inputImage'];

exec( "convert {$inputImage} \( +clone -blur 10x8 \) \ 
          \( +clone -gamma 0 -fill white \ 
          -draw 'rectangle {$x1},{$y1} {$x2},{$y2}' -blur  10x4 \) \ 
          -composite  {$outputImage}" ); 

echo $outputImage;

The JavaScript takes care of retrieving coordinate values from imgAreaSelect and sending them to our PHP script by AJAX, so for person using it, it appears as images is transformed on their own machine. Using a callback function we replace presented image with a newly transformed one once PHP is finished with its job.

Useful links

Comments

There are no comments for this post

Leave a comment

Antispam code

Enter the text you see to the left

Web Design Shrewsbury telephone 08000 805401

Web Design Manchester telephone 0161 7440075

Web Design Birmingham telephone 0121 7750085

Web Design London telephone 0207 1250044

Valid XHTML/CSS © Mutiny Design - Website Design and Development - Network House, Badgers Way, Oxon Business Park, Shrewsbury, Shropshire SY3 5AB