how to crop a "Arbitrary" shape from an image

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Post Reply
wasedaxiao
Forum Newbie
Posts: 3
Joined: Fri Jul 07, 2006 9:55 pm

how to crop a "Arbitrary" shape from an image

Post by wasedaxiao »

If GD2 library provides functions to be able to do that?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Have you looked at the image functions?

http://www.php.net/manual/en/ref.image.php
wasedaxiao
Forum Newbie
Posts: 3
Joined: Fri Jul 07, 2006 9:55 pm

Post by wasedaxiao »

astions wrote:Have you looked at the image functions?

http://www.php.net/manual/en/ref.image.php
I have checked the functions, but could not find a way to clip an arbitrary shape(e.g. triangle) out of a photo. It seems that it can only clip rectangle shapes.

In Java, it provides such APIs to do that. So i have to implement this functions by myself?

Any idea, please.

thanks.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

wasedaxiao wrote:I have checked the functions, but could not find a way to clip an arbitrary shape(e.g. triangle) out of a photo. It seems that it can only clip rectangle shapes.

In Java, it provides such APIs to do that. So i have to implement this functions by myself?

Any idea, please.
That's the problem with Java in my opinion .. practically everything has been provided so people forget how to write their own code. PHP is pretty bad for it too.

Anyway .. to crop an arbitrary shape you'll need to work out the rectangular bounds of the area you want, copy that rectangle to a temporary canvas, then work out which bits of that rectangle are outside the area you want to keep, and you'll need to set those pixels to transparent. That'll give you a temporary image you can then copy to another image.
wasedaxiao
Forum Newbie
Posts: 3
Joined: Fri Jul 07, 2006 9:55 pm

Post by wasedaxiao »

onion2k wrote:
wasedaxiao wrote:I have checked the functions, but could not find a way to clip an arbitrary shape(e.g. triangle) out of a photo. It seems that it can only clip rectangle shapes.

In Java, it provides such APIs to do that. So i have to implement this functions by myself?

Any idea, please.
That's the problem with Java in my opinion .. practically everything has been provided so people forget how to write their own code. PHP is pretty bad for it too.

Anyway .. to crop an arbitrary shape you'll need to work out the rectangular bounds of the area you want, copy that rectangle to a temporary canvas, then work out which bits of that rectangle are outside the area you want to keep, and you'll need to set those pixels to transparent. That'll give you a temporary image you can then copy to another image.
Yes, I agree with you. As students, it's better not to use exist APIs for learning.

It seems that there are not such functions in PHP now and i will try your basic idea.

thanks for your comments. I will post back when i getting the results.
Post Reply