I have a function that trims white space from an image. function source:
This works perfectly for my needs. I am trying to adapt it to detect transparency, but it isn't detecting it.
Current function:
//load the image
$img = imagecreatefromgif("img.gif");
//find the size of white border.
$border = 0;
while(imagecolorat($img, $border, $border) == 0xFFFFFF) {
$border++;
}
//copy the contents, excluding the border
//This code assumes that the border is the same size on all sides of the image.
$newimg = imagecreatetruecolor(imagesx($img)-($border*2), imagesy($img)-($border*2));
imagecopy($newimg, $img, 0, 0, $border, $border, imagesx($newimg), imagesy($newimg));
//finally, if you want, overwrite the original image
imagejpeg($newimg, "img.jpg");
Latest Attempt:
//load the image
$img = imagecreatefromgif("img.gif");
//find the size of transparent border.
$border = 0;
while((imagecolorat($img, $border, $border) >> 24) & 0x7F) {
$border++;
}
//copy the contents, excluding the border
//This code assumes that the border is the same size on all sides of the image.
$newimg = imagecreatetruecolor(imagesx($img)-($border*2), imagesy($img)-($border*2));
imagecopy($newimg, $img, 0, 0, $border, $border, imagesx($newimg), imagesy($newimg));
//finally, if you want, overwrite the original image
imagejpeg($newimg, "img.jpg");