Question

It sounds harder than it really is, and I can program this, though I'm searching for shortcuts.

I have a transparent PNG, with the dimensions of 250x525 pixels. Inside is a character that can be customized by my users, thus the dimensions of the real content inside that 250x525 image can vary. I want to reduce the 250x525 image down to just the avatar, by detecting where the first pixel begins vertically and horizontally, and where the last pixel ends.

Is there a shortcut for this? A function, or another way to do it perhaps? An example of an image I'd like to apply this to: http://i.troll.ws/92f2db20.png - imagine a box around the character. I want to calculate an invisible box around it, and pull the character out of it into its own image resource in GD.

The Reason: I have a function that can take an image and scale it to fit a larger image. I want to apply this to these characters, and first need to dumb it down to just the character.

Was it helpful?

Solution

Not GD, but Imagemagick/Imagick is able to do that vith the trimImage method.

$im = new Imagick("http://i.troll.ws/92f2db20.png");
$im->trimImage(0);
$im->writeImage('image.png');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top