Search code examples
phpcsshuecss-filters

CSS `hue-rotate` filter to php


I'm trying to apply the css hue-rotate filter result in PHP

Currently I'm using Imagick php library using the modulateImage function to change hue like this

function modulateImage($imagePath, $hue, $brightness, $saturation) {
  $imagick = new \Imagick(realpath($imagePath));
  $imagick->modulateImage($brightness, $saturation, $hue);
  header("Content-Type: image/jpg");
  echo $imagick->getImageBlob();
}

But for some reason, applying the same value of CSS's hue-rotate to PHP function gives me different color result, I'm not sure about the calculation used in them both/ percentage/ degree, I wish someone could explain or any alternative for them ( mostly alternative for PHP, I find CSS filter is perfect for my needs, I just need to make same applies in PHP )


Solution

  • modulateImage in imagick requires a percentage for $hue while css requires degres. Here is the formula to convert : http://www.imagemagick.org/Usage/color_mods/#modulate_hue
    hue_angle = ( modulate_arg - 100 ) * 180/100; modulate_arg = ( hue_angle * 100/180 ) + 100;