Upvote:3
If you go through the UK's "check if you need a visa" tool you will find that you need a visa whether or not you plan to leave the airport, unless you meet one of the exceptions (such as if you hold a long-term "category D" visa for Spain, not an ordinary "category C" short-term visit visa).
To leave the airport (and pass through UK immigration), you'll need a Visitor in Transit visa. If you do not need or want to pass through UK immigration (even to recheck luggage because you have booked two separate tickets), you can apply for a Direct Airside Transit Visa.