implement flipping for php version lower 5.5
refactor greyscale