It sounds to me like you might have added an app that is corrupting the rotate command.
How long has this been going on? How many programs have you added recently? My suggestion is to uninstall the last couple apps you installed and see if the problem goes away. If it does, then you can add them back one at a time (with a generous amount of time in between each install to test with) And see if the problem comes back.
It might be the way one of the apps installed and by removing it and reinstalling it, you might correct the problem anyway.
I had the same problem with my iPhone 4. The screen would not rotate when viewing pic or typing and it was not locked. I thought it was maybe a recent update of a software but it wasn't that. I did the reset also. So than i thought it probably is the accelerometer as Hawk suggested. After playing two games one Labyrinth 3d and the other Tap tap revenge 3 i know this that the Labyrinth game worked good. I had very good control of the ball in all directions but when playing TTR the middle arrow for up and down taps would respond with out my input like it was stuck. Right and Left worked good. A little frustrated i threw the iPhone in my protective case two times against the carpeted floor as the last resort and voila. It's working now! I looked online and nobody mentioned that idea so i made this profile in hopes it helps someone but i'm not responsible for any damage.
Ha ha. I know it's brilliant. It still happens occasionally so duly turn off and give it hit. Amazingly I had a problem with my PC in work and this time gave it a short sharp kick. Worked like a dream. The IT team were duly amused. So glad it is now working for you