One way to do this is to scale the image to one pixel, and then use the colors of that pixel as a reference.
<?php $image = new Imagick('800x480.jpg'); $image->scaleImage(1, 1, true); $pixel = $image->getImagePixelColor(0,0); $red = ($rgb >> 16) & 0xFF; $green = ($rgb >> 8) & 0xFF; $blue = $rgb & 0xFF; ?>
This way you do not need to handle messy parts. and you can use more intelligent scaling algorithms to achieve better accuracy.
Edit: you can use Imagick :: resizeImage if you need a more complex algorithm. It can use various algorithms, such as an interpolation filter.
source share