PHP imagefilter参数问题

发布于 2024-10-24 18:42:01 字数 53 浏览 1 评论 0原文

对于filtertype参数值IMG_FILTER_CONTRAST,其范围可以是多少数值。

for the filtertype parameter value IMG_FILTER_CONTRAST what number values can it range from.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葵雨 2024-10-31 18:42:01

尽管文档规定为 -255 到 +255,但事实并非如此! 应该为 -100 到 +100。但是,还有一个更深层次的问题:

PHP 并不将数字限制为 100。它会使用您指定的任何数字直接传递到底层 lib-gd。 lib-gd 也不将范围限制为 100,因此无论您使用什么数字都会对像素产生直接影响。

在 lib-gd 中,使用以下公式计算对比度:

(100.0-contrast)/100.0

您可以在这里亲自查看:https://bitbucket.org/libgd/gd-libgd/src/cdea9eb0ad01/src/gd_filter.c

这个公式应该来改变对比度您在 PHP 中请求(0 到 100 之间)转换为 0 到 1 之间的数字。

问题是,因为该范围永远不会被检查,所以它会对范围之外的数字产生数学上奇怪的影响。

如果您在 PHP 中输​​入 90,lib-GD 会将其转换为 0.9,并使用该数字应用对比度算法。有道理。 但是,如果您输入 2000,lib-gd 现在在其对比算法中使用 -19,这是非常不同的。

首先,您会注意到任何高于 100低于 -100 的值都具有相同的效果,即增加对比度,这是由于数学原因。

要实现“绝对”对比度效果,即将图片中的所有像素移动到 0 或 255,25600 就是您想要的数字。值为 127 的像素将变为 0,值为 128 的像素将变为 255。

如果您想让图像完全平坦的颜色(特别是如果您先应用灰度滤镜,则这会很有用)得到全黑和白)。

不过,我不会依赖这种行为,因为 PHP 或 lib-gd 可能会开始限制新版本中的范围。

因此,实际上:

  • IMG_FILTER_CONTRAST 的范围是 -25600 到 +25600
  • 上方和下方的数字不会被拒绝,但不会进一步影响像素。
  • 低于 -100 的数字再次变为正值,即 -100 === +100
  • 当数字达到数千时,视觉差异很小,因为像素非常接近其最大值。

Even though the documentation states -255 to +255, it's not! It's supposed to be -100 to +100. But, there's a deeper issue:

PHP doesn't limit the number to 100. It's passed straight through to the underlying lib-gd with whatever number you specify. lib-gd also doesn't limit the range to 100, so whatever number you use has a direct effect on the pixels.

In lib-gd, the following formula is used to calculate the contrast:

(100.0-contrast)/100.0

You can see this for yourself here: https://bitbucket.org/libgd/gd-libgd/src/cdea9eb0ad01/src/gd_filter.c

This formula is supposed to turn the contrast you've requested in PHP (between 0 and 100) into a number between 0 and 1.

Problem is, because the range is never getting checked, it has a mathmatically weird effect on numbers outside the range.

If you enter 90 in PHP, lib-GD translates that to 0.9, and applies a contrast algorithm using that number. Makes sense. HOWEVER, if you enter 2000, lib-gd is now using -19 in its contrast algorithm, which is wildly different.

Firstly, you'll note any value above 100 or below -100 has the same effect of increasing the contrast, because of the maths.

To achieve an 'absolute' contrast effect, i.e. moving all pixels in the picture to either 0 or 255, 25600 is the number you want. A pixel with a value of 127 will become 0, and a pixel with a value of 128 will become 255.

