for the filtertype parameter value IMG_FILTER_CONTRAST what number values can it range from.
问题:
回答1:
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
回答2:
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.