Javascript PHP方法来检测PNG的非透明区域
我希望转换一个Javascript函数,该函数已被编写为检测图像的不透明区域 迭代很容易转换成PHP,但是我很难获得与这里所写相同的图像数据:(getImageData和Uint32Array) 有没有一种方法可以让我用PHP获得相同的数据?给出的Javascript方法存在浏览器兼容性问题,我通过编写节点应用程序解决了这些问题。如果可能的话,我更喜欢使用PHP方法Javascript PHP方法来检测PNG的非透明区域,javascript,php,Javascript,Php,我希望转换一个Javascript函数,该函数已被编写为检测图像的不透明区域 迭代很容易转换成PHP,但是我很难获得与这里所写相同的图像数据:(getImageData和Uint32Array) 有没有一种方法可以让我用PHP获得相同的数据?给出的Javascript方法存在浏览器兼容性问题,我通过编写节点应用程序解决了这些问题。如果可能的话,我更喜欢使用PHP方法 $img=imagecreatefrompng($img\u文件); $img = imagecreatefrompng($im
$img=imagecreatefrompng($img\u文件);
$img = imagecreatefrompng($img_file);
if (!ctype_xdigit($hex)) $hex = imagecolorat($img, 0,0);
$b_top = $b_lft = 0;
$b_rt = $w1 = $w2 = imagesx($img);
$b_btm = $h1 = $h2 = imagesy($img);
do {
//top
for(; $b_top < $h1; ++$b_top) {
for($x = 0; $x < $w1; ++$x) {
if(imagecolorat($img, $x, $b_top) != $hex) {
break 2;
}
}
}
// stop if all pixels are trimmed
if ($b_top == $b_btm) {
$b_top = 0;
$code = 2;
break 1;
}
// bottom
for(; $b_btm >= 0; --$b_btm) {
for($x = 0; $x < $w1; ++$x) {
if(imagecolorat($img, $x, $b_btm-1) != $hex) {
break 2;
}
}
}
// left
for(; $b_lft < $w1; ++$b_lft) {
for($y = $b_top; $y <= $b_btm; ++$y) {
if(imagecolorat($img, $b_lft, $y) != $hex) {
break 2;
}
}
}
// right
for(; $b_rt >= 0; --$b_rt) {
for($y = $b_top; $y <= $b_btm; ++$y) {
if(imagecolorat($img, $b_rt-1, $y) != $hex) {
break 2;
}
}
}
$w2 = $b_rt - $b_lft;
$h2 = $b_btm - $b_top;
$code = ($w2 < $w1 || $h2 < $h1) ? 1 : 0;
} while (0);
如果(!ctypexdigit($hex))$hex=imagecolorat($img,0,0);
$b_top=$b_lft=0;
$b_rt=$w1=$w2=imagesx($img);
$b_btm=$h1=$h2=imagesy($img);
做{
//顶
对于(;$b_top<$h1;++$b_top){
对于($x=0;$x<$w1;++$x){
如果(imagecolorat($img,$x,$b_-top)!=$hex){
破口2;
}
}
}
//如果修剪了所有像素,则停止
如果($b_top==$b_btm){
$b_top=0;
$code=2;
破口1;
}
//底部
对于(;$b_btm>=0;--$b_btm){
对于($x=0;$x<$w1;++$x){
如果(imagecolorat($img,$x,$b_btm-1)!=hex){
破口2;
}
}
}
//左
对于(;$b_lft<$w1;++$b_lft){
对于($y=$b_-top;$y=0;--$b_-rt){
对于($y=$b_top;$y php(至少gd)不像javascript那样处理像素。你通常需要在x/y上循环并使用和x/y坐标来获得特定像素的颜色数据。你想用一个工作示例来详细说明吗?我尝试过使用imagecolorat,但我对结果非常不熟悉,并且不确定如何对RGB/Alpha位进行排序以正确迭代。有是手册中关于如何使用imagecolorat()
的示例。您只需获取图像的高度/宽度,然后循环,并使用嵌套for循环获取每个像素的颜色。
$img = imagecreatefrompng($img_file);
if (!ctype_xdigit($hex)) $hex = imagecolorat($img, 0,0);
$b_top = $b_lft = 0;
$b_rt = $w1 = $w2 = imagesx($img);
$b_btm = $h1 = $h2 = imagesy($img);
do {
//top
for(; $b_top < $h1; ++$b_top) {
for($x = 0; $x < $w1; ++$x) {
if(imagecolorat($img, $x, $b_top) != $hex) {
break 2;
}
}
}
// stop if all pixels are trimmed
if ($b_top == $b_btm) {
$b_top = 0;
$code = 2;
break 1;
}
// bottom
for(; $b_btm >= 0; --$b_btm) {
for($x = 0; $x < $w1; ++$x) {
if(imagecolorat($img, $x, $b_btm-1) != $hex) {
break 2;
}
}
}
// left
for(; $b_lft < $w1; ++$b_lft) {
for($y = $b_top; $y <= $b_btm; ++$y) {
if(imagecolorat($img, $b_lft, $y) != $hex) {
break 2;
}
}
}
// right
for(; $b_rt >= 0; --$b_rt) {
for($y = $b_top; $y <= $b_btm; ++$y) {
if(imagecolorat($img, $b_rt-1, $y) != $hex) {
break 2;
}
}
}
$w2 = $b_rt - $b_lft;
$h2 = $b_btm - $b_top;
$code = ($w2 < $w1 || $h2 < $h1) ? 1 : 0;
} while (0);