Short answer: Yes
Long answer: Yes, it's possible (how was already mentioned in comments: basically everything is possible), but you won't be able to write the algorithm without knowledge of image processing & pattern recognition; it's actually kinda rocket science :)
So that means, that you'll need to use some library. I would recommend OpenCV which is quite mature open source library built for this purpose (any many others). There is already built-in detector of faces.
I suggest to search for "OpenCV in PHP" to find PHP wrapper for this library and then you'll need to figure out how to pass the image properly to find face(s) in OpenCV in $filetoReplace
and how to replace rectange/oval with $fileReplaceWith
.