Hey guys I need help.
Does php have a function that will convert numbers like this:
Lets say i have a range from 0 to 100. Is there a way to convert that into 100 to 500?

In Adobe After Effects there is an expression "linear" which is what I want to do in PHP:

 linear(value, 0, 100, 20, 80)
After Effects Help File wrote:

This method—like all of the Interpolation methods—can also be used to convert from one range of values to another. For example this expression on the Opacity property converts the Opacity values from the range 0%-100% to the range 20%-80%:

    Well, it depends on what kind of "conversion" you want. For example, one function that maps the range of integers [0,100] to the range of integers [100,500] is f(x)=4x+100. Therefore, I can implement this function in PHP:

    <?php
    function f($x) {
        return 4 * $x + 100;
    }
    ?>

    and now we have a way to convert the range from 0 to 100 to the range from 100 to 500.

      ok great! Now we're on the right track... But how would I rewrite that so that I can use any range of numbers?

      f(value, 0, 100, 100, 500)

        hmm... considering the way I came up with that formula, I would suggest:

        <?php
        function linear($value, $source_begin, $source_end, $dest_begin, $dest_end) {
            $multiplier = ($dest_end - $dest_begin) / ($source_end - $source_begin);
            return $multiplier * $value + ($dest_begin - $source_begin);
        }
        
        echo linear(50, 0, 100, 100, 500);
        ?>

        However, if you want to replicate exactly what linear does in Adobe After Effects, then you would need to do more research.

          Thanks a lot that's exactly what I wanted!

            Write a Reply...