I can see that most of you didn't understand the question :) (question was how to validate binary data in buffer, not a file on disk).
I had same problem, and resolved it with:
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mimeType = $finfo->buffer($rawImage);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…