I am uploading an image from a form (in wordpress), and I want to allow users to use a pencil tool to draw a line around an area. Think of the lasso tool in photoshop. So users can basically ‘mark’ that area to be modified.
What I would like to see, is if it is possible to use either imagemagik, gd, or a client side JS library to find that selected area, and convert everything inside to be transparent.
here is an example image. Say a person wants to upload this image, and using the uploader, they marked a red line to select what they would like to be transparent.
So the image inside the red line, should be detected by imagemagik/gd/client js, and everything in there will be transparent.
I could not find anything to see if this was possible or not. On the server I am using PHP so I can do it there if needed with imgmagik/gd if it is possible. So I would need to know if those can take the inside of the red area and make it transparent. But I would prefer, if I could find a client side JS library or jquery plugin, that will let the end user do that on the client side via an image upload utility. and then finally upload the final modified version of the image.
Any help or input on this is greatly appreciated.
2
Answers
There’s a few tools out there using HTML5’s canvas & ImageMagick. Here’s how I would implement a solution.
Display uploaded image with a canvas element on top. Use javascript to match width & height, and bind event listeners to collect mouse movements.
Not the best, but it gives the user some feed back.
On the backend, normalize the given points into a list of (x,y) associated tuples.
At this point, you can either draw a image mask, and apply it to the image with Imagick::compositeImage. Or draw directly on the image with a Chroma Key color & swap to transparency.
You can do this with ImageMagick…
First you make everything that is not red become black like this:
Then you make it black and white and use a threshold to force reds to become white:
Now you flood fill the image with white starting at the top left corner (0,0):
Then you use that black and white image as a transparency mask for the original lion image and do all the processing steps in a single command with this:
which gives this: