Example
function exportImagePixels($image_path)
{
$imagick = new \Imagick(realpath($image_path));
for ($y=0; $y< $imagick->getImageHeight(); $y++) {
$redPixels = $imagick->exportImagePixels(0, $y, $imagick->getImageWidth(), 1, "R", \Imagick::PIXEL_CHAR);
$average = array_sum($redPixels) / count($redPixels);
//Make the pixels that are redder than average be 100% red.
foreach ($redPixels as &$redPixel) {
if ($redPixel > $average) {
$redPixel = 255;
}
}
$imagick->importImagePixels(0, $y, $imagick->getImageWidth(), 1, "R", \Imagick::PIXEL_CHAR, $redPixels);
}
header("Content-Type: image/jpeg");
echo $imagick->getImageBlob();
}