-1

I'm searching for a way to cut out any shape from an image and save it somewhere on the server. I'm looking for solutions in PHP and/or JavaScript but haven't found anything yet.

I'm not sure but as far as I know the gd-library in PHP is only capable of cutting out or copying rectangular shapes from source images. In JavaScript this probably can be achieved somehow through the canvas-object but I haven't found any libraries that provide such a function (rafael.js or paper.js don't seem to do the job) ...

Example:

Let's say i got the following image: enter image description here

Now I would like to mark a certain part of that image (by x/y-coordinates): enter image description here

And finally i want to extract that part as a single image (with the rest of the background beeing filled black): enter image description here

Thank you for any help

Benjamin
  • 1,067
  • 20
  • 30
  • You will need [`GD-library`](http://php.net/manual/en/book.image.php) – Rayon Jan 25 '16 at 10:12
  • Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it. – Epodax Jan 25 '16 at 10:23
  • 1
    Images are just a matrix of pixels. Php gives you access to each individual pixel with `imagecolorat`. Your task involves choosing which pixels to copy, and saving them into a new image. As all image files are rectangular, you must also choose a background color for pixels outside the shape, eg white or transparent. This is all possible with `GD` – Steve Jan 25 '16 at 10:32

1 Answers1

1

Using PHP only, imagecopy lets you easily copy a part of an image by coordinates and do with it as you wish.

Edit: Using Imagick it's possible to crop images to specific shapes among other functions.

Ben
  • 759
  • 1
  • 6
  • 19
  • According to the doc you linked in your comment, imagecopy only supports rectangular shapes for copying (start coordinates (x and y) and scale (width and height)) – Benjamin Jan 25 '16 at 17:02
  • 1
    http://stackoverflow.com/a/10071441/2931464 I haven't tried this suite too much myself, but it has very in-depth image manipulation capabilities like its morphology and transforming modules. It might be what you're looking for. – Ben Jan 26 '16 at 08:22
  • 1
    the actual accepted answer is the comment about imagick. Thanks for that. – Benjamin Jan 27 '16 at 01:48