检测while循环内的透明度

时间:2017-01-18 09:14:03

标签: php

我有一个修剪图像空白区域的功能。功能来源:

这完全符合我的需要。我试图调整它以检测透明度,但它没有检测到它。

当前功能:

//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");

最新尝试:

//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");

0 个答案:

没有答案