Gdev
Gdev

Reputation: 167

convert CSS filter brightness to PHP IMG_FILTER_BRIGHTNESS

I have an image on my page with a HTML range slider below. When I move the slider, a bit of jQuery changes the CSS filter brightness of the image.

so I can for example get CSS to reduce the brightness by 10%, 20%, double the brightness to 200% etc. That works fine.

I want **PHP **(GD) to apply the brightness changes using IMG_FILTER_BRIGHTNESS. The problem is IMG_FILTER_BRIGHTNESS doesn't use percentages, it uses 0 for 100%, 255 for pure white and -255 for pure black.

Does anyone know how I can convert the value chosen on the slider to the corresponding value needed for PHP?

Upvotes: 2

Views: 689

Answers (1)

timclutton
timclutton

Reputation: 13014

Assuming that the ranges correlate like so:

     | black | no change | white
-----|-------+-----------+-------
 CSS |     0 |         1 |     2
-----+-------+-----------+-------
 PHP |  -255 |         0 |   255

You can make a simple calculation in PHP:

$brightness = (255 - (255 * $css)) * -1;

Results:

  • If $css == 1 (100%) then $brightness == 0.
  • If $css == 0 (0%) then $brightness == -255.
  • If $css == .5 (50%) then $brightness == -127.5.
  • If $css == 1.2 (120%) then $brightness == 51.

Upvotes: 3

Related Questions