PHP imagefilter参数问题 [英] PHP imagefilter parameter question

查看:148
本文介绍了PHP imagefilter参数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于filtertype参数值IMG_FILTER_CONTRAST,它可以是哪个数字值.

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

推荐答案

即使文档中指出-255到+255,也不是! 假定为-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的数量没有限制为100.它直接传递给具有您指定的 任何 编号的基础lib-gd. lib-gd也不将范围限制为100,因此无论您使用什么数字,都会直接影响像素.

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.

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

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

(100.0-contrast)/100.0

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

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

假定该公式 可以将您在PHP中要求的对比度(0到100之间)转换为0到1之间的数字.

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.

如果在PHP中输入90,lib-GD会将其转换为0.9,并使用该数字应用对比度算法.说得通. 但是,如果您输入2000,则lib-gd的对比度算法现在使用-19,这是千差万别的.

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.

首先,您会注意到高于100 或低于-100的的任何值都具有提高对比度的相同效果,因为数学.

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

要获得绝对"对比度效果,即将图片中的所有像素移动到0或255, 25600 是您想要的数字.值为127的像素将变为0,而值为128的像素将变为255.

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).

尽管我不会依赖这种行为,因为PHP或lib-gd都可以开始限制新版本中的范围.

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

因此,实际上:

  • IMG_FILTER_CONTRAST的范围是-25600到+25600
  • 上方和下方的数字不会被拒绝,但不会进一步影响像素.
  • 低于-100的数字再次变为正数,即-100 === +100
  • 随着数字成千上万,由于像素非常接近其最大值,因此视觉差异很小.
  • 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.

这篇关于PHP imagefilter参数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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