0

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 )

3
  • Your code is straight cp'd from php.net/manual/en/imagick.modulateimage.php (as a side note: notice the strange order of params from the modulateImage function versus the order of params for the imagick function) and looks ok. The values for hue, brightness and saturation are in percentages (from 0 to 100), could you add a example call for the modulateImage function you tried? Also did you try multiple call's with the same image file? Commented May 1, 2015 at 9:21
  • Oh now i understand, CSS filter uses degree not percentage, How would the calculation be to convert percentage to degree or vice versa ? Commented May 1, 2015 at 9:25
  • Sounds good, Could you submit the full answer Commented May 1, 2015 at 9:29

1 Answer 1

1

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;

Not the answer you're looking for? Browse other questions tagged or ask your own question.