本文介绍了将CSS滤镜的亮度转换为PHP IMG_FILTER_BRIGHTNESS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上有一张图片,下面有一个 HTML 范围滑块.当我移动滑块时,一些 jQuery 会更改图像的 CSS 过滤器亮度.

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.

所以我可以例如使用 CSS 将亮度降低10%,20%,将亮度增加一倍至200%,等等.

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

我希望** PHP **(GD)使用IMG_FILTER_BRIGHTNESS应用亮度更改.问题是IMG_FILTER_BRIGHTNESS不使用百分比,它对100%使用0,对于纯白色使用255,对于纯黑色使用-255.

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.

有人知道我如何将滑块上选择的值转换为 PHP 所需的相应值吗?

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

推荐答案

假定范围的相关性如下:

Assuming that the ranges correlate like so:

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

您可以在PHP中进行简单的计算:

You can make a simple calculation in PHP:

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

结果:

  • 如果$css == 1(100%),则$brightness == 0.
  • 如果$css == 0(0%),则$brightness == -255.
  • 如果$css == .5(50%),则$brightness == -127.5.
  • 如果$css == 1.2(120%),则$brightness == 51.
  • 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.

这篇关于将CSS滤镜的亮度转换为PHP IMG_FILTER_BRIGHTNESS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-31 16:31