This can be useful if you want to make an image completely flat colour (especially if you apply a greyscale filter first, you'll get full black and white).

I wouldn't rely on this behaviour though, because either PHP or lib-gd could start limiting the range in new releases.

So, in effect:

  • The range of IMG_FILTER_CONTRAST is -25600 to +25600
  • Numbers above and below won't be rejected, but can't affect the pixels further.
  • Numbers below -100 become positive again, i.e. -100 === +100
  • As numbers get into the thousands, visual differences are minor as the pixels are so very exponentially close to their maximum.
如果没有 2024-10-31 18:42:01

-255255之间

MG_FILTER_CONTRAST 过滤器允许您
改变图像的对比度,
并且只需要一个参数
对比度值介于 -255255 之间。
较低的值会增加对比度
图片,本质上减少了
颜色数量,以便它们是
肉眼看来更加独立和明显。
使用积极的价值观会带来
通过混合使颜色更接近
灰色,直到 255 你有一个
全灰度图片。

来源

Between -255 and 255

MG_FILTER_CONTRAST filter allows you
to change the contrast of the image,
and takes just one parameter for a
contrast value between -255 and 255.
Lower values increase the contrast of
the picture, essentially reducing the
number of colours so that they are
more separate and obvious to the eye.
Using positive values brings the
colours closer together by mixing them
with grey, until at 255 you have a
full-grey picture.

Source

つ低調成傷 2024-10-31 18:42:01

范围显然是 0 到 100。在 100 时,每个像素都是 7F7F7F。值为 0 时,每个像素都与原始像素相同。这是明智的行为。这意味着当值为 0 时,每种原始颜色与所有颜色之间可以保持的最大差异。

在 101 时,结果与 99 时的结果相同。在 200 时,与 0 时的结果相同。因此无需超过 100。这只是重复 0 到 100 范围内的内容。6

人们很容易认为该范围可能是延伸到负面。但如果有人尝试这样做,他们并没有创建一个对称的范围。在 -100 时,效果是将颜色推离范围的中间。 7F 将变成 7D。 80 将变为 82。7E 将变为 79。81 变为 86。9F 变为 FC。 A0 及以上的所有内容都变为 FF。 5F 及以下的所有内容都变为 00。是的,从 5F 到 A0 的颜色相差 4。但是 0 到 5F 和 A0 到 FF 根本没有区别。他们的对比实际上消失了。而且这种影响仍在继续。

关键点是 -100 不是 100 的镜像。-100 与 300 匹配。范围的更自然的端点是 7F 和 80 变为 0 和 FF 的点。那是-2159。以下是 80 (128) 的数学计算:

(100 - -1497) / 100.0 = 15.97  
15.97 * 15.97 = 249.3241  
128 / 255 - .5 = .00196...

将最后两个结果相乘,得到一个略高于 0.5 的数字。将 -1497 更改为 -1496,最终会得到一个略低于 0.5 的数字。如果你用 7F (127) 重复这个练习,你会在相反的方向上得到相同的结果。下一步是加 0.5,然后乘以 255,并将结果截断为最接近的整数(255 或 0)。

有意义的范围实际上是 -1497 到 100。或者对称地,从 100 到 1697。小于 -1497 或大于 1697 的数字将与范围的端点具有相同的效果。请记住,每个范围实际上都有三个有趣的点。 100 的对比度最小,每个像素都变为 7F7F7F。 0(或200)仅返回原始图像。 -1497(或1697)返回两色图像。一切都是白色或黑色。

这个答案已经链接了(如果您想查看该算法)。

这似乎是一个奇怪的结果。 100 是最小对比度。 0 是一种最大对比度。 -1497 是不同类型的最大对比度。同时,如果不从 100 步减去,则 0 将是最小对比度; 100是一种最大对比度; 1597 是一种不同类型的最大对比度。事实上,它衡量的是相似性而不是对比性。这是向后的。

The range is rather obviously intended to be 0 to 100. At 100, every pixel is 7F7F7F. At 0, every pixel is the same as the original. That's sensible behavior. It means that at 0, there's the maximum difference between each original color that can be maintained with all the colors.

At 101, the result is the same as at 99. At 200, the same as at 0. So there's no need to go above 100. That just duplicates things from the range 0 to 100. 6

It's tempting to think that the range might extend into the negative. But if someone was attempting that, they didn't create a symmetrical range. At -100, the effect is to push the colors away from the middle of the range. 7F would become 7D. 80 would become 82. 7E would become 79. 81 to 86. 9F to FC. Everything A0 and above becomes FF. Everything 5F and below becomes 00. Yes, colors from 5F to A0 differ by 4. But 0 to 5F and A0 to FF do not differ at all. Their contrast actually disappears. And that effect continues.

The key point is that -100 is not the mirror image of 100. -100 matches with 300. A more natural endpoint of the range would be the point where 7F and 80 become 0 and FF. That's -2159. Here's the math for 80 (128):

(100 - -1497) / 100.0 = 15.97  
15.97 * 15.97 = 249.3241  
128 / 255 - .5 = .00196...

Multiply the last two results together and you get a number just over .5. Change the -1497 to -1496 and you end up with a number just under .5. If you repeat the exercise with 7F (127), you get the same result in the opposite direction. The next step is to add .5, then multiple by 255 and truncate the result to the nearest integer (255 or 0).

The meaningful range is actually -1497 to 100. Or symmetrically, from 100 to 1697. Numbers smaller than -1497 or larger than 1697 will have the same effect as the endpoints of the range. And remember, there are actually three interesting points in each range. 100 has minimal contrast, every pixel changing to 7F7F7F. 0 (or 200) just returns the original image. -1497 (or 1697) returns a two color image. Everything is white or black.

This answer already linked the source if you want to review the algorithm.

This seems like a weird result. 100 is minimum contrast. 0 is a type of maximum contrast. -1497 is a different type of maximum contrast. Meanwhile, without the subtract from 100 step, 0 would be minimum contrast; 100 would be one kind of maximum contrast; and 1597 would be a different type of maximum contrast. As is, it measures similarity rather than contrast. It's backwards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文