What to do if I have seen an exam before I have taken it?
In a situation in which you would suffer no matter what you do, you can also work to protect yourself. 

An anonymous note to the professor that the exam has been compromised and that there are photographs of it circulating comes close to resolving the issue. The person who took the photo likely deserves punishment, of course, so this solution doesn't resolve that. But at least academic integrity is preserved. The professor will need to provide a new exam, of course.

The professor may also announce to the class that there is a problem and ask that whoever sent the anonymous note inform him of their identity. You may have to deal with that. 

Also, you may not be the only student struggling with this. 

If you are friends with the person who took the photo, you could also confront him/her with a suggestion that they step forward. If they do this before an accusation is made, I would think any punishment would be less than otherwise.