This function is quite slow and prone to time out. Large values for the cluster and smooth threshold appear to be faster, and so safer.
function segmentImage($image_path, $colorSpace, $clusterThreshold, $smoothThreshold)
{
$imagick = new \Imagick(realpath($image_path));
$imagick->segmentImage($colorSpace, $clusterThreshold, $smoothThreshold);
header("Content-Type: image/jpeg");
echo $imagick->getImageBlob();
}