Search code examples
phpimagickgmagick

PHP GMagick: how to get pixel color?


Is it possible to get color of any picture pixel with PHP Gmagick extension?

In IMagick I can get this info from getImagePixelColor method:

$color = $img->getImagePixelColor($x, $y)->getColor();

How to get this result via GMagick?


Solution

  • There isn't currently a native method to do this with Gmagick. This will get the job done though:

    $img_clone = clone $img;    // No need to modify the original object.
    $img_clone->cropImage(1, 1, $x, $y);
    $color = $img_clone->getImageHistogram()[0]->getColor();
    

    Returns

    (string) rgb(0,0,0)