两个异常的PHP运算符一起使用以获取图像像素颜色,请解释 [英] Two unusual PHP operators used together to get image pixel color, please explain

查看:79
本文介绍了两个异常的PHP运算符一起使用以获取图像像素颜色,请解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP imagecolorat()函数可用于获取图片像素的RGB值,如文档所示:

The PHP imagecolorat() function can be used to get the RGB values of an image pixel, as demonstrated in the documentation:

$im = imagecreatefrompng("php.png");
$rgb = imagecolorat($im, 10, 15);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;

我根本不了解最后三行;我知道它们返回正确的值,但是我无法弄清楚>>&运算符如何一起工作.有人可以解释一下吗?

I don't understand the last three lines at all; I know that they return the correct values, but I cannot figure out how the >> and & operators work together to do so. Could someone please explain?

作为参考,当$rgb = 9750526时,RGB值显示为(148,199,254).

For reference, when $rgb = 9750526, the RGB values turn out to be(148,199,254).

感谢您的答复.

推荐答案

&和>>是两个不同的按位运算符(

& and >> are two different bitwise operators (Bitwise operators in PHP). They work with the bit representation of the values, which gives us a few useful (and fast!) possibilities.

>> 右移.如果您右移一个值,则将所有位向右移动到表示形式中.如果您有"11100"并将其右移两个位,那么您将剩下"111"(在右端移开的位会消失). >>右侧的数字表示要移位的位数.

>> is right shift. If you right shift a value, you're moving all the bits to the right in the representation. If you have "11100" and right shift it by two places, you're left with "111" (the bits shifted off on the right end disappears). The number on the right side of >> indicates the number of bits to shift.

& 按位和.如果您有两个表示为111000和101011的值,并决定按位进行运算,则最终将得到101000(两个值中设置相同位的位将为1,否则为0).

& is bitwise and. If you have two values represented as 111000 and 101011, and decide to bitwise and them, you'll end up with 101000 (the bits where the same bits in both values are set will be 1, otherwise 0).

我们现在已经有了需要理解上面代码的东西. $ rgb包含一个整数值(该值并不有趣,但是位模式是),它表示24位(RGBA为32位,但在这里我们将忽略它).该值由红色的8位,绿色的8位和蓝色的8位组成.但是,我们对这24位表示的位数不感兴趣,而对分别的R,G和B值是什么感兴趣.

We now have what we need to make sense of the code above. $rgb contains an integer value (the value is not interesting, but the bit pattern is), which represents 24 bits (32 bits for RGBA, but we'll ignore that here). The value consists of 8 bits for red, 8 bits for green and 8 bits for blue. We're however not interested in what number those 24 bits represent together, but what the separate R, G and B values are.

我们的值代表颜色的以下位:

Our value represent the following bits for the colors:

rrrrrrrr gggggggg bbbbbbbb 

如果将此位掩码更改为>> 16,则会得到:

If we shift this bitmask with >> 16, we'll get:

                  rrrrrrrr

这就是>> 16的工作,只剩下代表红色的位.现在我们将位一直向下移动,该值表示[0,255]中的数字.然后,我们添加& 255(这里用十六进制表示为0xFF)以除去我们感兴趣的8以上的任何杂散位.当我们看一下如何获得G值时,我们将开始研究这种情况:

This is what >> 16 does, and leaves us with just the bits representing the red color. As we've now moved the bits all the way down, the value represents a number in [0, 255]. We then add & 255 (which here is written as 0xFF in hexadecimal notation) to remove any stray bits above the 8 we're interested in. We'll get to how that happens now, when we look at how we get the G value:

我们的价值观仍然勇敢地代表着不同的颜色:

Our value still bravely represents the different colors:

rrrrrrrr gggggggg bbbbbbbb 

如果我们用>> 8将位掩码移8个位置,则会得到:

If we shift this bitmask 8 places with >> 8, we'll get:

         rrrrrrrr gggggggg

但是等等!这不仅是代表g的位,而且还代表r!的位!这不是我们追求的值(因为我们只想要g).在前面的示例中,我们很幸运,因为我们没有在r上方设置任何位,但是这次我们真的为我们完成了工作.但是我们了解& ;,是时候实际了解它了.

But wait! This is not just the bits that represents g, but also the bits that represent r! This is not the value we're after (as we just want g's). We were lucky in the previous example since we didn't have any bits set above r, but this time we've really got our work cut out for us. But we know about &, and it's time to see it actually to it's stuff.

我们现在有:

         rrrrrrrr gggggggg

然后我们应用& 255(代码中的0xFF),以位值表示为:

And then we apply & 255 (0xFF in your code), which is representing in a bit value as:

         00000000 11111111

自&仅保留在两个操作数中设置的位,我们将仅得到g值:

Since & only keeps the bits which are set in both operands, we'll end up with just the g values:

                  gggggggg

在移位后的值中表示红色的位现在为0,只剩下代表原始颜色的绿色的位.由于它们已经转移到表示[0,255]的相同8位中,所以我们又得到了我们的价值.

The bits that represented red in the shifted value are now 0, and we're left with only the bits representing the green of the original color. As they've been shifter into the same 8 bits that represent [0, 255], we've got our value yet again.

最后一个值比较容易,因为它已经位于我们想要的位置:位于表示[0,255]的位中.我们可以做&在这里进行操作,然后走到另一边:

The last value is easier, as it's already located where we want it: in the bits representing [0, 255]. We can just do the & operation here, and get out on the other side:

rrrrrrrr gggggggg bbbbbbbb & 
00000000 00000000 11111111

最终以:

                  bbbbbbbb

我们也有蓝色值.

希望如此!

这篇关于两个异常的PHP运算符一起使用以获取图像像素颜色,请解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